学生宿舍管理系统-数据库DBUtil.java
作者:互联网
package dataBase;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//建立数据库
public class DBUtil {
//创建连接并初始化
static public Connection conn = null;
static public Statement st = null;
static public ResultSet rs = null;
//获取链接并捕捉异常
public Connection getConn()throws SQLException, ClassNotFoundException{
try {
//加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//创建连接
conn = DriverManager.getConnection(
"jdbc:sqlserver://127.0.0.1:1433;DatabaseName=dormitory", "sa", "xu1714799060");
}catch (Exception e) {
}
return conn;
}
//关闭连接
public void close(){
try{
if(!conn.isClosed()){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
标签:学生宿舍,java,static,sql,import,DBUtil,public,conn 来源: https://blog.csdn.net/weixin_44522477/article/details/108954881