Java.sql.SQLException:Can not issue data manipulation statements with executeQuery ().

Source: Internet
Author: User

1. Error description

Java.sql.SQLException:Can not issue data manipulation statements with executeQuery (). Com.mysql.jdbc.SQLError.createSQLException (sqlerror.java:996) at Com.mysql.jdbc.SQLError.createSQLException ( sqlerror.java:935) at Com.mysql.jdbc.SQLError.createSQLException (sqlerror.java:924) at Com.mysql.jdbc.SQLError.createSQLException (sqlerror.java:870) at COM.MYSQL.JDBC.STATEMENTIMPL.CHECKFORDML ( statementimpl.java:472) at Com.mysql.jdbc.StatementImpl.executeQuery (statementimpl.java:1401) at Com.you.sql.Student.commonMethod (student.java:117) at Com.you.sql.Student.insertStudent (student.java:86) at Com.you.sql.Student.main (student.java:181)

2. Cause of error

public static void Insertstudent () {StringBuffer sql = new StringBuffer (); int j = 0; String str = "0"; for (int i=5;i<1000;i++) {++j;if (i%2 = = 0) {str = "0";} Else{str = "1";} Sql.append ("INSERT INTO T_stu_info ("). Append (i). Append (","). Append (""). 103+j+ (","). Append ("   append" + (i-4)). Append (","). Append (str). Append (","). Append (Integer.parseint (Math.Round (Math.random (   ) *10+20) + ""). Append (" , ")   . Append (" 123 "+i); Commonmethod (Sql.tostring ());}

/** * * @Title: Student * @Description: * @param sqlstu * @Date: June 11, 2015 Morning 12:25:56 * @return: void * @throws */public static void Commonmethod (String sqlstu) {StringBuffer sql = new StringBuffer (); Sql.append (Sqlstu); Connection conn = null; Statement stat = null; ResultSet rs = null;try {try {class.forname (driver_class);} catch (ClassNotFoundException e) {e.printstacktrace ();} conn = Drivermanager.getconnection (URL, USER, PASSWORD); stat = Conn.createstatement (); rs = Stat.executequery ( Sql.tostring ()); while (Rs.next ()) {String Stuid = rs.getstring ("stu_id"); String stuname = rs.getstring ("Stu_name"); String stusex = rs.getstring ("Sex"); String stuage = rs.getstring ("Stu_age"); String Stuphone = rs.getstring ("Stu_phone"); System.out.println ("School Number:" +stuid+ "----" + "Name:" +stuname+ "----" + "Gender:" +stusex+ "---" + "Age:" +stuage+ "----" + "Phone:" + Stuphone);}} catch (SQLException e) {e.printstacktrace ();} Finally{if (rs! = null) {try {rs.close ();} catch (SQLException e) {e.printstacktrace ()}} if (stat! = null) {try {Stat.cloSe ();} catch (SQLException e) {e.printstacktrace ();}} IF (conn! = null) {try {conn.close ();} catch (SQLException e) {e.printstacktrace ();}}}}

3. Solutions

An error occurred because the ExecuteQuery method was called when the insert operation was performed, and the insert operation should call the Executeupdate method

int result = Stat.executeupdate (sql.tostring ());

Java.sql.SQLException:Can not issue data manipulation statements with executeQuery ().

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.