我的Java应用程序出现了一些错误,我不知道我的错误代码在哪里。
我的错误是:
org.h2.jdbc.JdbcSQLException: Data conversion error converting "audi"; SQL statement:
select id, engine, regnum, crashed from cars where model=? [22018-193]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:168)
at org.h2.value.Value.convertTo(Value.java:972)
at org.h2.expression.Comparison.getValue(Comparison.java:255)
at org.h2.expression.Expression.getBooleanValue(Expression.java:178)
at org.h2.command.dml.Select.queryFlat(Select.java:544)
at org.h2.command.dml.Select.queryWithoutCache(Select.java:655)
at org.h2.command.dml.Query.query(Query.java:341)
at org.h2.command.dml.Query.query(Query.java:309)
at org.h2.command.dml.Query.query(Query.java:36)
at org.h2.command.CommandContainer.query(CommandContainer.java:110)
at org.h2.command.Command.executeQuery(Command.java:201)
at org.h2.server.TcpServerThread.process(TcpServerThread.java:319)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:158)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NumberFormatException: For input string: "audi"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at org.h2.value.Value.convertTo(Value.java:920)
... 12 more
at org.h2.engine.SessionRemote.done(SessionRemote.java:624)
at org.h2.command.CommandRemote.executeQuery(CommandRemote.java:158)
at org.h2.jdbc.JdbcPreparedStatement.executeQuery(JdbcPreparedStatement.java:110)
at MyFrame$SearchAction.actionPerformed(MyFrame.java:177)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我的源代码是:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class MyFrame extends JFrame{
Connection conn = null;
Connection conn1 = null;
PreparedStatement state = null;
PreparedStatement state1 = null;
ResultSet result = null;
ResultSet result1 = null;
MyModel myModel = null;
MyModel myModel1 = null;
JPanel topPanel = new JPanel();
JPanel midPanel = new JPanel();
JPanel downPanel = new JPanel();
JPanel midPanelUp = new JPanel();
JPanel midPanelDown = new JPanel();
JLabel modelLabel = new JLabel("Model: ");
JLabel yearLabel = new JLabel("Year: ");
JLabel engineLabel = new JLabel("Engine: ");
JLabel crashedLabel = new JLabel("Is crashed: ");
JLabel regNumLabel = new JLabel("Reg Number: ");
JTextField modelField = new JTextField();
JTextField yearField = new JTextField();
JTextField engineField = new JTextField();
JTextField regNumField = new JTextField();
JTextField searchText = new JTextField(10);
String[] content = {"No", "Yes"};
JComboBox<String> crashedCombo = new JComboBox<String>(content);
String[] searchContent = {"Model", "Year","Mix"};
JComboBox<String> searchCombo = new JComboBox<String>(searchContent);
JButton addButton = new JButton("Add");
JButton removeButton = new JButton("Remove");
JButton updateButton = new JButton("Update");
JButton searchButton = new JButton("Search");
JTable table = new JTable();
JScrollPane scroller = new JScrollPane(table);
public MyFrame(){
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400, 600);
this.setLocation(700, 400);
this.setLayout(new GridLayout(3,1));
this.add(topPanel);
this.add(midPanel);
this.add(downPanel);
//topPanel
topPanel.setLayout(new GridLayout(5, 2));
topPanel.add(modelLabel);
topPanel.add(modelField);
topPanel.add(yearLabel);
topPanel.add(yearField);
topPanel.add(engineLabel);
topPanel.add(engineField);
topPanel.add(regNumLabel);
topPanel.add(regNumField);
topPanel.add(crashedLabel);
topPanel.add(crashedCombo);
//end topPanel
//midPanel
midPanel.setLayout(new GridLayout(2, 1));
midPanel.add(midPanelUp);
midPanel.add(midPanelDown);
midPanelUp.add(removeButton);
removeButton.addActionListener(new RemoveAction());
midPanelUp.add(updateButton);
updateButton.addActionListener(new UpdateAction());
midPanelUp.add(addButton);
addButton.addActionListener(new AddAction());
midPanelDown.add(searchButton);
midPanelDown.add(searchCombo);
midPanelDown.add(searchText);
searchButton.addActionListener(new SearchAction());
//end midPanel
//downPanel
scroller.setPreferredSize(new Dimension(350, 150));
downPanel.add(scroller);
getAllFromDB();
//end downPanel
}
public class SearchAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String model = null;
int year = 1908;
String critiria = "model";
switch(searchCombo.getSelectedIndex()){
case 0: model = searchText.getText();
break;
case 1: year = Integer.parseInt(searchText.getText());
critiria = "year";
break;
case 2: model = modelField.getText();
year = Integer.parseInt(yearField.getText());
break;
}
conn = UtilDB.getConnected();
conn1 = UtilDB.getConnected();
String sql = "select id, engine, regnum, crashed " + "from cars " + "where " + critiria + "=?";
String sqlMix = "select id, model, engine, regnum, crashed " + "from cars " + "where model like ? AND year >= ?";
try {
state1 = conn1.prepareStatement(sqlMix);
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
if (searchCombo.getSelectedIndex() == 2){
try {
state1.setString(1, model + "%");
state1.setInt(2, year);
result1 = state1.executeQuery();
myModel1 = new MyModel(result1);
table.setModel(myModel1);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
state = conn.prepareStatement(sql);
if (searchCombo.getSelectedIndex() == 0){
state.setString(1, model);
}
else{
state.setInt(1, year);
}
result = state.executeQuery();
myModel = new MyModel(result);
table.setModel(myModel);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public class UpdateAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String model = modelField.getText();
String regNum = regNumField.getText();
conn = UtilDB.getConnected();
String sql = "update cars set model = ? where regnum = ?";
try {
state = conn.prepareStatement(sql);
state.setString(1, model);
state.setString(2, regNum);
state.execute();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
getAllFromDB();
}
}
public class RemoveAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String regNum = regNumField.getText();
clearFields();
conn = UtilDB.getConnected();
String sql = "delete from cars where regnum = ?";
try {
state = conn.prepareStatement(sql);
state.setString(1, regNum);
state.execute();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
getAllFromDB();
}
}
public class AddAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String model = modelField.getText();
int year = Integer.parseInt(yearField.getText());
float engine = Float.parseFloat(engineField.getText());
String regNum = regNumField.getText();
boolean isCrashed = false;
if (crashedCombo.getSelectedIndex() == 1) {
isCrashed = true;
}
clearFields();
conn = UtilDB.getConnected();
String sql = "insert into cars values(null, ?, ?, ?, ?, ?)";
try {
state = conn.prepareStatement(sql);
state.setString(1, model);
state.setInt(2, year);
state.setFloat(3, engine);
state.setString(4, regNum);
state.setBoolean(5, isCrashed);
state.execute();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
getAllFromDB();
}// end actionPerformed
}// end class
public void clearFields() {
modelField.setText("");
yearField.setText("");
engineField.setText("");
regNumField.setText("");
crashedCombo.setSelectedIndex(0);
}
public void getAllFromDB(){
conn = UtilDB.getConnected();
String sql = "select * from cars";
try {
state = conn.prepareStatement(sql);
result = state.executeQuery();
myModel = new MyModel(result);
table.setModel(myModel);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}// end MyFrame class
我的错误代码可能是在SQL查询中,但我不确定..有些想法如何修复它? 我的第二个任务是当用户输入框中的模型字母&#34; MA&#34;时,向MySql发出Like请求。出去马自达和玛莎拉蒂..
答案 0 :(得分:0)
它看起来像是下面的开关块:
switch(searchCombo.getSelectedIndex()){
case 0: model = searchText.getText();
break;
case 1: year = Integer.parseInt(searchText.getText());
critiria = "year";
break;
case 2: model = modelField.getText();
year = Integer.parseInt(yearField.getText());
break;
searchText
或yearField
中的任何一个都有string
值,因此,当它进入case
1或2时,它会尝试将其转换为整数,从而导致NumberFormatException
。
答案 1 :(得分:0)
它正在抛出NumberFormatException,这意味着值&#34; audi&#34;不是一个数字,但你仍然试图将其转换为某种数字格式。
答案 2 :(得分:-1)
错误消息全部显示:
引起:java.lang.NumberFormatException:对于输入字符串:“audi”
您正在尝试将字符串字(音频为msg建议..)转换为数字
也许在这里?
case 1: year = Integer.parseInt(searchText.getText());
critiria = "year";
break;