淺談通過使用JDBC的statement進(jìn)行數(shù)據(jù)操作
作者:nbtlxx
本文將介紹如何通過使用JDBC的statement進(jìn)行數(shù)據(jù)操作,Statement生成的自動生成鍵是否可用于獲取。希望本文對大家有所幫助。
使用JDBC的statement進(jìn)行數(shù)據(jù)的查詢,基本步驟如下:
1. 初始化simpleDbSource對象
2. 獲得getconnection
3. createStatement 獲得查詢語句
4. executeUpdate, 執(zhí)行更新語句
5. 關(guān)閉使用的statement, connection, 注意次序不要弄錯
注意:更新語句,執(zhí)行過一次后,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, 執(zhí)行更新語句
- * 5. 關(guān)閉使用的statement, connection, 注意次序不要弄錯
- *
- * 注意:更新語句,執(zhí)行過一次后,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();
- //數(shù)據(jù)庫更新工作,包括create, drop, update, insert etc.
- stmt.executeUpdate(insertSql);
- System.out.println("執(zhí)行成功"+ insertSql);
- //進(jìn)行數(shù)據(jù)庫查詢
- ResultSet rs = stmt.executeQuery(querySql);
- //進(jìn)行遍歷
- 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();
- }
- //依次關(guān)閉statement和conn數(shù)據(jù)庫連接對象,清空資源
- 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, 執(zhí)行更新語句
- * 5. 關(guān)閉使用的statement, connection, 注意次序不要弄錯
- *
- * 注意:更新語句,執(zhí)行過一次后,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();
- //數(shù)據(jù)庫更新工作,包括create, drop, update, insert etc.
- stmt.executeUpdate(insertSql);
- System.out.println("執(zhí)行成功"+ insertSql);
- //進(jìn)行數(shù)據(jù)庫查詢
- ResultSet rs = stmt.executeQuery(querySql);
- //進(jìn)行遍歷
- 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();
- }
- //依次關(guān)閉statement和conn數(shù)據(jù)庫連接對象,清空資源
- 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. 設(shè)置具體更新內(nèi)容,setInt(colIndex, value), setString(colIndex,value)
- * 4. executeUpdate, 執(zhí)行更新語句
- * 5. 關(guān)閉使用的PreparedStatementstatement, connection, 注意次序不要弄錯
- *
- * 注意:更新語句,執(zhí)行過一次后,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");
- //數(shù)據(jù)庫更新工作,包括create, drop, update, insert etc.
- pstmt.executeUpdate();
- //清空設(shè)置的參數(shù),為后續(xù)更新準(zhǔn)備
- pstmt.clearParameters();
- System.out.println("執(zhí)行成功"+ pstmtSql);
- //進(jìn)行數(shù)據(jù)庫查詢
- Connection conn1 = dbSource.getConnect();
- Statement stmt = conn1.createStatement();
- ResultSet rs = stmt.executeQuery(querySql);
- //進(jìn)行遍歷
- 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();
- }
- //依次關(guān)閉jdbc的statement和conn數(shù)據(jù)庫連接對象,清空資源
- 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;
- }
- }
- }
- }
【編輯推薦】
責(zé)任編輯:彭凡
來源:
javaeye