代码:
package jsp_jdbc;import java.sql.*;
import java.io.*;public class sql {
public static void main(String args[])
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url =
// "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=book";
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=book";
Connection con = java.sql.DriverManager.getConnection(url, "sa", ""); Statement stmt = con.createStatement();
ResultSet rst = stmt.executeQuery("seletc * from book");
while (rst.next()) {
/*
out.println("<tr>");
out.println("<td>" + trans(rst.getString("bookId")) + "</td>");
out.println("<td>" + trans(rst.getString("bookName")) + "</td>");
out.println("</tr>");*/
String bookId = rst.getString("bookId");
String bookName = rst.getString("bookName");
System.out.println("bookId:" + bookId + "\t bookName:" + bookName);
}
rst.close();
stmt.close();
con.close(); }
catch(Exception e)
{
System.out.println(e);
}
}
}错误:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]用户 'sa' 登录失败。原因: 未与信任 SQL Server 连接相关联。为什么老是给我报这个错???????????????
package jsp_jdbc;import java.sql.*;
import java.io.*;public class sql {
public static void main(String args[])
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url =
// "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=book";
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=book";
Connection con = java.sql.DriverManager.getConnection(url, "sa", ""); Statement stmt = con.createStatement();
ResultSet rst = stmt.executeQuery("seletc * from book");
while (rst.next()) {
/*
out.println("<tr>");
out.println("<td>" + trans(rst.getString("bookId")) + "</td>");
out.println("<td>" + trans(rst.getString("bookName")) + "</td>");
out.println("</tr>");*/
String bookId = rst.getString("bookId");
String bookName = rst.getString("bookName");
System.out.println("bookId:" + bookId + "\t bookName:" + bookName);
}
rst.close();
stmt.close();
con.close(); }
catch(Exception e)
{
System.out.println(e);
}
}
}错误:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]用户 'sa' 登录失败。原因: 未与信任 SQL Server 连接相关联。为什么老是给我报这个错???????????????
通常连接数据库代码这样写:
try
{
Class.forName("sun.jdbc.odbc.Jdbc.JdbcOdbcDriver");//该语句指出连接数据库驱动,常用这种JDBC桥接ODBC方式。
Connection con;
con=Driver.getConnetion("jdbc:odbc:mydatasource","user","password");//mydatasource 处写数据源的名字,user处写登陆数据时候的用户名,password处写登陆数据库该用户的密码。
Statement stmt = con.createStatement();
ResultSet rst = stmt.executeQuery("seletc * from book");
}
catch(Exception e)
{
System.out.println(e);
}//以上是如果我写连接数据库就这么写,如果要调用的数据库不在本机,最好用JSP+JAVABEAN方式或者EJB。如果还有问题回复顶!
-------------------------------
1 驱动不对,回抛出classnotfound错误!根本不会发生连接!
2 url不对,会提示 not a valid url.更加不会去尝试登陆了!
3 应该是密码不对!这个你猜中了
但这种写法严重影响平台无关性!
com.microsoft.jdbc.sqlserver.SQLServerDriver是纯java的驱动程序!
应该是密码错了.你是不是设置了window与db都可以验证?建议catch sqlexception 取得错误代码,然后根据错误号查找数据库看详细信息
{
Class.forName("sun.jdbc.odbc.Jdbc.JdbcOdbcDriver");//该语句指出连接数据库驱动,常用这种JDBC桥接ODBC方式。
Connection con;
con=Driver.getConnetion("jdbc:odbc:mydatasource","user","password");//mydatasource 处写数据源的名字,user处写登陆数据时候的用户名,password处写登陆数据库该用户的密码。
Statement stmt = con.createStatement();
ResultSet rst = stmt.executeQuery("seletc * from book");
}
catch(Exception e)
{
System.out.println(e);
}
其实原因在你的SQL SERVER服务器,一般同志们的服务器都是以默认的windows用户登录的,而非sa用户,所以在程序里用sa用户当然是不能登录到服务器的。操作步骤:
1、启动MSSQLSERVER管理器,右击服务器名选择属性,选择“安全性”页,身份验证处选择“sql server和windows”,然后确定保存。
2、右击服务器名选择“编辑sql server注册属性”,选择“使用sql server身份验证”用sa来登录,密码默认是空的,然后确认。
一切完毕后你再试一下,如果又是这样提示,那就是SA密码有误了,呵呵,这俺可管不了。
--祝你好运!
---------------------------------------------------------------
一天四个包子,我的生活!--- --罗毅