0755mx 发表于 2018-9-22 13:11:04

使用PreparedStatement操作Oracle数据库

使用PreparedStatement操作Oracle数据库:  1.定义预处理SQL语句;
  2.为每一个“?”设置对应的值;
  3.使用PreparedStatement操作数据库。
  TestPreparedStmt.java:
view plaincopy to clipboardprint?
[*]import java.sql.*;
[*]
[*]
[*]public class TestPreparedStmt {
[*]
[*]    public static void main(String[] args) {
[*]      Connection conn = null;
[*]      PreparedStatement pstmt = null;
[*]      String sql = null;
[*]      int id = 0;
[*]      String name = null;
[*]      if (args.length != 2) {
[*]            System.exit(-1);
[*]      } else {
[*]            id = Integer.parseInt(args);
[*]            name = args;
[*]      }
[*]      try {
[*]            Class.forName("oracle.jdbc.driver.OracleDriver");
[*]            conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:mgc", "system", "admin");
[*]            sql = "Insert INTO member(id,name) VALUES(?,?)";
[*]            pstmt = conn.prepareStatement(sql);
[*]            pstmt.setInt(1, id);
[*]            pstmt.setString(2, name);
[*]            pstmt.executeUpdate();
[*]      } catch (ClassNotFoundException e) {
[*]            e.printStackTrace();
[*]      } catch (SQLException e) {
[*]            e.printStackTrace();
[*]      } finally {
[*]            try {
[*]                if (pstmt != null) {
[*]                  pstmt.close();
[*]                  pstmt = null;
[*]                }
[*]                if (conn != null) {
[*]                  conn.close();
[*]                  conn = null;
[*]                }
[*]            } catch (SQLException e) {
[*]                e.printStackTrace();
[*]            }
[*]      }
[*]    }
[*]
[*]}

页: [1]
查看完整版本: 使用PreparedStatement操作Oracle数据库