Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2567185
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-12-27 16:58:28

The “PreparedStatement” interface is extended “Statement”, with extra feature to send a pre-compiled SQL statement with parameters. For create, insert, update or delete statement, uses “PreparedStatement.executeUpdate(sql)“; select query, uses “PreparedStatement.executeQuery(sql)“.

完整代码:
JDBCMySQL.java
  1. package org.hnrsc.jdbc;

  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;

  8. public class JDBCMySQL {

  9.     private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
  10.     private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306/test";
  11.     private static final String DB_USER = "root";
  12.     private static final String DB_PASSWORD = "";
  13.     
  14.     
  15.     public static void main(String[] args) {
  16.         try{
  17.             //createTable();
  18.             insertRecord();
  19.             batchInsertRecords();
  20.             deleteRecords();
  21.         
  22.             selectRecords();
  23.         }catch(SQLException e){
  24.             System.out.println(e.getMessage());
  25.         }
  26.     }
  27.     
  28.     /*
  29.      * BUG:
  30.      * INSERT INTO h_user(username) VALUES(?,?)
  31.      * You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?)' at line 1
  32.      *You're calling Statement.executeUpdate(String) with your prepared statement placeholder
  33.      *query, which is incorrect. You should be calling PreparedStatement.executeUpdate().
  34.      *Remove the reference to the query from your call to executeUpdate() and things should
  35.      *work.
  36.      */
  37.     private static void insertRecord() throws SQLException{
  38.         Connection conn = null;
  39.         PreparedStatement prep = null;
  40.         
  41.         String sql = "INSERT INTO h_user"
  42.             +"(username,password) VALUES"
  43.             +"(?,?)";
  44.         
  45.         try{
  46.             conn = getDBConnection();
  47.             prep = conn.prepareStatement(sql);
  48.             prep.setString(1, "Kity");
  49.             prep.setString(2, "woshikity");
  50.             
  51.             System.out.println(sql);
  52.             //Executes the SQL statement in this PreparedStatement object
  53.             prep.executeUpdate();//preparedStatement.executeUpdate(sql); error
  54.     
  55.             System.out.println("A Record has been inserted!");
  56.         }catch(SQLException e){
  57.             System.out.println(e.getMessage());
  58.         }finally{
  59.             if(prep!=null){
  60.                 prep.close();
  61.             }
  62.             if(conn!=null){
  63.                 conn.close();
  64.             }
  65.         }
  66.     }
  67.     private static void createTable() throws SQLException{
  68.         Connection conn = null;
  69.         PreparedStatement prep = null;
  70.         
  71.         String sql = "CREATE TABLE h_user("
  72.             +"id int auto_increment primary key,"
  73.             +"username varchar(50) NOT NULL,"
  74.             +"password varchar(50) NOT NULL"
  75.             +")";
  76.         
  77.         try{
  78.             conn = getDBConnection();
  79.             prep = conn.prepareStatement(sql);
  80.             System.out.println(sql);
  81.             prep.execute();
  82.             System.out.println("Table \"h_user\" is created!");
  83.         }catch(SQLException e){
  84.             System.out.println(e.getMessage());
  85.         }finally{
  86.             if(prep!=null){
  87.                 prep.close();
  88.             }
  89.             if(conn!=null){
  90.                 conn.close();
  91.             }
  92.         }
  93.         
  94.     }
  95.     
  96.     private static void selectRecords() throws SQLException{
  97.         Connection conn = null;
  98.         PreparedStatement prep = null;
  99.         
  100.         String sql = "SELECT id,username,password FROM h_user where id > ?";
  101.         try{
  102.             conn = getDBConnection();
  103.             prep = conn.prepareStatement(sql);
  104.             prep.setInt(1, 3);
  105.             System.out.println(sql);
  106.             
  107.             //execute select SQL statement
  108.             
  109.             ResultSet rs = prep.executeQuery();
  110.             while(rs.next()){
  111.                 int id = rs.getInt("id");
  112.                 String username = rs.getString("username");
  113.              System.out.println("id: "+ id);
  114.              System.out.println("username: "+ username);
  115.             }
  116.         }catch(SQLException e){
  117.             System.out.println(e.getMessage());
  118.         }finally{
  119.             if(prep!=null){
  120.                 prep.close();
  121.             }
  122.             if(conn!=null){
  123.                 conn.close();
  124.             }
  125.         }
  126.     }
  127.     private static void deleteRecords() throws SQLException{
  128.         Connection conn = null;
  129.         PreparedStatement prep = null;
  130.         
  131.         String sql = "DELETE FROM h_user where id=?";
  132.         try{
  133.             conn = getDBConnection();
  134.             prep = conn.prepareStatement(sql);
  135.             prep.setInt(1, 3);
  136.             System.out.println(sql);
  137.             prep.executeUpdate();
  138.             
  139.         }catch(SQLException e){
  140.             System.out.println(e.getMessage());
  141.         }finally{
  142.             if(prep!=null){
  143.                 prep.close();
  144.             }
  145.             if(conn!=null){
  146.                 conn.close();
  147.             }
  148.         }
  149.     }
  150.     private static void batchInsertRecords() throws SQLException{
  151.         Connection conn = null;
  152.         PreparedStatement prep = null;
  153.         
  154.         String sql = "INSERT INTO h_user(username,password) values"
  155.             +"(?,?)";
  156.         
  157.         try{
  158.             conn = getDBConnection();
  159.             prep = conn.prepareStatement(sql);
  160.         
  161.             conn.setAutoCommit(false);
  162.             prep.setString(1, "Ella");
  163.             prep.setString(2, "elladepassword");
  164.             prep.addBatch();
  165.             
  166.             prep.setString(1, "Jerry");
  167.             prep.setString(2, "oooooooooo");
  168.             prep.addBatch();
  169.             prep.executeBatch();
  170.          conn.commit();
  171.         
  172.             System.out.println("Batch Records have been inserted!");
  173.         }catch(SQLException e){
  174.             System.out.println(e.getMessage());
  175.         }finally{
  176.             if(prep!=null){
  177.                 prep.close();
  178.             }
  179.             if(conn!=null){
  180.                 conn.close();
  181.             }
  182.         }
  183.     }
  184.     private static Connection getDBConnection(){
  185.         Connection conn = null;
  186.         try{
  187.             Class.forName(DB_DRIVER);
  188.         }catch(ClassNotFoundException e){
  189.             System.out.println(e.getMessage());
  190.         }
  191.         try{
  192.             conn = DriverManager.getConnection(DB_CONNECTION,DB_USER, DB_PASSWORD);
  193.         }catch(SQLException e){
  194.             System.out.println(e.getMessage());
  195.         }
  196.         
  197.         return conn;
  198.     }

  199. }

阅读(3192) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~