2013-08-14 3 views
0

3 개의 키 값으로 더비 데이터베이스를 검색 할 수 있어야합니다. GUI를 설정하고 데이터베이스의 데이터로 채울 수 있습니다. 어느 방향으로나 스크롤 할 수 있도록 설정했습니다. 내가 겪고있는 문제는 쿼리가 실패한 주요 필드 중 하나에 대한 특정 검색을 수행하는 메소드를 만들 때입니다. 다음은 검색을 용이하게하기 위해 사용하려는 방법입니다. 문을 사용하여 시도하고 문자열 문에 변수를 연결하고 준비된 문을 사용하여 시도하고 변수를 바인딩했습니다. 어느 쪽도 나는 일하게 될 수 없다. 오류가 발생한 코드에 표시했습니다.매개 변수 JtextField에서 java/jdbc를 사용하여 전달

public static void Search(){ 
    String idField = InitGUI.getidField().getText(); 
    String sql = ("SELECT * FROM MYDB.Employee WHERE Employee ID = '"+idField +"'"); 

    try { 

//  prepStat = dbCon.prepareStatement(sql); 
//  prepStat.setString(1, idField); 
//  rs = prepStat.executeQuery(); 
    rs = stmt.executeQuery(sql);  <== This is where the error occurs. 
    SqlStatements.SearchResult(rs.getRow()); 
    } catch (SQLException e) { 

     e.printStackTrace(); 
    } 
} 

다음은 스택 추적입니다.

Connected to database 
java.sql.SQLSyntaxErrorException: Syntax error: Encountered "ID" at line 1, column 44. 
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source) 
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source) 
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source) 
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source) 
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source) 
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source) 
at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source) 
at org.apache.derby.impl.jdbc.EmbedStatement.executeQuery(Unknown Source) 
at Scheduler.DBConnector.Search(DBConnector.java:77) 
at Scheduler.myActionListener.actionPerformed(myActionListener.java:20) 
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 
at javax.swing.DefaultButtonModel.setPressed(Unknown Source) 
at javax.swing.plaf.basic.BasicButtonListener.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(Unknown Source) 
at java.awt.LightweightDispatcher.dispatchEvent(Unknown 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$400(Unknown Source) 
at java.awt.EventQueue$2.run(Unknown Source) 
at java.awt.EventQueue$2.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(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.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 
Caused by: java.sql.SQLException: Syntax error: Encountered "ID" at line 1, column 44. 
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) 
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source) 
... 46 more 
Caused by: ERROR 42X01: Syntax error: Encountered "ID" at line 1, column 44. 
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source) 
at org.apache.derby.impl.sql.compile.ParserImpl.parseStatement(Unknown Source) 
at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source) 
at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source) 
at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source) 
... 40 more 
+2

가) 어떤 오류가 발생) 귀하의 열 이름 Employee ID하면

, 큰 따옴표 또는 역 인용 부호로 탈출, 당신의 DBMS에 따라 달라집니다? stacktrace가 있으면 추가하십시오. b) Employee.ID가 데이터베이스의 문자열 필드입니까? 만약 당신이 정수로 변환해야 할 수도 있습니다 (즉, 첫 번째 쿼리에서 따옴표를 사용하지 않거나 다른 방법으로'preStat.setInteger()'사용) –

+0

'e.printStackTrace()'의 출력을 게시하십시오 –

+0

Nit Pick -'idField'를'Search' 메소드에 전달해야합니다. 'PreparedStatement'를 사용해야합니다. [Java 명명 규칙] (http://www.oracle.com/technetwork/java/codeconv-138413.html) – MadProgrammer

답변

3

당신은 EmployeeID 사이의 .을 잊지 마세요?
"SELECT * FROM MYDB.Employee WHERE Employee.ID = "+idField

stacktrace Syntax error은 SQL 구문이 잘못되었음을 나타냅니다. Encoutered "ID" at line 1EmployeeID 사이의 구문 분석에 실패했음을 의미합니다.

+0

임 전체 데이터베이스에 새로운 점이 있습니다. 직원 ID 열에 공백이있는 것처럼 보입니다. 그게 가능하지 않니? – Keith

+0

열을 확인한 후 직원 ID에는 실제로 공간이 있습니다. 이게 부적절한가요? – Keith

+0

@Keith 어떤 데이터베이스가 있습니까? 열 이름을 "인용"해야 할 수도 있습니다 – MadProgrammer