insertexample.java

来自「比较实用的C++编程思想 有很多例子可以用」· Java 代码 · 共 68 行

JAVA
68
字号
/* * This sample shows how to insert data in a table. */// You need to import the java.sql package to use JDBCimport java.sql.*;class InsertExample{  public static void main (String args [])       throws SQLException  {    // Load the Oracle JDBC driver    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());    // Connect to the database    // You can put a database name after the @ sign in the connection URL.    Connection conn =      DriverManager.getConnection ("jdbc:oracle:oci8:@", "scott", "tiger");    // Prepare a statement to cleanup the emp table    Statement stmt = conn.createStatement ();    try    {      stmt.execute ("delete from EMP where EMPNO = 1500");    }    catch (SQLException e)    {      // Ignore an error here    }    try    {      stmt.execute ("delete from EMP where EMPNO = 507");    }    catch (SQLException e)    {      // Ignore an error here too    }    // Close the statement    stmt.close();    // Prepare to insert new names in the EMP table    PreparedStatement pstmt =       conn.prepareStatement ("insert into EMP (EMPNO, ENAME) values (?, ?)");    // Add LESLIE as employee number 1500    pstmt.setInt (1, 1500);          // The first ? is for EMPNO    pstmt.setString (2, "LESLIE");   // The second ? is for ENAME    // Do the insertion    pstmt.execute ();    // Add MARSHA as employee number 507    pstmt.setInt (1, 507);           // The first ? is for EMPNO    pstmt.setString (2, "MARSHA");   // The second ? is for ENAME    // Do the insertion    pstmt.execute ();    // Close the statement    pstmt.close();    // Close the connecion    conn.close();  }}

⌨️ 快捷键说明

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