2014-03-13 4 views
0

누구나 다음 연결에 어떤 문제가 있는지 말해 줄 수 있습니까? 데이터베이스에 레코드를 삽입하려하고 있습니다. 몇 가지 이유 때문에 시도 할 때마다 다음 두 가지를 계속 인쇄합니다. 내 프로그램을 실행하려면 :쿼리 문자열 길이 만 출력

Stacktrace below: 
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '14:55:32 , SimpleX Error Notification, Error Simple X Queue, No Troubleshoo' at line 1 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
    at com.mysql.jdbc.Util.getInstance(Util.java:386) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2819) 
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811) 
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725) 
    at mypackagename.Myclassreader.call(Myclassreader.java:492) 
    at mypackagename.Myclassreader.call(Myclassreader.java:25) 
: 여기
Statement InsertRemoteResultsErrorLogStmt = null; 
ResultSet SelectErrorLogRS = null; 
Connection connRemoteforCatch = null; 

try { 

    String QueryStringInsertIntoErrorLogs = ""; 

    connRemoteforCatch = DriverManager.getConnection("jdbc:mysql://xx.xx.x.xxx:3306/test", MainUser, MainPass); 

    QueryStringInsertIntoErrorLogs = " INSERT INTO simplequeue.errorlogs " 
      + " (" 
      + " ErrorNumber_int, " 
      + " Created_dt, " 
      + " Subject_vch, " 
      + " Message_vch, " 
      + " TroubleshootingTips_vch, " 
      + " CatchType_vch, " 
      + " CatchMessage_vch, " 
      + " CatchDetail_vch, " 
      + " Host_vch, " 
      + " Referer_vch, " 
      + " UserAgent_vch, " 
      + " Path_vch, " 
      + " Querystring_vch " 
      + ") " 
      + " VALUES " 
      + " (" 
      //+ " NULL, " // ErrorLogIdInt 
      + " 2002 , " // ErrorNumber_int 
      + " 2014-03-12 14:55:32 , " 
      + " SimpleX Error Notification, " 
      + " Error Simple X Queue, " 
      + " No Troubleshooting Tips Specified, " 
      + " testgetsqlstate , " 
      + " testgetmessage ," 
      + " NULL, " 
      + " ebmdevii.messagebroadcast.com," 
      + " NULL, " 
      + " CFSCHEDULE, " 
      + " NULL, " 
      + " DBSOURCE=BishopDev, " 
      + ") "; 

    System.out.println("the length of the Query String for Error is :" + QueryStringInsertIntoErrorLogs.length()); 

    InsertRemoteResultsErrorLogStmt = connRemoteforCatch.createStatement(); 
    InsertRemoteResultsErrorLogStmt.executeUpdate(QueryStringInsertIntoErrorLogs); 

} catch (SQLException ex1) { 
    System.out.println("Stacktrace below:"); 
    ex1.printStackTrace(); 
    System.out.println("Value of Catch inside Catch with SQLException ex1:" + connRemoteforCatch.isValid(2)); 

} 

내 스택 트레이스입니다 다음과 같이

the length of the Query String for Error is :506 
Value of Catch inside Catch with SQLException ex1:true 

내 코드입니다

첫 줄에 SQL 구문 오류가 있지만, 동일한 SQL 쿼리를 MySQL Workbench에서 사용하고 성공적으로 레코드를 삽입했습니다. 그리고 Java 프로그램에서 구문 오류가없는 것 같습니다.

Stacktrace below: 
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Error Notification, Error Simple X Queue, No Troubleshooting Tips Specified, ' at line 1 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 

최근 스택 트레이스 :

업데이트 스택 트레이스를 알려 주시기 바랍니다

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
at com.mysql.jdbc.Util.getInstance(Util.java:386) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237) 
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169) 
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617) 
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778) 
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2819) 
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811) 
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725) 
at ebmextractor.Myclassreader.call(Myclassreader.java:493) 
at ebmextractor.Myclassreader.call(Myclassreader.java:25) 
at javafx.concurrent.Task$TaskCallable.call(Task.java:1259) 
at java.util.concurrent.FutureTask.run(FutureTask.java:262) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
at java.lang.Thread.run(Thread.java:744) 
+2

주석 처리 대신 스택 추적을 추가하면 문제가 무엇인지 알 수 있습니다. 인쇄 줄을 주석 처리하고 스택 추적의 주석을 제거하고 결과를 게시하십시오. – Acapulco

+1

스택 트레이스를 보여줄 수 있습니까? uncomment'ex1.printStackTrace(); ' – Incognito

+0

['Connection # isValid'] (http://docs.oracle.com/javase/7/docs/api/java/sql/Connection)에서이 문서를 읽어야한다고 생각합니다. .html # isValid (int)) 이것이 당신에게 무엇을 말하고 있는지 이해할 수 있습니다. – MadProgrammer

답변

1

NULL 및 숫자 값을 제외하고 작은 따옴표로 삽입하려는 값을 포함하십시오.

PROTIP : System.out.println을 사용하여 최종 쿼리 문자열을 확인한 다음 MySQL에서 실행 해 봅니다. JDBC 오류 메시지의 혼잡을 줄입니다.

+0

당신은'System.out.println ("Query String :"+ QueryStringInsertIntoErrorLogs);와 같은 것을해야한다는 것을 의미합니까? –

+0

멋진 팁, 팁으로 인해 가장 최근의 오류를 해결할 수있었습니다. 추가 팁을 가져 주셔서 감사합니다. 내 SQL 쿼리의 마지막 열 값 뒤에 쉼표가 추가되었습니다. –

2

내가 MySQL을 몰라,하지만 아마도 당신의 날짜는 적어도 따옴표에 있어야합니다 - 가능성도 첨부 된 캐스트가 있습니다.

  + " '2014-03-12 14:55:32' , " 

편집 : this page에서 살펴보면 나는 그것이처럼 볼 수 있었다 추측하고있다 :

+ " TIMESTAMP '2014-03-12 14:55:32' , " 

하지만 난 당신이

편집에 삽입하고있는 컬럼의 유형을 모르는 2 :

Emanual 덕분에 다른 문자열을 따옴표로 묶어야한다고 덧붙이고 싶습니다.

,451,515,
QueryStringInsertIntoErrorLogs = " INSERT INTO simplequeue.errorlogs " 
     + " (" 
     + " ErrorNumber_int, " 
     + " Created_dt, " 
     + " Subject_vch, " 
     + " Message_vch, " 
     + " TroubleshootingTips_vch, " 
     + " CatchType_vch, " 
     + " CatchMessage_vch, " 
     + " CatchDetail_vch, " 
     + " Host_vch, " 
     + " Referer_vch, " 
     + " UserAgent_vch, " 
     + " Path_vch, " 
     + " Querystring_vch " 
     + ") " 
     + " VALUES " 
     + " (" 
     //+ " NULL, " // ErrorLogIdInt 
     + " 2002 , " // ErrorNumber_int 
     + " '2014-03-12 14:55:32', " 
     + " 'SimpleX Error Notification', " 
     + " 'Error Simple X Queue', " 
     + " 'No Troubleshooting Tips Specified', " 
     + " 'testgetsqlstate' , " 
     + " 'testgetmessage' ," 
     + " NULL, " 
     + " 'ebmdevii.messagebroadcast.com'," 
     + " NULL, " 
     + " 'CFSCHEDULE', " 
     + " NULL, " 
     + " 'DBSOURCE=BishopDev', " 
     + ") "; 
+0

어쩌면 공백이 문제를 일으키는 것입니까? 나는 MySQL의 전문가는 아니지만 jdbc 드라이버가 공백을 잘못 파싱 한 것일까? – Acapulco

+0

날짜를 '+' '2014-03-12 14:55:32'로 변경하면 내 스택 트레이스가 업데이트 된 게시물에 표시된 것처럼 두 줄로 줄어 들었습니다. –

+1

@Jeff - 거의 없지만 OP는 날짜뿐만 아니라 삽입되는 다른 문자열을 포함해야합니다. –

1

예외는 SQL 쿼리의 잘못된 형성에 기인,

봅니다 예에 대한

, 단일있는 qoute에 문자열을 넣어

+"'ebmdevii.messagebroadcast.com'"+