Metadata meta data
/ * Metadata meta data * / Public classTest03 {Connection conn =NULL; PreparedStatement pstmt=NULL; ResultSet rs =NULL; Public void Test() {conn=dbutil.getconnection ();Try{DatabaseMetaData dbmd=conn.getmetadata ();//Get database meta dataSystem. out. println (Dbmd.getdatabaseproductname ()); System. out. println (Dbmd.getdatabaseproductversion ()); System. out. println (Dbmd.getdrivername ()); System. out. println (Dbmd.getdriverversion ()); Pstmt=conn.preparestatement ("Select Username,password from User"); Rs=pstmt.executequery ();//Get result set meta dataResultSetMetaData Rsmd=rs.getmetadata (); System. out. println (Rsmd.getcolumncount ()); System. out. println (Rsmd.getcolumnname (1)); System. out. println (Rsmd.getcolumnname (2)); }Catch(SQLException e) {E.printstacktrace (); } } Public Static void Main(string[] args) {NewTest03 (). Test (); }}
Scrollable result set
/ * Scrollable result set * /public class TEST04 {Connection conn = null;PreparedStatement pstmt = null;ResultSet rs = null;Insert a record when reading to a specified user public void test01 () {String sql ="SELECT * from User";try {conn = Dbutil. getconnection();PSTMT = conn. Preparestatement(SQL, ResultSet. TYPE_scroll_insensitive, ResultSet. CONCUR_updatable);rs = pstmt. ExecuteQuery();//scrollable result set, can update tablewhile (RS. Next()) {String name = rs. getString("username");if (name. Equals("Tom") {//To determine the currently read user//Move the cursor to the Insert row, the Insert row is actually a buffer to prepare the data RS. Movetoinsertrow();Rs. updatestring("username","Alex");Rs. updatestring("Password","999");Data in the commit buffer RS. InsertRow();System. out. println("Insert User success!" ");Moves the cursor to the current line, which is the record before the record is inserted RS. Movetocurrentrow();} System. out. println(RS. GetInt(1) +","+ RS. getString(2) +","+ RS. getString(3));}} catch (SQLException e) {E. Printstacktrace();} finally {Dbutil. CloseAll(RS, PSTMT, conn);}}//delete the specified user public void test02 () {String sql ="SELECT * from User";try {conn = Dbutil. getconnection();PSTMT = conn. Preparestatement(SQL, ResultSet. TYPE_scroll_insensitive, ResultSet. CONCUR_updatable);rs = pstmt. ExecuteQuery();while (RS. Next()) {String name = rs. getString("username");if (name. Equals("BBB")) {RS. DeleteRow();//delete current lineRs. Next();//Remove the cursor downSystem. out. println("Delete User Success");} System. out. println(RS. GetInt(1) +","+ RS. getString(2) +","+ RS. getString(3));}} catch (SQLException e) {E. Printstacktrace();} finally {Dbutil. CloseAll(RS, PSTMT, conn);}}//move cursor public void test03 () {String sql ="SELECT * from User";try {conn = Dbutil. getconnection();PSTMT = conn. Preparestatement(SQL, ResultSet. TYPE_scroll_insensitive, ResultSet. CONCUR_updatable);rs = pstmt. ExecuteQuery();while (RS. Next()) {String name = rs. getString("username");if (name. Equals("s001")) {RS. Relative(3);} System. out. println(RS. GetInt(1) +","+ RS. getString(2) +","+ RS. getString(3));}} catch (SQLException e) {E. Printstacktrace();} finally {Dbutil. CloseAll(RS, PSTMT, conn);}} public static void Main (string[] args) {new Test04 (). Test03 ();}}
Java Learning Notes (55)-meta data metadata and scrollable result sets