大家好,请考虑以下代码。我正在使用它向mysql数据库(booking
)中的表(seat_booking
)中添加新值。
变量包括String(id,cname,ctype,event_name)
和int(seatsno)
。我在此代码中还调用了一个函数EventName()
,相信我,它工作得很好,并且我能够从另一个函数中派生事件名称。表(事件),但是唯一的问题出现在此方法中。
我的项目是制作一个座位预订GUI应用程序,因此当我使用它为“管理员”模式添加新事件时此代码确实起作用,但是在这里不起作用(即当我需要它来存储预订时)特定客户的价值。请帮助大家。我真的很感激。
private void newbookbtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
Class.forName("java.sql.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/seat_booking","root","<mypassword>");
Statement stmt = con.createStatement();
String query = "SELECT * FROM booking";
ResultSet rs = stmt.executeQuery(query);
String cname = tfcname.getText();
int seatsno = Integer.parseInt(tfseatsno.getText());
String evtname = EventName();
String ctype = "Normal Customer"; long bookid = getID();
String id = Long.toString(bookid);
String query1="insert into booking values('"+ id +"','"+ cname +"','"+ ctype +"','"+evtname+"',"+ seatsno+");";
stmt.executeUpdate(query1);
JOptionPane.showMessageDialog(null, "Booking successfull!");
stmt.close();
con.close();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"Error encountered while booking new event!");
}
}
我尝试将e.Stacktrace();
语句放入应用程序中,并运行了以下命令,结果是错误输出:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's Track Events Warm-up,1)' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2788)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1816)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1730)
at CustomerNewBook.newbookbtnActionPerformed(CustomerNewBook.java:256)
at CustomerNewBook.access$100(CustomerNewBook.java:16)
at CustomerNewBook$2.actionPerformed(CustomerNewBook.java:110)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
...
答案 0 :(得分:0)
您使用了错误的驱动程序类。正确的是:
Class.forName("com.mysql.jdbc.Driver");
java.sql.Driver
仅仅是一个接口。有关更多信息,请参见javadoc
顺便说一句:了解有关防止sqlinjection的准备好的声明 不要抑制错误消息。至少将它们记录下来或放入消息对话框中:
catch (Exception e)
{
e.printStacktrace();
JOptionPane.showMessageDialog(null,"Error encountered while booking new event!"+e.getMessage());
}
答案 1 :(得分:0)
更新2个(用于this comment):
检查错误,
您的SQL语法有错误...
s Track Events Warm-up,1)
附近...
问题在于您传递的字符串。确保您的任何变量(id, cname, ctype, evtname
都不包含单个qoutes('
)或任何其他可能干扰查询的转义字符。
使用Prepared Statements可以轻松解决此问题。试试
String query1 = "INSERT INTO `booking` VALUES(?, ?, ?, ?, ?)";
PreparedStatement stmt = con.prepareStatement(query1);
stmt.setString(1, id); //1 specifies the first parameter in the query
stmt.setString(2, cname);
stmt.setString(3, ctype);
stmt.setString(4, evtname);
stmt.setInt(5, seatsno); //use setInt() to parse ints
stmt.executeUpdate();
更新(针对this comment):
在您的catch
块中,注释JOptionPane
行并添加以下内容,
catch (Exception e){
e.printStackTrace();
// JOptionPane.showMessageDialog(null, ...);
}
然后再次运行您的应用程序,并使用错误输出更新问题。
原始答案:
如果您使用的是jdbc:mysql
,则需要MySQL连接器。
mysql-connector-java-8.0.12.jar
添加到您的项目中。更改驱动程序类,
Class.forName("com.mysql.jdbc.Driver");