jdbc行级对数据库进行增删改
jdbc行级对数据库进行增删改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| package RowSet; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Properties; import com.sun.rowset.CachedRowSetImpl;
public class Test4 { public static void main(String[] args) throws Exception { Properties p = new Properties(); p.load(new FileInputStream("Ch4/RowSet/db.properties")); Class.forName(p.getProperty("dirStr")); Connection con = DriverManager.getConnection(p.getProperty("conStr"),p.getProperty("username"),p.getProperty("pwd")); Statement stm = con.createStatement(); String sql = "select id,chinese,english,history from score"; ResultSet rs = stm.executeQuery(sql); CachedRowSetImpl rows = new CachedRowSetImpl(); rows.populate(rs); con.setAutoCommit(false); while(rows.next()){ System.out.println(rows.getInt("id")+"\t"+rows.getInt("chinese")+"\t"+rows.getInt("english")+"\t"+rows.getInt("history")); }
rows.setTableName("score"); rows.moveToInsertRow(); rows.updateInt(1, 6); rows.updateInt("chinese", 10); rows.updateInt("english", 10); rows.updateInt(4, 10); rows.insertRow(); rows.moveToCurrentRow(); rows.acceptChanges(con); } }
|