javamysqljdbcdocumentlistener

java.sql.SQLException: Invalid value for getShort() - ' '


I want to auto filter the search using the DocumentListener in my java code but the code shows

error java.sql.SQLException: Invalid value for getShort() - ''

with matching value in the 'single quote' ie. the program shows the error ( java.sql.SQLException: Invalid value for getShort() - 'Rabin Karki' ) when I enter (' r ') in the search text field

My database table is 'staff' and contains following columns

Name varchar(40), Designation varchar(40), Salary int(), Address varchar(40), Contact 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();
    }
}

Solution

  • select * from Returns the columns in order they are created by the create Statement. In your case Name will be the fiorst column. Which you read with rs.getShort(1) from the resultset. You have to use String here:

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

    You should better add the columns to the select to define the order.