2 qq 16773675 qq_16773675 于 2014.12.02 22:29 提问

这个问题该怎么解决,大神帮我看看

import java.sql.*;

public class Test {

public static void main(String[] args) {
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=JSPTest","sa","123");
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

// con = DriverManager.getConnection("jdbc:odbc:JDBCSQLDemo_JSPTest");
stmt = con.createStatement();
rs = stmt.executeQuery("select userName,passWord,email from regist");
while(rs.next()){
String strUserName = rs.getString(1);
String strPassWord = rs.getString(2);
String strEmail = rs.getString("email");
System.out.println(strUserName+"\t"+strPassWord+"\t"+strEmail+"\t");
}
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
System.out.println("Game Over~");
}
}

}

import java.sql.*;

public class Test {

public static void main(String[] args) {
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=JSPTest","sa","123");
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

// con = DriverManager.getConnection("jdbc:odbc:JDBCSQLDemo_JSPTest");
stmt = con.createStatement();
rs = stmt.executeQuery("select userName,passWord,email from regist");
while(rs.next()){
String strUserName = rs.getString(1);
String strPassWord = rs.getString(2);
String strEmail = rs.getString("email");
System.out.println(strUserName+"\t"+strPassWord+"\t"+strEmail+"\t");
}
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
System.out.println("Game Over~");
}
}

}

2个回答

u014228527
u014228527   2014.12.02 23:50

把错误日志贴出来,不然肿么分析你错误的地方。。。

caozhy
caozhy   Ds   Rxr 2014.12.03 02:52

目测你的SQL Server配置有问题。你有没有启用tcp/ip协议,sql远程访问,防火墙打开1433端口,以及你的sa密码是否是123。

Csdn user default icon
上传中...
上传图片
插入图片