发布网友 发布时间:2022-04-20 13:53
共1个回答
热心网友 时间:2023-07-15 05:03
package com.icss.carmall.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
/**
* 获得数据库连接
* @return
*/
public static Connection getConn(){
Connection conn = null;
try {
// 加载驱动
Class.forName("net.sourceforge.jtds.jdbc.Driver");
final String url = "jdbc:jtds:sqlserver://127.0.0.1:1433/CarDB";
final String user = "sa";
final String password ="123456";
// 获得数据库连接
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/**
* 关闭数据连接
* @param conn
* @param pstmt
* @param rs
*/
public static void closeConn(Connection conn, PreparedStatement pstmt, ResultSet rs){
if(null != conn){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null != pstmt){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null != rs){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
DBUtil.getConn();
}
}