温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何通过使用JDBC的statement进行数据操作

发布时间:2021-11-03 13:53:18 来源:亿速云 阅读:115 作者:小新 栏目:编程语言

小编给大家分享一下如何通过使用JDBC的statement进行数据操作,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

使用JDBC的statement进行数据的查询,基本步骤如下:

1. 初始化simpleDbSource对象

2. 获得getconnection

3. createStatement 获得查询语句

4. executeUpdate, 执行更新语句

5. 关闭使用的statement, connection, 注意次序不要弄错

注意:更新语句,执行过一次后,column需要递增,否则报错

Java代码

/**     *      */    package db;         import java.io.FileNotFoundException;     import java.io.IOException;     import java.sql.Connection;     import java.sql.ResultSet;     import java.sql.SQLException;         /**     * @author sean     *      * 1. 初始化simpleDbSource对象     * 2. 获得getconnection     * 3. createStatement 获得查询语句     * 4. executeUpdate, 执行更新语句     * 5. 关闭使用的statement, connection, 注意次序不要弄错     *      * 注意:更新语句,执行过一次后,column需要递增,否则报错     */    public class StatementDemo {             private static String insertSql="insert into user values('7','sean','sean@mail.com','hellofromsean')";         private static String querySql ="select * from user";             /**         * @param args         */        public static void main(String[] args) {             // TODO Auto-generated method stub             DBSource dbSource;             Connection conn = null;             java.sql.Statement stmt = null;                          try {                 dbSource = new SimpleDBSource();                 conn = dbSource.getConnect();                 stmt = conn.createStatement();                                  //数据库更新工作,包括create, drop, update, insert etc.                 stmt.executeUpdate(insertSql);                 System.out.println("执行成功"+ insertSql);                                  //进行数据库查询                 ResultSet rs = stmt.executeQuery(querySql);                                  //进行遍历                 while(rs.next()){                     System.out.println(rs.getInt(1)+ "\t");                     System.out.println(rs.getString(2)+ "\t");                     System.out.println(rs.getString(3)+ "\t");                     System.out.println(rs.getString(4)+ "\t");                     System.out.println("**********************");                 }                                                                } catch (FileNotFoundException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             } catch (IOException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             } catch (ClassNotFoundException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             } catch (SQLException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             }                          //依次关闭statement和conn数据库连接对象,清空资源             finally{                 if(stmt!= null){                     try {                         stmt.close();                     } catch (SQLException e) {                         // TODO Auto-generated catch block                         e.printStackTrace();                     }                     stmt= null;                 }                 if(conn!=null){                     try {                         conn.close();                     } catch (SQLException e) {                         // TODO Auto-generated catch block                         e.printStackTrace();                     }                     conn= null;                 }             }         }     }     /**   *    */  package db;   import java.io.FileNotFoundException;  import java.io.IOException;  import java.sql.Connection;  import java.sql.ResultSet;  import java.sql.SQLException;   /**   * @author sean   *    * 1. 初始化simpleDbSource对象   * 2. 获得getconnection   * 3. createStatement 获得查询语句   * 4. executeUpdate, 执行更新语句   * 5. 关闭使用的statement, connection, 注意次序不要弄错   *    * 注意:更新语句,执行过一次后,column需要递增,否则报错   */  public class StatementDemo {    private static String insertSql="insert into user values('7','sean','sean@mail.com','hellofromsean')";   private static String querySql ="select * from user";    /**    * @param args    */   public static void main(String[] args) {    // TODO Auto-generated method stub    DBSource dbSource;    Connection conn = null;    java.sql.Statement stmt = null;        try {     dbSource = new SimpleDBSource();     conn = dbSource.getConnect();     stmt = conn.createStatement();          //数据库更新工作,包括create, drop, update, insert etc.     stmt.executeUpdate(insertSql);     System.out.println("执行成功"+ insertSql);          //进行数据库查询     ResultSet rs = stmt.executeQuery(querySql);          //进行遍历     while(rs.next()){      System.out.println(rs.getInt(1)+ "\t");      System.out.println(rs.getString(2)+ "\t");      System.out.println(rs.getString(3)+ "\t");      System.out.println(rs.getString(4)+ "\t");      System.out.println("**********************");     }                   } catch (FileNotFoundException e) {     // TODO Auto-generated catch block     e.printStackTrace();    } catch (IOException e) {     // TODO Auto-generated catch block     e.printStackTrace();    } catch (ClassNotFoundException e) {     // TODO Auto-generated catch block     e.printStackTrace();    } catch (SQLException e) {     // TODO Auto-generated catch block     e.printStackTrace();    }        //依次关闭statement和conn数据库连接对象,清空资源    finally{     if(stmt!= null){      try {       stmt.close();      } catch (SQLException e) {       // TODO Auto-generated catch block       e.printStackTrace();      }      stmt= null;     }     if(conn!=null){      try {       conn.close();      } catch (SQLException e) {       // TODO Auto-generated catch block       e.printStackTrace();      }      conn= null;     }    }   }  }    /**   *   */   package db;    import java.io.FileNotFoundException;   import java.io.IOException;   import java.sql.Connection;   import java.sql.ResultSet;   import java.sql.SQLException;   import java.sql.Statement;    /**   * @author sean   *   * 1. 初始化simpleDbSource对象   * 2. 获得getconnection   * 3. createPreparedStatement 获得查询语句   * 4. 设置具体更新内容,setInt(colIndex, value), setString(colIndex,value)   * 4. executeUpdate, 执行更新语句   * 5. 关闭使用的PreparedStatementstatement, connection, 注意次序不要弄错   *   * 注意:更新语句,执行过一次后,column需要递增,否则报错   */   public class PreparedStatementDemo {    private static String querySql ="select * from user";   private static String pstmtSql = "insert into user values(?,?,?,?)";    Connection conn1;   static Statement stmt;   /**   * @param args   */   public static void main(String[] args) {   // TODO Auto-generated method stub   DBSource dbSource;   Connection conn = null;   java.sql.PreparedStatement pstmt = null;    try {   dbSource = new SimpleDBSource();   conn = dbSource.getConnect();   pstmt = conn.prepareStatement(pstmtSql);    pstmt.setInt(1, 9);   pstmt.setString(2, "sean");   pstmt.setString(3, "my@hotmail.com");   pstmt.setString(4, "add some comments");    //数据库更新工作,包括create, drop, update, insert etc.   pstmt.executeUpdate();    //清空设置的参数,为后续更新准备   pstmt.clearParameters();    System.out.println("执行成功"+ pstmtSql);    //进行数据库查询   Connection conn1 = dbSource.getConnect();   Statement stmt = conn1.createStatement();   ResultSet rs = stmt.executeQuery(querySql);    //进行遍历   while(rs.next()){   System.out.println(rs.getInt(1)+ "\t");   System.out.println(rs.getString(2)+ "\t");   System.out.println(rs.getString(3)+ "\t");   System.out.println(rs.getString(4)+ "\t");   System.out.println("**********************");   }   } catch (FileNotFoundException e) {   // TODO Auto-generated catch block   e.printStackTrace();   } catch (IOException e) {   // TODO Auto-generated catch block   e.printStackTrace();   } catch (ClassNotFoundException e) {   // TODO Auto-generated catch block   e.printStackTrace();   } catch (SQLException e) {   // TODO Auto-generated catch block   e.printStackTrace();   }    //依次关闭jdbc的statement和conn数据库连接对象,清空资源   finally{   if(stmt!= null){   try {   stmt.close();   } catch (SQLException e) {   // TODO Auto-generated catch block   e.printStackTrace();   }   stmt= null;   }    if(pstmt!= null){   try {   pstmt.close();   } catch (SQLException e) {   // TODO Auto-generated catch block   e.printStackTrace();   }   pstmt= null;   }    if(conn!=null){   try {   conn.close();   } catch (SQLException e) {   // TODO Auto-generated catch block   e.printStackTrace();   }   conn= null;   }   }   }   }

以上是“如何通过使用JDBC的statement进行数据操作”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI