insertanddelete.java

来自「java 完全探索的随书源码」· Java 代码 · 共 97 行

JAVA
97
字号
import java.sql.*;public class InsertAndDelete{  // Private reference to an instance of the Connection  Connection connection = null;  // Default Constructor  public InsertAndDelete( Connection conn )  {    super();    connection = conn;  }  // Private accessor for the connection  private Connection getConnection()  {    return connection;  }  // Insert and Delete a new Employee Record  public void insertAndDelete()  {    Statement stmt = null;    ResultSet rs = null;    try    {      // Get a scrollable ResultSet      Connection conn = getConnection();      stmt = conn.createStatement(                                  ResultSet.TYPE_SCROLL_SENSITIVE,                                  ResultSet.CONCUR_READ_ONLY );      // Get all of the fields from the employee table      rs = stmt.executeQuery( "SELECT * from DUMMY" );      System.out.println( rs.getType() );      System.out.println( rs.getConcurrency() );      //Move the cursor to where we need to insert a new row      rs.moveToInsertRow();      // Even though we are perfoming an insert, we have to use the      // udpate Methods        rs.updateString( "DUMMY", "3154" );//      rs.updateTimestamp( "HIREDATE", new Timestamp(System.currentTimeMillis()) );//      rs.updateFloat( "SAL", 35 );//      rs.updateInt( "DEPTNO", 10 );//      rs.updateString( "ENAME", "BOB" );//      rs.updateInt( "JOB", 1 );//      rs.updateInt( "MGR", 7902 );      // Cause the update changes to take effect      rs.insertRow();      // Move to the last row and print out the values so      // we know that it worked      rs.last();      System.out.println( "Data values for the last row" );      int empNo = rs.getInt( "EMPNO" );      System.out.println( empNo );      rs.deleteRow();      // Close the connection      rs.close();    }    catch( SQLException ex )    {      ex.printStackTrace();    }  }  public static void main(String[] args)  {     // Use the previous DatabaseManager    Connection conn = DatabaseManager.getConnection();    InsertAndDelete example = new InsertAndDelete( conn );    example.insertAndDelete();    // Always make sure to close the connection when you are finished    try    {      conn.close();    }    catch( SQLException ex )    {      ex.printStackTrace();    }    catch( Exception ex )    {      ex.printStackTrace();    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?