2017-08-09 9 views
0

はそれについて少し助けが必要です。例外に関するいくつかの問題があり、私はこのライブラリを使用することで非常に新しいです。事前に感謝:)HashMapを使用してPrepared Statement、例外

エラー:

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 ''common_translations.name' as nm JOIN ('common_translations.name_id' as nid)

マイコード:

private DatabaseConnection db; 
private final HashMap <String, String> statements; 

public DatabaseReader(DatabaseConnection db) { 
    statements = new HashMap<String, String>() { 
     private static final long serialVersionUID = -1827340576955092045L; 
    { 
     put("odds","vfl::%"); 
     put("common_translations", "vhc::%"); 
     put("common_translations","vdr::%"); 
     put("common_translations", "vto::%"); 
     put("common_translations","vbl::%"); 
     put("common_translations", "vf::%"); 
     put("odds","vsm::%"); 
     put("odds", "rgs::%"); 
     put("odds", "srrgs::%"); 
    }}; 

    this.db = db; 
} 

public void read() { 
    try { 
     Connection connection = db.connect(db.getUrl_common_translation()); 
     PreparedStatement ps = (PreparedStatement) connection.prepareStatement("SELECT nm.id, nid.key, nm.name FROM ? as nm JOIN (? as nid)\r\n" + 
       "     ON (nm.id = nid.id) where nid.key like ? and nm.typeId=8 and nm.sourceId=-1 and nm.languageCode='en'");   
     for(Entry <String,String> e : statements.entrySet()) { 
      ps.setString(1, e.getKey() + ".name"); 
      ps.setString(2, e.getKey() + ".name_id"); 
      ps.setString(3, e.getValue()); 
      ResultSet rs = ps.executeQuery(); 
      while(rs.next()) { 
       int id = rs.getInt("id"); 
       //String tag = rs.getString("tag"); 
       //String translation = rs.getString("translation");  
       System.out.println(id); 
      } 
     } 



    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
} 
+3

あなたは、テーブル名や列名を置き換えるために、 ''プレースホルダシステムを使用することはできません、それが唯一の値のために働く:

は、あなたがこのスニペットを使用することができますあなたの問題を解決します。 – Berger

+0

ありがとうございます。これをスマートな方法で行うためのアドバイスはありますか? –

+2

うまくいけば、 'StringBuilder'を使って、ターゲットテーブルに従ってクエリ文字列を構築することができます。 – Berger

答えて

1

あなたは間違った方法で列の名前を設定している、この:

ps.setString(1, e.getKey() + ".name"); 
ps.setString(2, e.getKey() + ".name_id"); 

ウィル入力を引用符で囲みます。

FROM "something.name" as 

これは誤った構文です。

は、代わりにあなたがこのようなプリペアドステートメントせずに直接名前を設定する必要があります。

Connection connection = db.connect(db.getUrl_common_translation()); 
PreparedStatement ps = (PreparedStatement) connection.prepareStatement(); 
for (Entry<String, String> e : statements.entrySet()) { 
    String query = "SELECT nm.id, nid.key, nm.name FROM " + e.getKey() + ".name" +" as nm " 
      //----------------------------------------------^__________________^ 
      + "JOIN (" + e.getKey() + ".name_id" + " as nid) ON (nm.id = nid.id) " 
      //-----------^_____________________^ 
      + "where nid.key like ? and nm.typeId=8 " 
      + "and nm.sourceId=-1 and nm.languageCode='en'"; 
    ps.setString(1, e.getValue()); 
    ResultSet rs = ps.executeQuery(query); 
    //------------------------------^^ 

をしかし、この前に名前を確認する必要があり、構文エラーを回避するために、(クエリを感染何かが含まれてはならない、とSQLインジェクション)、ビューのDBの観点から

0

前にいくつかのコントローラを作成する必要があるので、あなたは、クエリを実行しようとします:

SELECT nm.id, nid.key, nm.name 
    FROM :param1 as nm 
     JOIN (:param2 as nid) ON (nm.id = nid.id) 
where nid.key like :param3 and nm.typeId=8 and nm.sourceId=-1 and nm.languageCode='en' 

一部parameとters。しかし、パラメータとしてテーブルやビューを渡すことはできません。意味がない。 ?

String sql = "SELECT nm.id, nid.key, nm.name FROM %s as nm JOIN (%s as nid)\r\n" + 
      "     ON (nm.id = nid.id) where nid.key like ? and nm.typeId=8 and nm.sourceId=-1 and nm.languageCode='en'" 
for(Entry <String,String> e : statements.entrySet()) { 
    PreparedStatement ps = (PreparedStatement) connection.prepareStatement(
      String.format(sql, e.getKey() + ".name", e.getKey() + ".name_id") 
    );   
    ps.setString(1, e.getValue()); 
     .... your code here .... 
} 
+0

まあありがとう、これは私が探していたものです:)しかし、このコードはデータベースによって拒否されています: –

関連する問題