运行时,出现在样的出错提示:
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:512)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at query.QueryFrame. <init> (QueryFrame.java:48)
at query.QueryFrame.main(QueryFrame.java:103)
////////////////////////////////////////////////////////
运行环境
IDE : Eclipse3.1
DB : SQLServer2000
Driver : MS 的官方驱动: msbase.jar 、 mssqlserver.jar 、 msutil.jar
/////////////////////////////////////////////////////////
原代码:
package query;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class QueryFrame extends JFrame implements ActionListener{
JTextField jf = new JTextField(20);
String[] value = { "姓名 ", "部门 ", "薪水 "};
JComboBox box = new JComboBox(value);
JButton find = new JButton( "查询 ");
JPanel panel_find = new JPanel();
JPanel panel_content = new JPanel();
Statement stmt = null;
public QueryFrame() {
this.setTitle( "查询系统 ");
Container c = this.getContentPane();
//完成了查找界面的设置
jf.addActionListener(this);
find.addActionListener(this);
panel_find.add(jf);
panel_find.add(box);
panel_find.add(find);
c.add(panel_find, "North ");
c.add(panel_content, "Center ");
this.setSize(400,400);
this.setLocation(200,100);
//连接SQL Server数据库
try{
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
Connection conn = DriverManager.getConnection( "jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=xdf ", "xdf ", "xdf ");
stmt = conn.createStatement();
}catch(Exception e){
e.printStackTrace();
}
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
panel_content.removeAll();
String item = (String)box.getSelectedItem();
//确定用户所选择的查询依据
if(item.equals( "姓名 ")){
item = "name ";
}
if(item.equals( "部门 ")){
item = "department ";
}
if(item.equals( "薪水 ")){
item = "salary ";
}
//获取用户输入的查询关键字
String content = jf.getText();
int i = 0;
try{
String sql = "select * from employee ";
if(!content.equals( " ")){
sql = sql + " where "+item+ " like '% "+content+ "% ' "; //组装查询语句
}
//执行查询语句,并显示查询结果