JDBC应用与数据库的连接

1.DBC class 负责数据库的添加查找删除!

  1. package mydbc.dbc;
  2. import java.sql.*;
  3. import tool.SqlConfig;
  4. /// 数据库连接公共类
  5. public class DBC {
  6.  // 数据源
  7.  public String url = null;
  8.  // 创建connection对象
  9.  public Connection conn;
  10.  public static DBC only;
  11.  // 创建DBC对象
  12.  public static DBC getInstance() {
  13.   if (only == null) {
  14.    return new DBC();
  15.   } else {
  16.    return only;
  17.   }
  18.  }
  19.  // 创建数据库连接
  20.  public void getConnection() {
  21.   try {
  22.    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
  23.    getSqlUrl();
  24.    conn = DriverManager.getConnection(url);
  25.   } catch (SQLException ex) {
  26.    javax.swing.JOptionPane.showMessageDialog(null, ex.getMessage()
  27.      .toString());
  28.   } catch (ClassNotFoundException ex) {
  29.    javax.swing.JOptionPane.showMessageDialog(null, ex.getMessage()
  30.      .toString());
  31.   }
  32.  }
  33.  public void getSqlUrl() {
  34.   // "jdbc:microsoft:sqlserver://Localhost:1433;DatabaseName=BOOKSDB;User=sa;Password=sa;";
  35.   SqlConfig sc = new SqlConfig();
  36.   sc.init("sqlConfig.properties");
  37.   String DatabaseName = sc.props.getProperty("DatabaseName");
  38.   // System.out.println(DatabaseName);
  39.   String User = sc.props.getProperty("User");
  40.   // System.out.println(User);
  41.   String Password = sc.props.getProperty("Password");
  42.   // System.out.println(Password);
  43.   url = "jdbc:microsoft:sqlserver://Localhost:1433;DatabaseName="
  44.     + DatabaseName + ";User=" + User + ";Password=" + Password
  45.     + ";";
  46.   // System.out.println(url);
  47.  }
  48.  // 数据库查询方法
  49.  public ResultSet executeQuery(String sql) {
  50.   ResultSet rs = null;
  51.   try {
  52.    getConnection();
  53.    Statement stmt = conn.createStatement();
  54.    rs = stmt.executeQuery(sql);
  55.   } catch (SQLException ex) {
  56.    javax.swing.JOptionPane.showMessageDialog(null, ex.getMessage()
  57.      .toString());
  58.   }
  59.   return rs;
  60.  }
  61.  // 更新,插入数据并返回是否成功
  62.  public boolean executeUpdate(String sql) {
  63.   getConnection();
  64.   int i = 0;
  65.   try {
  66.    Statement stmt = conn.createStatement();
  67.    i = stmt.executeUpdate(sql);
  68.    stmt.close();
  69.    conn.close();
  70.   } catch (SQLException ex) {
  71.    javax.swing.JOptionPane.showMessageDialog(null, ex.getMessage()
  72.      .toString());
  73.    return false;
  74.   }
  75.   if (i > 0) {
  76.    return true;
  77.   } else {
  78.    return false;
  79.   }
  80.  }
  81. }

2.SqlConfig  class负责读取文件中sqlserver数据库的用户名密码所需要的数据库。

  1. package tool;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.util.Properties;
  7. public class SqlConfig {
  8.  public Properties props = new Properties();
  9.  public void init(String fileName) {
  10.   File file = new File(fileName);
  11.   FileInputStream fis = null;
  12.   try {
  13.    fis = new FileInputStream(file);
  14.    // System.out.println(fis);
  15.    props.load(fis);
  16.   } catch (FileNotFoundException fe) {
  17.    fe.printStackTrace();
  18.   } catch (IOException ie) {
  19.    ie.printStackTrace();
  20.   }
  21.  }
  22.  /*
  23.   * public static void main(String[] args) { SqlConfig sc = new SqlConfig();
  24.   * sc.init("a.properties"); String str = sc.props.getProperty("User");
  25.   * System.out.println(str); }
  26.   */
  27. }

注意sqlConfig.properties要和classes文件夹在同一目录下面。这样的好处是将你的应用程序打包成jar文件,可以在jar文件外面配置数据库信息。其实打包的jar文件就相当于classes文件夹。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值