在ECLLIPSE里创建项目时导入JDBC的JAR包(msbase.jar,mssqlserver.jar,msutil.jar)
代码是:
//Test.java 
import java.awt.Container; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 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 Test 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(); 
java.sql.Statement stmt = null; 
public Test(){ 
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); 
try{ 
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
java.sql.Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename = test","sa","test"); 
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+"%'"; 

ResultSet rs = stmt.executeQuery(sql); 
while(rs.next()){ 
int id = rs.getInt(1); 
String name = rs.getString(2); 
String department = rs.getString(3); 
int salary = rs.getInt(4); 
JLabel label = new JLabel(id +" "+name+" "+department + " "+salary); 
JPanel panel = new JPanel(); 
panel.add(label); 
panel_content.add(panel); 
i++; 

}catch(Exception e1){ 
e1.printStackTrace(); 

panel_content.setLayout(new GridLayout(i,1)); 
panel_content.validate(); 

public static void main(String[] args){ 
new Test(); 
} } 
数据是:1,张桂圆,技术部,3000 
        2,加封眼,开发部,2500 
        3,张预想,技术部,5000 
        4,张大牛,开发部,5500 运行后总是没有查到数据  帮忙看看  

解决方案 »

  1.   

    找问题的步骤:
    1.程序有无报错。如:链接不上SQLServer
    2.拼写成SQL语句直接能不能查出结果。如果不能查出结果的话,你这里没有查到数据就很正常了。
    3.如果也能查出正确的结果,就看下面的现实部分的逻辑是否正确。多多练习调试,这是程序员必备的本领。
      

  2.   

    应该是你的JFrame中的错误吧,组件是不是只有声明,还有布局是不是有问题啊
      

  3.   

    我看了这么长时间,你编写的代码应该没有什么问题
    感觉问题出在:
    你没有在Test类的构造函数内调用你要显示内容的方法actionPerformed(ActionEvent e)
    你看一下调用关系,main方法new Test()-->在Test()的构造函数中,你对容器和面板以及布局都没有错误,
    并且数据库连接也没有问题,但是没有发现对你下面显示内容的方法actionPerformed(ActionEvent e)的调用
    所以也就没有内容了
      

  4.   

    应该是这个问题,楼主试一下,加入对方法actionPerformed(ActionEvent e)调用,
    应该就可以了
      

  5.   


    public Test(){ 
       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);    try{ 
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
            java.sql.Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename = test","sa","test"); 
             stmt = conn.createStatement();    }catch(Exception e){ 
             e.printStackTrace(); 
       } 
             this.setVisible(true); 

    你仔细看一边,你的Test()构造函数内没有调用actionPerformed(ActionEvent e )
    而你的main()方法中只有new Test(). 也,没有对actionPerformed方法的调用啊!
    怎么会说你已经调用了呢
      

  6.   

    package com.shop.db;
    import java.sql.*;
    import java.io.*;/**************************************************
     * author:East(张栋芳)
     * date:2008-6-13
     * note:打开数据库的连接,和关闭连接
     **************************************************/public final class DataBaseOperator {

        public DataBaseOperator() {
        }
        
        /***
         *连接数据库用户指定driver,url,user,pwd
         */
        public static Connection createConnection(String driver,String url,String user,String pwd){
            Connection con = null;
            try{
                Class.forName(driver);
                con = DriverManager.getConnection(url,user,pwd);
            }catch(ClassNotFoundException ce){
                ce.printStackTrace();
            }catch(SQLException se){
                se.printStackTrace();
            }
            return con;
        }
        /***
         *连接数据库用户用默认的SQL server 2000的纯驱动
         */
        public static Connection createConnection(){
            String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
            String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=pubs";
            String user = "sa";
            String pwd = "";
            return createConnection(driver,url,user,pwd);
        }
        /***
         *创建一个连接通过配制文件
         */
        public static Connection createConnection(String fileName){
         String driver="";
         String url="";
         String user="";
         String pwd="";
         BufferedReader in = null;
         
         try{
          in =  new BufferedReader(new FileReader(fileName));
          driver = in.readLine();
          url = in.readLine();
          user = in.readLine();
          pwd = in.readLine();
          in.close(); 
         }catch(IOException ioe){
          ioe.printStackTrace();
          
         }
         return createConnection(driver,url,user,pwd);
        }
        /***
         *创建一个连接桥驱动
         */
        public static Connection createConnection(String driver,String url){
          driver = "sun.jdbc.odbc.JdbcOdbcDriver";
          url = "jdbc:odbc:myodbc";
         String user = "sa";
         String pwd = "";
         return createConnection(driver,url,user,pwd);
        }
        /***
         *关闭连接。con,pstmt,rs;
         */
        public static void closeAll(Connection con,PreparedStatement pstmt,ResultSet rs){
            try{
                    if(rs != null)rs.close();
                if(pstmt != null) pstmt.close();
                if(con != null) con.close();
            }catch(SQLException se){
                se.printStackTrace();
            }finally{
                rs = null;
                pstmt = null;
                con = null;        }
        }
        
        /***
         *关闭连接。con,pstmt;
         */ 
        public static void closeAll(Connection con,PreparedStatement pstmt){
            try{
                if(pstmt != null) pstmt.close();
                if(con != null) con.close();
            }catch(SQLException se){
                se.printStackTrace();
            }finally{
                pstmt = null;
                 con = null;
            }
        }
        /***
         *关闭连接。con
         */ 
        public static void closeAll(Connection con){
            try{
                if(con != null) con.close();
            }catch(SQLException se){
                se.printStackTrace();
            }finally{
                con = null;
            }
        }
        /**
         *关闭一个连接con,stmt,rs
         */
        public static void closeAll(Connection con,Statement stmt,ResultSet rs){
         try{
          if(con != null) con.close();
          if(stmt != null) stmt.close();
          if(rs != null) rs.close();
         }catch(SQLException se){
          se.printStackTrace();
         }
        }
        /**
         *关闭一个连接con,stmt
         */
        public static void closeAll(Connection con,Statement stmt){
         try{
          if(con != null) con.close();
          if(stmt != null) stmt.close();
         }catch(SQLException se){
          se.printStackTrace();
         }
        }}看看这个
      

  7.   

    package com.failthshopping.daos;import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Vector;
    import com.failthshopping.beans.UserInfoBean;/**
     * @date 2008-8-4
     * @author East(张栋芳)
     * @UserInfoDao(用户信息表)的数据库操作
     */
    public class UserInfoDao
        extends BaseDao {
      public UserInfoDao() {
      }
      /****
       * 增加用户信息
       */
      public boolean insertUserInfo(UserInfoBean user) {
        Connection con = null;
        PreparedStatement pstmt = null;
        int result = 0;
        String strSql = "insert into userInfo(userName,password,sex,age,card,qq,email,telephone,address,buyPoint,sellPoint,headImg,isLogin,isVip,pwdQuestion,pwdAnswer,re)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
          con = super.createConnection();
          pstmt = con.prepareStatement(strSql);
          pstmt.setString(1, user.getUserName());
          pstmt.setString(2, user.getPassword());
          pstmt.setString(3, user.getSex());
          pstmt.setInt(4, user.getAge());
          pstmt.setString(5, user.getCard());
          pstmt.setString(6, user.getQq());
          pstmt.setString(7, user.getEmail());
          pstmt.setString(8, user.getTelephone());
          pstmt.setString(9, user.getAddress());
          pstmt.setInt(10, user.getBuyPoint());
          pstmt.setInt(11, user.getSellPoint());
          pstmt.setString(12, user.getHeadImg());
          pstmt.setInt(13, user.getIsLogin());
          pstmt.setInt(14, user.getIsVip());
          pstmt.setString(15, user.getPwdQuestion());
          pstmt.setString(16, user.getPwdAnswer());
          pstmt.setString(17, user.getRe());
          result = pstmt.executeUpdate();
        }
        catch (SQLException se) {
          se.printStackTrace();
        }
        finally {
          super.closeAll(con, pstmt);
          return result > 0 ? true : false;
        }
      }
      /****
       * 通过用户名修改用户基本信息
       */
      public boolean updateUserInfoByUserName(UserInfoBean user) {
        Connection con = null;
        PreparedStatement pstmt = null;
        int result = 0;
        String strSql = "update userInfo set password=?,sex=?,age=?,card=?,qq=?,email=?,telephone=? where userName=?";
        try {
          con = super.createConnection();
          pstmt = con.prepareStatement(strSql);
          pstmt.setString(1, user.getPassword());
          pstmt.setString(2, user.getSex());
          pstmt.setInt(3, user.getAge());
          pstmt.setString(4, user.getCard());
          pstmt.setString(5, user.getQq());
          pstmt.setString(6, user.getEmail());
          pstmt.setString(7, user.getTelephone());
          pstmt.setString(8, user.getUserName());
          result = pstmt.executeUpdate();
        }
        catch (SQLException se) {
          se.printStackTrace();
        }
        finally {
          super.closeAll(con, pstmt);
          return result > 0 ? true : false;
        }
      }
      /****
       * 通过用户名修改用户密码
       */
      public boolean updateUserPwdByUserName(UserInfoBean user) {
        Connection con = null;
        PreparedStatement pstmt = null;
        int result = 0;
        String strSql = "update userInfo set password=? where userName=?";
        try {
          con = super.createConnection();
          pstmt = con.prepareStatement(strSql);
          pstmt.setString(1, user.getPassword());
          pstmt.setString(2, user.getUserName());
          result = pstmt.executeUpdate();
        }
        catch (SQLException se) {
          se.printStackTrace();
        }
        finally {
          super.closeAll(con, pstmt);
          return result > 0 ? true : false;
        }
      }  /****
       * 通过用户名删除用户信息
       */
      public boolean deleteUserInfoByUserName(UserInfoBean user) {
        Connection con = null;
        PreparedStatement pstmt = null;
        int result = 0;
        String strSql = "delete userInfo where userName=?";
        try {
          con = super.createConnection();
          pstmt = con.prepareStatement(strSql);
          pstmt.setString(1, user.getUserName());
          result = pstmt.executeUpdate();
        }
        catch (SQLException se) {
          se.printStackTrace();
        }
        finally {
          super.closeAll(con, pstmt);
          return result > 0 ? true : false;
        }
      }
      /***
       * 通过用户名修改是否登录
       */
      public boolean updateByUserNameIsLogin(UserInfoBean user) {
        Connection con = null;
        PreparedStatement pstmt = null;
        String strSql = "update userInfo set isLogin = ? while userName=?";
        int result = 0;
        try {
          con = super.createConnection();
          pstmt = con.prepareStatement(strSql);
          pstmt.setInt(1, user.getIsLogin());
          pstmt.setString(2, user.getUserName());
          result = pstmt.executeUpdate();
        }
        catch (SQLException se) {
          se.printStackTrace();
        }
        finally {
          super.closeAll(con, pstmt);
          return result > 0 ? true : false;
        }
      }
      /***
       * 通过用户名修改是否是会员
       */
      public boolean updateByUserNameIsVip(UserInfoBean user) {
        Connection con = null;
        PreparedStatement pstmt = null;
        String strSql = "update userInfo set isVip = ? where userName=?";
        int result = 0;
        try {
          con = super.createConnection();
          pstmt = con.prepareStatement(strSql);
          pstmt.setInt(1, user.getIsVip());
          pstmt.setString(2, user.getUserName());
          result = pstmt.executeUpdate();
        }
        catch (SQLException se) {
          se.printStackTrace();
        }
        finally {
          super.closeAll(con, pstmt);
          return result > 0 ? true : false;
        }
      }
      /****
       * 查询是否存在用户名和密码
       */
      public boolean isExistsUserName(UserInfoBean user) {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String strSql = "select * from userInfo where userName=? and password = ?";
        int result = 0;
        try {
          con = super.createConnection();
          pstmt = con.prepareStatement(strSql);      pstmt.setString(1, user.getUserName());
          pstmt.setString(2, user.getPassword());
          rs = pstmt.executeQuery();
          if (rs.next()) {
            result++;
          }
          else {
            return false;
          }
        }
        catch (SQLException se) {
          se.printStackTrace();
        }
        finally {
          super.closeAll(con, pstmt, rs);
          return result > 0 ? true : false;
        }
      }  /***
       * 通过用户名修改用户的密码问题和密码答案
       */
      public boolean updatePwdQuestion(UserInfoBean user) {
        int result = 0;
        Connection con = null;
        PreparedStatement pstmt = null;
        String strSql =
            "update userInfo set pwdQuestion =?,pwdAnswer=? where userName=?";
        try {
          con = super.createConnection();
          pstmt = con.prepareStatement(strSql);
          pstmt.setString(1, user.getPwdQuestion());
          pstmt.setString(2, user.getPwdAnswer());
          pstmt.setString(3, user.getUserName());
          result = pstmt.executeUpdate();
        }
        catch (SQLException se) {
          se.printStackTrace();
        }
        finally {
          super.closeAll(con, pstmt);
          return result > 0 ? true : false;
        }
      }
      /****
       * 通过用户名,密码问题,来获的密码答案
       */
      public String getPwdAnswerByNQ(UserInfoBean user){
        String pwdAnswer="";
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String strSql =
            "select pwdAnswer  from userInfo where userName=? and pwdQuestion=?";
        try {
          con = super.createConnection();
          pstmt = con.prepareStatement(strSql);
          pstmt.setString(1, user.getUserName());
          pstmt.setString(2, user.getPwdQuestion());
          rs = pstmt.executeQuery();
          if (rs.next()){
              pwdAnswer= rs.getString(1);
          }
        }
        catch (SQLException se) {
          se.printStackTrace();
        }
        finally {
          super.closeAll(con, pstmt);
          return pwdAnswer;
        }
      }  }
      

  8.   

    我又写了个简单的试了一下
    调试的时候 在java.sql.Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename = test","sa","test")
    这里有问题搞的不是很明白
      

  9.   

    把错误贴出来  大家帮忙看看,sqlserver的补丁是sp4的,sqlserver 2000 driver for jdbc 也安装了
    调试时还是有问题
    快来帮帮
    跪求知道
    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)
      

  10.   

    ?????????
    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:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at Number.Conn.getConnection(Conn.java:15)
    at Number.Conn.main(Conn.java:24)
    以上是我连接数据库时出现的错误,为什么1433的端口不能用?
    我的eclipse控制台中怎么不能输出汉语了?