java.sql.SQLException:getShort()的值无效 - '

时间:2017-03-30 09:40:07

标签: java mysql jdbc documentlistener

我想在我的java代码中使用DocumentListener自动过滤搜索 但代码显示

  

错误java.sql.SQLException:getShort()的值无效 - ''

在'单引号'中匹配值,即。程序显示错误(java.sql.SQLException:getShort()的无效值 - 'Rabin Karki') 当我在搜索文本字段中输入('r')

我的数据库表是'staff',包含以下列

  

Name varchar(40),Designation varchar(40),Salary int(),Address   varchar(40),联系int(),Email varchar(40),BirthDate date(),   JoinDate()

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.*;
import javax.swing.text.Document;

public class Test extends JFrame implements ActionListener{

    JTextField txt;
    JButton btn;

    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);

    Connection con;
    Statement stmt;
    PreparedStatement pstmt;
    ResultSet rs;

    public Test(){
        txt = new JTextField();
        btn = new JButton("Search");

        setLayout(null);

        add(txt);
        txt.setBounds(320,20,100,20);

        add(btn);
        btn.setBounds(500,20,80,20);

        txt.getDocument().addDocumentListener(new MyDocumentListener());

        try{
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/salarymanagement","root","");
            stmt = con.createStatement();
            rs = stmt.executeQuery("select * from staff");

            model.addColumn("Name");
            model.addColumn("Designation");
            model.addColumn("Salary");
            model.addColumn("Address");
            model.addColumn("Contact");
            model.addColumn("Email");
            model.addColumn("Birth Date");
            model.addColumn("Join Date");

            while(rs.next()){
                model.addRow(new Object[] { rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8)});
            }

            int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
            int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;

            JScrollPane scrollPane = new JScrollPane(table,v,h);
            add(scrollPane, BorderLayout.CENTER);
            scrollPane.setBounds(5,60,1000,1000);

            table.updateUI();
        }
        catch(Exception ex){
            System.out.println("ERROR :"+ex);
        }
        setSize(1200,1200);
        setVisible(true);
        setTitle("Test of search filter");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    class MyDocumentListener implements DocumentListener{

        public void update(DocumentEvent de){
            Document doc = (Document)de.getDocument();
            int length = doc.getLength();
            String str = null;

            try{
                str = doc.getText(0,length);
            }
            catch(Exception ex){
                System.out.println("error"+ex);
            }

            try{
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/salarymanagement","root","");
                pstmt = con.prepareStatement("select * from staff where Name like '"+str+"%' order by Name");
                rs = pstmt.executeQuery();

                model.setRowCount(0);

                while(rs.next()){
                    model.addRow(new Object[] {rs.getShort(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8) });
                }
                pstmt.close();
                con.close();
            }
            catch(Exception exc){
                System.out.println("error "+exc);
            }
        }

        @Override
        public void insertUpdate(DocumentEvent de) {
            update(de);
        }

        @Override
        public void removeUpdate(DocumentEvent de) {
            update(de);
        }

        @Override
        public void changedUpdate(DocumentEvent de) {
            update(de);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    public static void main(String[] args) {
        new Test();
    }
}

1 个答案:

答案 0 :(得分:1)

select * from按照create语句创建的列顺序返回列。 在您的情况下,Name将是fiorst列。您从结果集中使用rs.getShort(1)读取的内容。你必须在这里使用String:

               model.addRow(new Object[] {rs.getString(1...

您最好将列添加到select以定义订单。