📄 dbmanager.java
字号:
package com.wl.db;
import java.sql.*;
public class DBManager {
private static final String DRIVERNAME = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=shop";
private static final String UID = "sa";
private static final String PWD = "";
private Connection conn=null;
public DBManager() throws SQLException, ClassNotFoundException {
openConnection();
}
/**
* 打开连接
* @throws ClassNotFoundException
* @throws SQLException
*/
private void openConnection() throws ClassNotFoundException, SQLException {
Class.forName(DRIVERNAME);
conn=DriverManager.getConnection(URL,UID,PWD);
}
/**
* 执行查询
* @param strSql String
* @return ResultSet
* @throws SQLException
*/
public ResultSet executeQuery(String strSql) throws SQLException {
System.out.println(strSql);
Statement stat=conn.createStatement();
return stat.executeQuery(strSql);
}
/**
* 执行增删改
* @param strSql String
* @throws SQLException
*/
public void executeUpdate(String strSql) throws SQLException {
Statement stat=conn.createStatement();
stat.executeUpdate(strSql);
}
/**
* 取得 PreparedStatement对象
* @param strSql String
* @return PreparedStatement
* @throws SQLException
*/
public PreparedStatement getPreparedStatment(String strSql) throws
SQLException {
return conn.prepareStatement(strSql);
}
/**
* 执行存储过程
* @param proc String
* @return CallableStatement
* @throws SQLException
*/
public CallableStatement getCallableStatement(String proc) throws
SQLException {
return conn.prepareCall(proc);
}
/**
* 关闭连接
* @throws SQLException
*/
public void closeConnection() throws SQLException {
if(conn!=null){
if(!conn.isClosed()){
conn.close();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -