2016-05-28 5 views
1

はねえ、私はこのエラーメッセージを持っている人は、私は、サーバとクライアントの間でMySQLサーバからデータを選択し接続して、mysqlのクライアントでそれを挿入しますが、insert文はcom.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:SQL構文にエラーがあります。

com.mysql.jdbc.exceptions.jdbc4が起こるのではありません。 MySQLSyntaxErrorException:SQL構文にエラーがあります。あなたのMySQLサーバのバージョンに対応するマニュアルをチェックして正しい文法を確認してください。文脈の近くで使用してください.login VALUES(1、 'Alamal'、 'alamal'、 'alamal'、 'alamal')1行目

これは、コード..です

<% 
try{ 
Connection con1; 
Connection con2; 
Class.forName("com.mysql.jdbc.Driver"); 
con1=(Connection)DriverManager.getConnection("jdbc:mysql://192.168.101.1:3306/context","hospital","0000"); 
PreparedStatement ps1=(PreparedStatement)con1.prepareStatement("SELECT * from hospital"); 
String str; 
ResultSet rs1=ps1.executeQuery(); 

while(rs1.next()){ 
con2=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/context","root",""); 
con2.setAutoCommit(true); 
con2.createStatement(); 
    int id=rs1.getInt("ID"); 
    String username=rs1.getString("UserName"); 
String password=rs1.getString("Password"); 
String hname=rs1.getString("HospitalName"); 
    String haddress=rs1.getString("HospitalAddress"); 
    PreparedStatement state= (PreparedStatement)con2.prepareStatement("INSERT INTO 'context'.'login' VALUES(?,?,?,?,?);"); 
    state.setInt(1, id); 
    state.setString(2, username); 
    state.setString(3, password); 
    state.setString(4, hname); 
    state.setString(5, haddress); 
    state.executeUpdate(); 
    con2.close();  
    } 
    con1.close(); 

} catch(Exception ex){ 
    out.print(ex); 
} 
%> 
+0

「コンテキスト」から一重引用符を削除するとどうなりますか? –

+1

このメッセージが表示されます。com.mysql.jdbc.exceptions.jdbc4MYSQLSyntaxErrorException:テーブル 'context.login'が存在しません – toomy

+0

それが存在するかどうか確認してください。 –

答えて

0

あなたは今、それが正常に動作します書き込みSQLクエリの愚かな間違いを持っ​​ています。

<% 
try{ 
Connection con1; 
Connection con2; 
Class.forName("com.mysql.jdbc.Driver"); 
con1=(Connection)DriverManager.getConnection("jdbc:mysql://192.168.101.1:3306/context","hospital","0000"); 
PreparedStatement ps1=(PreparedStatement)con1.prepareStatement("SELECT * from hospital"); 
String str; 
ResultSet rs1=ps1.executeQuery(); 

while(rs1.next()){ 
con2=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/context","root",""); 
con2.setAutoCommit(true); 
con2.createStatement(); 
    int id=rs1.getInt("ID"); 
    String username=rs1.getString("UserName"); 
String password=rs1.getString("Password"); 
String hname=rs1.getString("HospitalName"); 
    String haddress=rs1.getString("HospitalAddress"); 
    PreparedStatement state= (PreparedStatement)con2.prepareStatement("INSERT INTO `context`.`login` VALUES(?,?,?,?,?);"); 
    state.setInt(1, id); 
    state.setString(2, username); 
    state.setString(3, password); 
    state.setString(4, hname); 
    state.setString(5, haddress); 
    state.executeUpdate(); 
    con2.close();  
    } 
    con1.close(); 

} catch(Exception ex){ 
    out.print(ex); 
} 
%> 
+0

ありがとう、私は一言引用が間違いであるとは信じられません。 – toomy

+0

ようこそ、それは私の喜びです。 –

0

SQLの引用符(')は、オブジェクト名ではなく文字列リテラルを示すために使用されます。ちょうどそれらを落として、あなたはうまくいくはずです。

INSERT INTO context.login VALUES(?,?,?,?,?) 
関連する問題