• 使用配置文件的链接数据库工具类


    //通过配置文件不用每次换一种数据库就要修改工具类,只要在配置文件中修改就可以了 //加载配置文件的方法 //Properties  properties = new Properties(); //properties.loder(this.getClass().getResourseAsStream("配置文件名称"));  //获得配置文件的值  such as: // dbDriver = properties.getProperty("dbDriver"); //  配置文件 JDBC.properties  dbDriver=com.microsoft.sqlserver.jdbc.SQLServerDriver
    dbURL=jdbc:sqlserver://localhost:1433;DatabaseName=Book
    dbUser=sa
    dbPassword=123
    工具类
    package Util;
     
    import java.awt.List;
    import java.beans.PropertyChangeEvent;
    import java.io.IOException;
    import java.net.ConnectException;
    import java.security.interfaces.RSAKey;
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.Properties;
     
     
    public class DateOperate {
     
    /*
    * 进行数据库链接
    * 返回数据库链接对象
    */
    public DateOperate() {
     
    public Connection getConnection() {
    String dbDriver=null;
    String dbURL=null;
    String dbUser=null;
    String dbPassword=null;
    Properties properties = new Properties();
    try {
    properties.load(this.getClass().getResourceAsStream("JDBC.properties"));
    dbDriver=properties.getProperty("dbDriver");
    dbURL=properties.getProperty("dbURL");
    dbUser= properties.getProperty("dbUser");
    dbPassword = properties.getProperty("dbPassword");
    } catch (IOException e1) {
     
    e1.printStackTrace();
    }
     
    Connection connection=null;
    try {
     
    Class.forName(dbDriver);
    connection=DriverManager.getConnection(dbURL,dbUser,dbPassword);
    System.out.print("链接成功");
    } catch (Exception e) {
    e.printStackTrace();
    }
    return connection;
    }
     
    /*
    * 释放所有资源
    * @param Connection
    * @param PreparedStatement
    * @param ResultSet
    */
    public  void closeAll(Connection connection,PreparedStatement ps,ResultSet rs){
    try {
    rs.close();
    ps.close();
    connection.close();
    } catch (SQLException e) {
     
    e.printStackTrace();
    }
    }
     
    /*
    * 执行SQl语句,执行增,删,改
    * @param PrepredSQl
    * @param param[]
    * return 受影响的的条数
    */
    public int executeSQL(String preparedSQL,Object[] param ){
    int num=0;
    Connection connection=null;
    PreparedStatement ps = null;
    connection =getConnection();
    try {
    ps=connection.prepareStatement(preparedSQL);
    if(param!=null){
    for(int i=0;i<param.length;i++){
    ps.setObject(i+1, param[i]);
    }
    }
    num = ps.executeUpdate();
    } catch (SQLException e) {
     
    e.printStackTrace();
    }
    return num;
    }
     
     
    /*
    * 执行sql语句,执行查询功能
    * @param PrepareSQl
    * @param param[]
    * 返回Result对象
    */
    public ResultSet executeQuerySQL(String prepareSQL,Object[] param){
    Connection connection=null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    connection = getConnection();
    try {
    ps = connection.prepareStatement(prepareSQL);
    if(param!=null){
    for(int i=0;i<param.length;i++){
    ps.setObject(i+1, param[i]);
    }
    }
    rs= ps.executeQuery();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
     
    return rs;
    }
     
     
     
    }
     

  • 相关阅读:
    poj 3422 Kaka's Matrix Travels
    poj 1815 Friendship
    poj 1966 Cable TV Network
    黑暗
    【bzoj2741】[FOTILE模拟赛] L
    整数拆分
    LCIS
    原题的旅行
    【codeforces gym】Increasing Costs
    【noip模拟】D(==)
  • 原文地址:https://www.cnblogs.com/xpng/p/3538286.html
Copyright © 2020-2023  润新知