SQL与java问题
编译java程序;mportjava.sql.*;publicclassTestSQLServer{publicstaticvoidmain(String[]args){...
编译java程序;
mport java.sql.*;
public class TestSQLServer {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://127.0.0.1:1433", "yht", "yht");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from mytable1");
while (rs.next()) {
System.out.print("sid:" + rs.getInt(1));
System.out.print("\tname:" + rs.getString(2).trim());
System.out.print("\tsex:" + rs.getString(3).trim());
System.out.println();
}
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
出现错误:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at TestSQLServer.main(TestSQLServer.java:7)
是什么原因啊? 展开
mport java.sql.*;
public class TestSQLServer {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://127.0.0.1:1433", "yht", "yht");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from mytable1");
while (rs.next()) {
System.out.print("sid:" + rs.getInt(1));
System.out.print("\tname:" + rs.getString(2).trim());
System.out.print("\tsex:" + rs.getString(3).trim());
System.out.println();
}
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
出现错误:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at TestSQLServer.main(TestSQLServer.java:7)
是什么原因啊? 展开
3个回答
展开全部
con = DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};server=127.0.0.1;database=pubs","yht", "yht");
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
catch(SQLException e)
{e.printStackTrace();}
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
catch(SQLException e)
{e.printStackTrace();}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
jdbc中的数据库连接没有创建成功啊。可能是你的sql server的sp4补丁没有安装吧。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Connection con = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://127.0.0.1:1433", "yht", "yht");
有问题??/!!!!!
"jdbc:microsoft:sqlserver://127.0.0.1:1433", "yht", "yht");
有问题??/!!!!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询