1.DBC class 负责数据库的添加查找删除!
- package mydbc.dbc;
- import java.sql.*;
- import tool.SqlConfig;
- /// 数据库连接公共类
- public class DBC {
- // 数据源
- public String url = null;
- // 创建connection对象
- public Connection conn;
- public static DBC only;
- // 创建DBC对象
- public static DBC getInstance() {
- if (only == null) {
- return new DBC();
- } else {
- return only;
- }
- }
- // 创建数据库连接
- public void getConnection() {
- try {
- Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
- getSqlUrl();
- conn = DriverManager.getConnection(url);
- } catch (SQLException ex) {
- javax.swing.JOptionPane.showMessageDialog(null, ex.getMessage()
- .toString());
- } catch (ClassNotFoundException ex) {
- javax.swing.JOptionPane.showMessageDialog(null, ex.getMessage()
- .toString());
- }
- }
- public void getSqlUrl() {
- // "jdbc:microsoft:sqlserver://Localhost:1433;DatabaseName=BOOKSDB;User=sa;Password=sa;";
- SqlConfig sc = new SqlConfig();
- sc.init("sqlConfig.properties");
- String DatabaseName = sc.props.getProperty("DatabaseName");
- // System.out.println(DatabaseName);
- String User = sc.props.getProperty("User");
- // System.out.println(User);
- String Password = sc.props.getProperty("Password");
- // System.out.println(Password);
- url = "jdbc:microsoft:sqlserver://Localhost:1433;DatabaseName="
- + DatabaseName + ";User=" + User + ";Password=" + Password
- + ";";
- // System.out.println(url);
- }
- // 数据库查询方法
- public ResultSet executeQuery(String sql) {
- ResultSet rs = null;
- try {
- getConnection();
- Statement stmt = conn.createStatement();
- rs = stmt.executeQuery(sql);
- } catch (SQLException ex) {
- javax.swing.JOptionPane.showMessageDialog(null, ex.getMessage()
- .toString());
- }
- return rs;
- }
- // 更新,插入数据并返回是否成功
- public boolean executeUpdate(String sql) {
- getConnection();
- int i = 0;
- try {
- Statement stmt = conn.createStatement();
- i = stmt.executeUpdate(sql);
- stmt.close();
- conn.close();
- } catch (SQLException ex) {
- javax.swing.JOptionPane.showMessageDialog(null, ex.getMessage()
- .toString());
- return false;
- }
- if (i > 0) {
- return true;
- } else {
- return false;
- }
- }
- }
2.SqlConfig class负责读取文件中sqlserver数据库的用户名密码所需要的数据库。
- package tool;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.Properties;
- public class SqlConfig {
- public Properties props = new Properties();
- public void init(String fileName) {
- File file = new File(fileName);
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(file);
- // System.out.println(fis);
- props.load(fis);
- } catch (FileNotFoundException fe) {
- fe.printStackTrace();
- } catch (IOException ie) {
- ie.printStackTrace();
- }
- }
- /*
- * public static void main(String[] args) { SqlConfig sc = new SqlConfig();
- * sc.init("a.properties"); String str = sc.props.getProperty("User");
- * System.out.println(str); }
- */
- }
注意sqlConfig.properties要和classes文件夹在同一目录下面。这样的好处是将你的应用程序打包成jar文件,可以在jar文件外面配置数据库信息。其实打包的jar文件就相当于classes文件夹。