java sqlserver jdbc问题

keymork 2012-05-23 06:58:17
package com.mini;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.*;

public class test extends JFrame{

/**
* @param args
*/
JPanel jp1, jp2;
JLabel jl1;
JButton jb1, jb2, jb3, jb4;
JTable jt;
JScrollPane jsp;
JTextField jtf;

Vector rowData, columnNames;


PreparedStatement ps = null;
Connection ct = null;
ResultSet rs= null;

public static void main(String[] args) {
// TODO Auto-generated method stub
test t = new test();
}

public test()
{
jp1 = new JPanel();
jtf = new JTextField(10);
jb1 = new JButton("查询");
jl1 = new JLabel("请输入名字");

jp1.add(jl1);
jp1.add(jtf);
jp1.add(jb1);

jp2 = new JPanel();
jb2 = new JButton("添加");
jb3 = new JButton("修改");
jb4 = new JButton("删除");

jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);

columnNames = new Vector();
columnNames.add("学号");
columnNames.add("名字");
columnNames.add("性别");
columnNames.add("年龄");
columnNames.add("籍贯");
columnNames.add("系别");

rowData = new Vector();

try{

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;DatabaseName=spdb1";
String user="sa";
String pwd="";
ct=DriverManager.getConnection(url,user,pwd);



// Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
// ct = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433; databaseName=spdb1","sa", "");

ps = ct.prepareStatement("select * from stu");

while(rs.next())
{
Vector hang = new Vector();
hang.add(rs.getString(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
hang.add(rs.getString(4));
hang.add(rs.getString(5));
hang.add(rs.getString(6));

rowData.add(hang);
}

}catch(Exception e){
e.printStackTrace();
}finally{
if(rs != null)
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(ps != null)
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(ct != null)
try {
ct.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

jt = new JTable(rowData, columnNames);
jsp = new JScrollPane(jt);
this.add(jsp);
this.add(jp1, "North");
this.add(jp2, "South");
this.setSize(400, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

}


报错


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(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.mini.test.<init>(test.java:77)
at com.mini.test.main(test.java:33)


引了sqlserver的jdbc包了 不知道哪里出了问题 请大家帮帮我。
...全文
131 3 打赏 收藏 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
你到底用的ps还是rs啊,貌似需要用rs接,ps = ct.prepareStatement("select * from stu");

改成:rs= ct.prepareStatement("select * from stu");

nmyangym 2012-05-24
  • 打赏
  • 举报
回复
String url="jdbc:sqlserver://localhost:1433;DatabaseName=spdb1";
改成:
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=spdb1";
古市轩 2012-05-23
  • 打赏
  • 举报
回复
首先保证你的程序中有SQLServer 2000 Driver for JDBC的驱动程序
其次你的系统要安装SQL Server 2000的SP4升级包。
还有你的数据库登录方式应用选择:SQL身份验证和系统验证。

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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