java.sql.SQLException: getShort() 的无效值 -“ ”
java.sql.SQLException: Invalid value for getShort() - ' '
我想在 java 代码中使用 DocumentListener 自动过滤搜索
但代码显示
error java.sql.SQLException: Invalid value for getShort() - ''
与 'single quote' 中的匹配值,即。程序显示错误(java.sql.SQLException:getShort() 的无效值 - 'Rabin Karki')
当我在搜索文本字段中输入 (' r ') 时
我的数据库 table 是 'staff' 并且包含以下列
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();
}
}
select * from
Returns 列按 create
语句创建的顺序排列。
在您的情况下,名称将是第一列。您使用 rs.getShort(1)
从结果集中读取的内容。你必须在这里使用字符串:
model.addRow(new Object[] {rs.getString(1...
您最好将列添加到 select
以定义顺序。
我想在 java 代码中使用 DocumentListener 自动过滤搜索 但代码显示
error java.sql.SQLException: Invalid value for getShort() - ''
与 'single quote' 中的匹配值,即。程序显示错误(java.sql.SQLException:getShort() 的无效值 - 'Rabin Karki') 当我在搜索文本字段中输入 (' r ') 时
我的数据库 table 是 'staff' 并且包含以下列
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();
}
}
select * from
Returns 列按 create
语句创建的顺序排列。
在您的情况下,名称将是第一列。您使用 rs.getShort(1)
从结果集中读取的内容。你必须在这里使用字符串:
model.addRow(new Object[] {rs.getString(1...
您最好将列添加到 select
以定义顺序。