日期:2014-05-20  浏览次数:20799 次

java sqlserver jdbc问题
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)