
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!
- Error: Can not find the tag library descriptor for
- Create a Database Table using JDBC PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- Java Jackson with Maven Example
- [fix] Java JDBC ConnectException: Connection refused
- Spring Boot: Transactions Management with JDBCTemplate Example
- Java Get Current Date for a Specific Time Zone
- What are the 8 Primitive Data Types in Java
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Maven Eclipse (M2e) No archetypes currently available
- How to Sort a LinkedList in Java
- [Fatal Error] XML The markup in the document following the root element must be well-formed.
- Split a String in Java with Examples
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- Truncate table using Java JDBC Example
- Java: Generate random numbers within a range
- Parse XML file in Java using DOM Parser
- How to get Client IP address using Java Code Example
- JDBCTemplate Querying Examples with Spring Boot 3
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- String Boot + Redis - SET and GET String Commands Examples
- Setting up Spring Boot 3 + Maven + MySQL + JDBC Example
- Spring Boot: JdbcTemplate Update Query With Parameters Example
- Java Split String by Spaces
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- How to Import External Jars to Android Studio Project - Android-Studio
- Trace using cURL Command Example - cURL
- How to rename a tab in Notepad++ - NotepadPlusPlus
- JavaScript : Get url protocol HTTP, HTTPS, FILE or FTP - JavaScript
- How to know Notepad++ is 32-bit or 64-bit Version - NotepadPlusPlus
- Can't Run SDK Manager find_java.bat issue - Android
- Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED - Android
- Android Studio - Installed build tools revision 31.0.0 is corrupted. Remove and install again using the SDK manager. - Android-Studio