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


    //通过配置文件不用每次换一种数据库就要修改工具类,只要在配置文件中修改就可以了 //加载配置文件的方法 //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;
    }
     
     
     
    }
     

  • 相关阅读:
    Python 用 matplotlib 中的 plot 画图
    python--serial串口通信
    verilog,vhdl,bdf文件一起综合
    项目小笔记2--qt designer 修改字体,部件拖入layout,引用time模块延时,正则表达式判断输入,进制转换,部件固定大小,graphics view显示图片,消息提示框使用
    虚拟环境下通过pyinstaller 打包
    FPGA--IIC通信
    FPGA--SPI通信
    verilog 语法
    【C_Language】---队列和栈的C程序实现
    【C_Language】---C文件学习
  • 原文地址:https://www.cnblogs.com/xpng/p/3538286.html
Copyright © 2020-2023  润新知