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 ().