📄 dbcon.java
字号:
package com.pet.util;
import java.sql.*;
public class DBCon {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
private static DBCon dbBasic;
//工厂方法
public static DBCon getInstance() {
if (dbBasic == null) {
dbBasic = new DBCon();
}
return dbBasic;
}
//连接数据库
private Connection getConnection() throws SQLException,
ClassNotFoundException {
//纯JDBC连接
// Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
// this.conn = DriverManager.getConnection(
// "jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=pet",
// "sa", "");
//ODBC连接
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
this.conn = DriverManager.getConnection("jdbc:odbc:pets");
return conn;
}
//查询方法
public ResultSet querySQL(String strSql) throws SQLException,
ClassNotFoundException {
if (conn == null) {
this.getConnection();
}
this.stmt = conn.createStatement();
//rs = stmt.executeQuery(strSql);
return stmt.executeQuery(strSql);
}
public int updateResult(String strSql) throws SQLException,
ClassNotFoundException {
if (conn == null) {
this.getConnection();
}
this.stmt = conn.createStatement();
return stmt.executeUpdate(strSql);
}
//关闭数据库
public void closeConn() throws SQLException {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -