Exercise about JDBC usage

zhaozj2021-02-17  50

/ **

Microsoft driver has been installed

JDK1.4; Win2000 Server; MS SQL2000;

* /

Import java.sql. *;

Class SQL {Public Static Void Main (String [] AGRS) {Connection CN = NULL; STATEMENT STMT = NULL; STRING SQL = NULL;

Try {class.Forname ("com.microsoft.jdbc.sqlser.sqlserverdriver);} catch (classnotfoundexception ex) {system.out.println (" not find the driver! ");}

Try {string URLS = "JDBC: Microsoft: SQLServer: // localhost: 1433; database = Webroot"; // Webroot library name .talbe is a table name; string user = "sa"; string password = "password"; cn = Drivermanager.getConnection (URLS, User, Password);

//stmt=cn.createstatement (ResultSet.Type_scroll_insensitive ,Resultset.concur_read_only );stmt=cn.createstatement (ResultSet.Type_scroll_insensitive ,Resultset.concur_updata);

SQL = "SELECT TOP 10 * from Table1"; ResultSet RS = Stmt.executeQuery (SQL); while (rs.next ()) {system.out.println (rs.getstring (2) " rgetstring 3));

}

rs.first (); system.out.print (rs.getrow () ""); system.out.println (rs.getstring (2) "1" rgetstring (3));

Rs.last (); system.out.print (rs.getrow () ""); System.out.Println (rs.getstring (2) "2" rs.getstring (3));

Rs.PREvious (); system.out.print (rs.getrow () ""); system.out.println (rs.getstring (2) "3" rgetstring (3));

Rs.next (); system.out.print (rs.getrow () ""); system.out.println (rs.getstring (2) "4" rgetstring (3));

rs.absolute (2); system.out.print (rs.getrow () ""); system.out.println (rs.getstring (2) "5" r.getstring (3)); / * rs.afterlast (); system.out.print (rs.getrow () ""); system.out.println (rs.getstring (2) " rs.getstring (3)); system.out. Print (rs.isafterlast ()); rs.beforefirst (); system.out.print (rs.getrow () ""); system.out.println (rs.getstring (2) " rgetstring (3)); * /

String SQL1 = "Update Table1 set topic =? WHERE ID =?"; PreparedStatement Stmt1 = cn.preparestatement (SQL1); string stat = new string ("Summer Difficult Summer Health"); String Stat1 = Unicoidetogb (STAT); / / Solve Chinese issues stmt1.setstring (1, stat1); Stmt1.setint (2,3423); stmt1.executeUpdate (); cn.commit ();

//System.out.println (stat1); //system.exit (0);

//cn.setautoCommit (False); "Update Table1 Set Title = 'Summer Trolls Foot Health 1' WHERE Id = '3407'"); Stmt.addbatch ("Update Table1 Set Title = 'Summer Prevention Midstroke Meal 1 'WHERE ID =' 3408 '"); Stmt.addbatch (" INSERT INTO TABLE1 VALUES ("11,' 12 ',' 13 ',' ',' ')"); Stmt.addbatch ("Insert Into Table1 Values ​​('12 ',' 12 ',' 13 ',' ',' ') "); stmt.addbatch (" INSERT INTO TABLE1 VALUES ('13', '12', '13', ',' ') "); Stmt.addbatch (" INSERT INTO TABLE1 VALUES ("114,' 12 ',' 13 ',' ',' ')"); Stmt.addbatch ("Insert Into Table1 Values ​​('15', '12', '13', '', '') "); Stmt.addbatch (" INSERT INTO TABLE1 VALUES ("16 ',' 12 ',' 13 ',' ',' ')"); STMT. Addbatch ("INSERT INTO TABLE1 VALUES ('17, '12', '13', '', '')"); stmt.addbatch ("INSERT INTO TABLE1 VALUES ("18 ',' 12 ',' 13 ' , '', '') "); Int [] Updatecounts = stmt.executebatch (); cn.commit (); stmt.close (); cn.close ();} catch (sqlexception e) {system.out. Println ("The Sqlexception Error!");

} / ** /

转载请注明原文地址:https://www.9cbs.com/read-29462.html

New Post(0)