SqlHelper.java
package com.mqs.process; import java.io.FileInputStream; import java.sql.*; import java.util.Properties; public class SqlHelper { //定義需要的變量 private static Connection ct=null; //在大多數情況下,使用PreparedStatement來替代Statement //這樣可以防止sql注入 private static PreparedStatement ps=null; private static ResultSet rs=null; private static CallableStatement cs=null; public static Connection getCt() { return ct; } public static PreparedStatement getPs() { return ps; } public static ResultSet getRs() { return rs; } //連接數據庫參數 private static String url=""; private static String username=""; private static String driver=""; private static String password=""; private static Properties pp=null; private static FileInputStream fis=null; //加載驅動,只需要一次 static{ try { //從dbinfo.propertis文件中讀取配置信息 pp = new Properties(); fis=new FileInputStream("dbinfo.properties"); pp.load(fis); url=pp.getProperty("url"); username=pp.getProperty("username"); driver=pp.getProperty("driver"); password=pp.getProperty("password"); Class.forName("driver"); } catch (Exception e) { // TODO: handle exception }finally{ try { fis.close(); } catch (Exception e) { e.printStackTrace(); } fis=null; } } //得到連接 public static Connection getConnection(){ try { ct = DriverManager.getConnection(url,username,password); } catch (Exception e) { // TODO: handle exception } return ct; } //統一的select public static ResultSet executeQuery(String sql,String parameters[]){ try { ct=getConnection(); ps=ct.prepareStatement(sql); if(parameters!=null&&!parameters.equals("")){ for(int i=0;i<parameters.length;i++){ ps.setString(i+1,parameters[i]); } } rs=ps.executeQuery(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); }finally{ //close(rs,ps,ct); } return rs; } //先寫一個update/insert/delete public static void executeUpdate(String sql,String [] parameters){ //1、創建一個ps try{ ct=getConnection(); ps=ct.prepareStatement(sql); //給?賦值 if(parameters!=null){ for(int i=0;i<parameters.length;i++){ ps.setString(i+1,parameters[i]); } } //執行 ps.executeUpdate(); }catch (Exception e){ e.printStackTrace();//開發階段 //拋出異常,拋出運行異常,可以給調用該函數的函數一個選擇 //可以處理,也可以放棄處理 throw new RuntimeException(e.getMessage()); }finally{ //關閉資源 close(rs,ps,ct); } } //如果有多個update/delete/insert[需要考慮事務] public static void executeUpdate2(String sql[],String [][]parameters){ try { //1、獲得鏈接 ct=getConnection(); //因為這時,用戶傳入的可能是多個sql語句 ct.setAutoCommit(false); for(int i=0;i<sql.length;i++){ if(parameters[i]!=null){ ps=ct.prepareStatement(sql[i]); for(int j=0;j<parameters[i].length;j++){ ps.setString(j+1, parameters[i][j]); } ps.executeUpdate(); } } ct.commit(); } catch (Exception e) { e.printStackTrace();//開發階段 //回滾 try { ct.rollback(); } catch (Exception e1) { e1.printStackTrace(); } //拋出異常,拋出運行異常,可以給調用該函數的函數一個選擇 //可以處理,也可以放棄處理 throw new RuntimeException(e.getMessage()); }finally{ close(rs,ps,ct); } } //分頁問題 public static ResultSet executeQuery2(){ return null; } //調用存儲過程 //sql 像{call 過程(?,?,?)} public static void callPro1(String sql,String []parameters){ try { ct=getConnection(); cs=ct.prepareCall(sql); //?號賦值 if(parameters!=null){ for(int i=0;i<parameters.length;i++){ cs.setObject(i+1, parameters[i]); } } cs.execute(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); }finally{ close(rs,cs,ct); } } //調用存儲過程,有返回Result //未完成,見《韓順平.2011最新j2ee視頻教程.jdbc第4講.超強SqlHelper工具類封裝(2)》 public static ResultSet callPro2(String sql,String []inparameters,String []outparameters){ try { ct=getConnection(); cs=ct.prepareCall(sql); if(inparameters!=null){ for(int i=0;i<inparameters.length;i++){ cs.setObject(i+1,inparameters[i]); } } } catch (Exception e) { // TODO: handle exception }finally{ } return null; } //關閉資源的函數 public static void close(ResultSet rs,Statement ps,Connection ct){ if(rs!=null){ try { rs.close(); } catch (Exception e) { e.printStackTrace(); } rs=null; } if(ps!=null){ try { ps.close(); } catch (Exception e) { e.printStackTrace(); } ps=null; } if(ct!=null){ try { ct.close(); } catch (Exception e) { e.printStackTrace(); } ct=null; } } }
DbTest.java
package com.mqs.process; import java.sql.*; import java.util.ArrayList; public class DbTest { public static void main(String[] args) { Connection ct =null; PreparedStatement ps=null; ResultSet rs=null; try { //1、獲取數據庫鏈接 ct=SqlHelper.getConnection(); //2、創建獲取rs String sql = "select * from news"; String parameters[]={}; rs=SqlHelper.executeQuery(sql,parameters); while(rs.next()){ System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)); } } catch (Exception e) { e.printStackTrace(); }finally{ if(rs!=null){ try { rs.close(); } catch (Exception e) { e.printStackTrace(); } rs=null; } if(ps!=null){ try { ps.close(); } catch (Exception e) { e.printStackTrace(); } ps=null; } if(ct!=null){ try { ct.close(); } catch (Exception e) { e.printStackTrace(); } ct=null; } } } }
dbinfo.properties
url=jdbc:mysql://127.0.0.1:3306/site?useUnicode=true&characterEncoding=UTF-8 username=root driver=com.mysql.jdbc.Driver password=654321
直接操作數據庫的方法
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; Connection conn = null; ResultSet rs = null; String sql = "select * from news"; String url = "jdbc:mysql://127.0.0.1:3306/site?user=root&password=654321&useUnicode=true&characterEncoding=UTF-8"; ArrayList<String[]> news = new ArrayList<String[]>(); try{ Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url); java.sql.Statement stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while(rs.next()){ String[] temp = {rs.getString(1),rs.getString(2),rs.getString(3)}; news.add(temp); } }catch (SQLException e) { System.out.println("MySQL操作錯誤"); e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } }
自己寫的封裝
package com.yourcompany.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DbHelper { private static Connection conn=null; private static PreparedStatement ps=null; private static ResultSet rs = null; private static String url = "jdbc:mysql://127.0.0.1:3306/site?user=root&password=654321&useUnicode=true&characterEncoding=UTF-8"; //private static String url = "jdbc:mysql://127.0.0.1:3306/site"; //獲取查詢數據 public static ResultSet getQuery(String sql,String parameters[]){ try{ Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url); ps = conn.prepareStatement(sql); if(parameters!=null&&!parameters.equals("")){ for(int i=0;i<parameters.length;i++){ ps.setString(i+1,parameters[i]); } } rs = ps.executeQuery(); }catch (SQLException e) { System.out.println("MySQL操作錯誤"); e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { } return rs; } //關閉資源的函數 public static void close(){ if(rs!=null){ try { rs.close(); } catch (Exception e) { e.printStackTrace(); } rs=null; } if(ps!=null){ try { ps.close(); } catch (Exception e) { e.printStackTrace(); } ps=null; } if(conn!=null){ try { conn.close(); } catch (Exception e) { e.printStackTrace(); } conn=null; } } }