代码:
import java.sql.*;public class jdbcTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433;integratedSecurity=true; DatabaseName=myServer"; // 连接服务器和数据库myServer
Connection dbConn = null; try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL);
System.out.println("Connection Successful!"); // 如果连接成功
// 控制台输出Connection // Successful!
} catch (Exception e) {
e.printStackTrace();
}
}}
错误信息:2010-10-15 14:00:43 com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit>
警告: Failed to load the sqljdbc_auth.dll
com.microsoft.sqlserver.jdbc.SQLServerException: 没有为集成身份验证配置驱动程序。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2338)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:1929)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:1917)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1416)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1061)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at work.jdbcTest.main(jdbcTest.java:19)
import java.sql.*;public class jdbcTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433;integratedSecurity=true; DatabaseName=myServer"; // 连接服务器和数据库myServer
Connection dbConn = null; try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL);
System.out.println("Connection Successful!"); // 如果连接成功
// 控制台输出Connection // Successful!
} catch (Exception e) {
e.printStackTrace();
}
}}
错误信息:2010-10-15 14:00:43 com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit>
警告: Failed to load the sqljdbc_auth.dll
com.microsoft.sqlserver.jdbc.SQLServerException: 没有为集成身份验证配置驱动程序。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2338)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:1929)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:1917)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1416)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1061)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at work.jdbcTest.main(jdbcTest.java:19)
你还有用户名和密码没有设置
public class DBConnection {
static String DriverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
static String url="jdbc:sqlserver://localhost:1433;DatabaseName=demo";
static String user="数据库用户名";
static String password="对应的密码可以为空";
public static Connection getCon(){
try {
Class.forName(DriverName);
Connection con = DriverManager.getConnection(url, user, password);
return con;
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
e.printStackTrace();
return null;
} catch (SQLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
return null;
}
}
public static void closeCon(){
if(getCon()!=null){
try {
getCon().close();
System.out.println("连接已关闭");
} catch (SQLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
public static void main(String[] args){
Connection con = getCon();
System.out.println(con);
closeCon();
}
}
com.microsoft.sqlserver.jdbc.SQLServerException: 通过端口 1433 连接到主机 192.168.0.137 的 TCP/IP 连接失败。错误:“connect timed out。请验证连接属性,并检查 SQL Server 的实例正在主机上运行,且在此端口接受 TCP/IP 连接,还要确保防火墙没有阻止到此端口的 TCP 连接。”。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1033)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:817)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at com.conn.Conn.<init>(Conn.java:27)
at com.dao.AccountDao.<init>(AccountDao.java:12)
at com.servlet.Register.doPost(Register.java:50)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:619)
// TODO Auto-generated method stub
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433;integratedSecurity=true;DatabaseName=FA"; // 连接服务器和数据库sample,如果使用身份认证integratedSecurity=true;可以不要,加上下面的账号密码即可
// String userName = "sa"; //默认用户名
// String userPwd = "luohai"; //密码
Connection dbConn; try {
Class.forName(driverName);
//采用身份认证,
// dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
//采用集成认证
dbConn = DriverManager.getConnection(dbURL);
System.out.println("Connection Successful!"); // 如果连接成功
// 控制台输出Connection
// Successful!
// String sql = "select * from admin";
// Statement stmt = dbConn.createStatement();
// ResultSet rs = stmt.executeQuery(sql);
// while (rs.next()) {
// System.out.println("编号:"+rs.getString(1)+" 用户名:"+rs.getString(2));
// }
} catch (Exception e) {
e.printStackTrace();
} }