展开全部
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();
}
}
追问
这是有用户名和密码的,不是windows身份验证啊
追答
windows身份验证只能通过sqlServer客户端才可以
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询