JDBC连接SQL Sever 2005

0x826 2007-04-03 04:20:07
从微软那下了个Microsoft SQL Server 2005 JDBC Driver 1.1

解压后修改classpath : ;C:\Program Files\Microsoft SQL Server 2005 JDBC Driver\sqljdbc_1.1\chs\sqljdbc.jar

找了个程序测试

import java.sql.*;
class Testj{
public static void main(String args[])
{
String RL = "jdbc:sqlserver://localhost:1026;DatabaseName=Java";//我的端口是1026
String user ="long\\Long";
String password = "";
String sqlStr = "select * from T";
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println( "类实例化成功!" );
System.out.println("slkdjf");
Connection con = DriverManager.getConnection(RL,user,password);
System.out.println( "创建连接对像成功!" );
Statement st = con.createStatement();
System.out.println( "创建Statement成功!" );
ResultSet rs = st.executeQuery( sqlStr );
System.out.println( "操作数据表成功!" );
System.out.println( "----------------!" );
while(rs.next())
{
System.out.print(rs.getInt("Sno") + " ");
System.out.print(rs.getString("Sname") + " ");
System.out.print(rs.getInt("Sage") + " ");
System.out.print(rs.getString("Ssex") + " ");
System.out.println(rs.getString("Sclass"));
}
rs.close();
st.close();
con.close();
}
catch(Exception err){
err.printStackTrace(System.out);
}
}
}

可结果是:

类实例化成功!
slkdjf
com.microsoft.sqlserver.jdbc.SQLServerException: 用户 'long\Long' 登录失败。该用户与可信 SQL Server 连接无关联。

请问是什么错误呢?
...全文
462 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaodaozy 2007-04-05
  • 打赏
  • 举报
回复
o sorry the problem is don not that!
xiaodaozy 2007-04-05
  • 打赏
  • 举报
回复
第五行是:
String URL 不是 RL
happy_hibernate 2007-04-05
  • 打赏
  • 举报
回复
package util;
import java.sql.*;
import javax.sql.*;
import java.util.*;
import java.io.*;

public class Jdbcutil {
private static Properties props=new Properties();
static{
InputStream in=null;
try {
in=new FileInputStream("db.properties");
props.load(in);//load方法
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
in.close();
}

}
public static Connection getConnection(){
/*String[] driver=new String[2];
String url="jdbc:oracle:thin:@192.168.0.100:1521:tarena";
//String url="jdbc:mysql://127.0.0.1:3306/test";
String user="sd0607";
String pwd="sd0607";
//String user="root";
//String pwd="root";
driver[0]="oracle.jdbc.driver.OracleDriver";
driver[1]="com.mysql.jdbc.Driver";*/

Connection con=null;

try {
Class.forName(props.getProperty("driver"));
/*for(int i=0;i<driver.length;i++){
Class.forName(driver[i]);
}
con=DriverManager.getConnection(url,user,pwd); */
con=DriverManager.getConnection(props.getProperty("url"),props.getProperty("user"),props.getProperty("pwd"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
public static void close(Object o){
try {
if(o instanceof ResultSet){
((ResultSet)o).close();
}else if(o instanceof Statement){
((Statement)o).close();
}else if(o instanceof Connection){
((Connection)o).close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}

}



package lab;
import util.Jdbcutil;
import java.sql.*;
import javax.sql.*;

public class Test {

public static void main(String[] args) {
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try {
con = Jdbcutil.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery("select username,id from t_user");
//rs = stmt.executeQuery("select * from q_test");
while(rs.next()){
System.out.println("name:"+rs.getString(1)+rs.getInt(2));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
Jdbcutil.close(rs);
Jdbcutil.close(stmt);
Jdbcutil.close(con);
}

}

}
violet219 2007-04-04
  • 打赏
  • 举报
回复
我觉得最好是在SQL Server 2005里重新设置一下安全、登录属性比较好。记得当初我也是在网上找的这个一模一样的程序尝试与SQL Server 2000连接,成功了。
xiaodaozy 2007-04-04
  • 打赏
  • 举报
回复
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";//
0x826 2007-04-03
  • 打赏
  • 举报
回复
我要连接本地服务器 用户名:long\Long 因为是转义字符 所以 String user ="long\\Long";
bearold 2007-04-03
  • 打赏
  • 举报
回复
用户名long\\Long?不是合法字符吧?!
likgui 2007-04-03
  • 打赏
  • 举报
回复
顶!
javabill23123 2007-04-03
  • 打赏
  • 举报
回复
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";//localhost:表示本地,你也可以填写IP地址,连接其他服务器, DatabaseName:表示数据库的名字
String user=""; //数据库的用户名
String password=""; //用户名对应的密码

你要知道你建的或者别人的数据库的名字,还有添入响应的用户名和密码

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧