
String insertQuery ="insert into students values(?,?,?,?)";
Connection connection = DriverManager.getConnection(url,userName,password);
PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
preparedStatement.executeQuery();
Stacktrace:
Exception in thread "main" java.sql.SQLException:
Statement.executeQuery() cannot issue statements that do not produce result sets.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:928)
at JDBCInsertExample.main(JDBCInsertExample.java:17)
Issue:
You get SQLException when you try to call executeQuery() on PreparedStatement Object if it does not return a result set, for example, INSERT, UPDATE or DELETE queries.
Solution:
Replace preparedStatement.executeQuery(); with preparedStatement.execute();
-
Have Questions? Post them here!
More Posts related to Java,
- Drop table using Java JDBC Template
- Java - Check if array contains the value
- YAML Parser using Java Jackson Library Example
- Java Jackson ObjectMapper Class with Examples
- Get Client IP address from HTTP Response in Java
- How to Word-Warp Console logs in IntelliJ
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- Setting Java_Home Environment variable on Windows Operating System
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Java SE JDBC Select Statement Example
- How to extract Java Jar/War/Ear files in Linux
- java: unclosed string literal [Error]
- [Solution] Exception in thread main java.util.EmptyStackException
- Read YAML file Java Jackson Library
- What Java version is used for Minecraft 1.18
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Program] How to read three different values using Scanner in Java
- Java 8 Predicate Functional Interface Examples
- Display Era details (AD BC) in Java Date using SimpleDateFormat
- Convert String Date to Date Object in Java
- Struts 2 Hello World Example in Eclipse
- Read a file using Java 8 Stream
- Java - How to set custom thread name?
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- java: ']' expected error [fixed]
More Posts:
- Installing MongoDB on Linux/Unix/macOS/Ubuntu - MacOS
- Can we move apps like WhatsApp, Facebook to external MicroSD card - WhatsApp
- How to open new tabs macOS Terminal - MacOS
- How to update Android Studio - Android-Studio
- Make Android View Scrollable both Horizontally and Vertically - Android
- Android : Unable to load VM from snapshot : Mac OS X Error - Android
- Remove AirDrop Icon from macOS Menu Bar - MacOS
- Check help on commands while being on sftp> prompt - FTP