Results 1 to 2 of 2
  1. #1
    harry is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Post Java MySql Select Statement

    I am trying to run the following sql statement in java (MySql), I am not sure what the problem is.

    When I run the SQL Query directly, it runs correctly.

    Java Code:
    Select * FROM enrollment where StudentId = 1 AND SubjectId In(Select SubjectCode FROM subject WHERE Major = 'E' OR Major = 'C' OR Major = 'B')
    I am not very good in java. Please help.


    Java Code:
     
    elective2 = "N";
    elective3 ="B";
    elective4 ="D";
     sql3 = "SELECT * FROM student WHERE StudentId = '"+ StudentId +"'  AND SubjectId  IN (Select SubjectCode FROM subject WHERE Major = '"+elective1 + "' OR Major = '"+elective2+"' OR Major = '" + elective3+ "')";
    
    
    Statement statement3 = con.createStatement();
    ResultSet rs3 = statement3.executeQuery(sql3);
    					     
     while (rs3.next()){
    System.out.println("SQL3 run correclty");
    numberOfElectivesEnrolled++;
    					     }
    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorEx ception: Unknown column 'SubjectId' in 'IN/ALL/ANY subquery'
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:41 1)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:1052)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:3609)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:3541)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:20 02)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java :2163)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionIm pl.java:2618)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionIm pl.java:2568)
    at com.mysql.jdbc.StatementImpl.executeQuery(Statemen tImpl.java:1557)
    at com.enroll.Enroll.checkSubjectIsCore(Enroll.java:2 75)
    at com.enroll.Enroll.checkStudentCurrentEnrollments(E nroll.java:319)
    at com.enroll.Enroll.checkStudentEligibity(Enroll.jav a:144)
    at com.enroll.RunEnrollment$EnrolListener.valueChange d(RunEnrollment.java:288)
    at javax.swing.DefaultListSelectionModel.fireValueCha nged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueCha nged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.setValueIsAd justing(Unknown Source)
    at javax.swing.plaf.basic.BasicTableUI$Handler.setVal ueIsAdjusting(Unknown Source)
    at javax.swing.plaf.basic.BasicTableUI$Handler.mouseR eleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Java MySql Select Statement

    First of all, use a PreparedStatement to be ABSOLUTELY sure that the content of those strings being cobbled into the statement are not part of the problem. Cobbling together statements like that is just BEGGING for problems, not to mention SQL Injection attacks.

Similar Threads

  1. Java MySQL Select Statement Query
    By Pita in forum JDBC
    Replies: 7
    Last Post: 03-08-2012, 07:03 PM
  2. Replies: 2
    Last Post: 01-21-2012, 06:34 PM
  3. using strings in select statement
    By Shryans in forum Eclipse
    Replies: 1
    Last Post: 12-20-2011, 08:32 PM
  4. MySQL/JDBC Prepared Statement Select query
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 02-12-2009, 06:29 PM
  5. MySQL/JDBC prepared statement problem
    By thelinuxguy in forum Advanced Java
    Replies: 3
    Last Post: 02-12-2009, 12:21 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •