在运行它之前,NetBeans中没有任何错误,然后控制台被错误地入侵......我一直在努力解决这个数据库rip(我的第一次),我似乎无法弄明白... 寻求帮助!
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
connection();
String userName = "abc";
String password = "abc";
String url = "abc";
try{
try (Connection connect = DriverManager.getConnection(url, userName, password)) {
PreparedStatement statement;
statement = connect.prepareStatement("select * from hemligt (tjanst,vem,service,bestalls,day)");
statement.executeUpdate();
statement.close();
connect.close();
}
String report = getString("tjanst" + "vem" + "service" + "bestalls" + "day");
JOptionPane.showMessageDialog(null, "sent");
System.out.println(report);
}catch (SQLException e) {
e.printStackTrace () ;
}
}
我做了一个“写入数据库”代码并且稍微改了一下,这可能是原因..
错误;
run:
java.sql.SQLException: Can not issue executeUpdate() for SELECTs
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1086)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2407)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2366)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2350)
at AppPackage.NewJFrame.jButton1ActionPerformed(NewJFrame.java:92)
at AppPackage.NewJFrame.access$000(NewJFrame.java:22)
at AppPackage.NewJFrame$1.actionPerformed(NewJFrame.java:46)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
BUILD SUCCESSFUL (total time: too many seconds)
答案 0 :(得分:1)
您需要执行查询(从数据库读取)而不是更新(数据库中的编辑数据)
参考:http://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html
而不是:
statement.executeUpdate();
你需要:
String query = "select * from hemligt (tjanst,vem,service,bestalls,day)";
ResultSet rs = statement.executeQuery(query)
有关如何从执行查询获得的ResultSet中提取必要信息的示例,请参阅该链接。