说明几点:
1、我在安装SQL Server选择的是Windows身份验证,所以,在连接的时候是没有用户名和密码的
2、报错内容如下:
com.microsoft.sqlserver.jdbc.SQLServerException: 用户 '' 登录失败。该用户与可信 SQL Server 连接无关联。
3、源程序代码如下:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DriverManager;public class SQLServer2005Conn { public static void main(String[] args) {
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String connURL = "jdbc:sqlserver://localhost:1433;DatabaseName=book";
String sql = "select * from book1";
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("装载驱动出错");
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(connURL);
System.out.println("connect successfully ---1---");
st = conn.createStatement();
System.out.println("connect successfully ---2---");
rs = st.executeQuery(sql);
System.out.println("connect successfully ---3---");
} catch (Exception ex) {
System.out.println("数据库连接报错");
ex.printStackTrace();
} finally { try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
}
1、我在安装SQL Server选择的是Windows身份验证,所以,在连接的时候是没有用户名和密码的
2、报错内容如下:
com.microsoft.sqlserver.jdbc.SQLServerException: 用户 '' 登录失败。该用户与可信 SQL Server 连接无关联。
3、源程序代码如下:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DriverManager;public class SQLServer2005Conn { public static void main(String[] args) {
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String connURL = "jdbc:sqlserver://localhost:1433;DatabaseName=book";
String sql = "select * from book1";
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("装载驱动出错");
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(connURL);
System.out.println("connect successfully ---1---");
st = conn.createStatement();
System.out.println("connect successfully ---2---");
rs = st.executeQuery(sql);
System.out.println("connect successfully ---3---");
} catch (Exception ex) {
System.out.println("数据库连接报错");
ex.printStackTrace();
} finally { try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
}
final String DBURL ="jdbc:sqlserver://127.0.0.1:1433;databaseName=loginUser";
final String USER_NAME="admin";
final String USER_PWD = "lichunliang";
Connection conn = null;
PreparedStatement ps = null;
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
boolean flag = false;
String sql = "select * from person where id = ? and password =?";
try{
Class.forName(DRIVER_NAME);
conn = DriverManager.getConnection(DBURL,USER_NAME,USER_PWD);
ps = conn.prepareStatement(sql);
ps.setString(1,userName);
ps.setString(2,userPwd);
ResultSet rs = ps.executeQuery();
if(rs.next())
{
flag = true;
session.setAttribute("uname",rs.getString(2));
}
}catch(Exception e)
{
}
最近写的一个代码 你可以参考一下
我在网上看见说:要在SQL Server Configuration Manager里面把SQLEXPRESS的协议中的TCP/IP启用,然后再重启SQL Server(SQLEXPRESS),可是我那个启用了TCP/IP协议后,SQL Server就无法重启了,总是提示失败