/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package i2client; import java.io.BufferedReader; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; /** * * @author LENOVO */ public class DBHelper { private static String DRIVER=""; //private static final String URL="jdbc:mysql://192.168.1.188/dss"; private static String URL=""; private static String USER=""; private static String PASSWORD=""; public void GetText(String path){ if(DRIVER!=""){ return; } // 1.定义目标文件 File srcFile = new File(path); // 2.创建一个流,指向目标文件 BufferedReader is = null; try { is = new BufferedReader(new FileReader(srcFile)); int i= 0; String s = null; while ((s = is.readLine()) != null) {// 使用readLine方法,一次读一行 if(i==0){ DRIVER = s; } if(i==1){ URL = s; } if(i==2){ USER = s; } if(i==3){ PASSWORD = s; } i++; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭io流 try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 连接数据库 * @return 链接数据库对象 */ public Connection getConnection(){ GetText("/home/shjdconfig.txt"); //GetText("E:\\shjd\\shjdconfig.txt"); Connection conn=null; try { Class.forName(DRIVER); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { conn=DriverManager.getConnection(URL, USER, PASSWORD); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } /** * 释放相应的资源 * @param rs * @param pstmt * @param conn */ public void closeAll(ResultSet rs,PreparedStatement pstmt,Connection conn){ try { if(rs!=null){ rs.close(); } if(pstmt!=null){ pstmt.close(); } if(conn!=null){ conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 此方法可以完成增删改所有的操作 * @param sql * @return true or false */ public int excuteUpdate(String sql){ int res=0;//受影响的行数 Connection conn=null; PreparedStatement pstmt = null; ResultSet rs=null; try { conn=getConnection(); pstmt=conn.prepareStatement(sql);//装载sql语句 res=pstmt.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeAll(rs, pstmt, conn); } return res; } /** * 使用泛型方法和反射机制进行封装 * @param sql * @param params * @param cls * @return */ public List executeQuery(String sql,List params,Class cls) throws Exception{ Connection conn=null; PreparedStatement pstmt = null; ResultSet rs=null; List data=new ArrayList(); try { conn=getConnection(); pstmt=conn.prepareStatement(sql);//装载sql语句 if(params!=null){ //加入有?占位符,在执行之前把?占位符替换掉 for(int i=0;i