import java.io.FileInputStream; import java.util.Properties; import javax.sql.rowset.JdbcRowSet; import javax.sql.rowset.RowSetFactory; import javax.sql.rowset.RowSetProvider; public class JdbcRowSetTest { private String driver; private String url; private String username; private String password; public void initParam(String paramFile)throws Exception { Properties props = new Properties(); props.load(new FileInputStream(paramFile)); driver = props.getProperty("driver"); url = props.getProperty("url"); username = props.getProperty("username"); password = props.getProperty("password"); } public void update(String sql)throws Exception { Class.forName(driver); RowSetFactory factory = RowSetProvider.newFactory(); try( JdbcRowSet jdbcRs = factory.createJdbcRowSet(); ) { jdbcRs.setUrl(url); jdbcRs.setUsername(username); jdbcRs.setPassword(password); jdbcRs.setCommand(sql); jdbcRs.execute(); jdbcRs.afterLast(); while(jdbcRs.previous()) { System.out.println(jdbcRs.getString(1) +"\t"+jdbcRs.getString(2)+"\t" +jdbcRs.getString(3)+"\t" +jdbcRs.getString(4)); if(jdbcRs.getInt(1)==100) { jdbcRs.updateString(2, "灵感"); jdbcRs.updateRow(); } } } } public static void main(String[] args) throws Exception{ JdbcRowSetTest jt = new JdbcRowSetTest(); jt.initParam("Mysql.ini"); jt.update("select * from user"); } }
时间: 2024-10-22 05:02:33