2017-04-20 66 views
1

我在將數據插入數據庫時​​遇到了一些問題。該數據是由一個CSV解析器讀取和改變數據除此之外,我不斷收到此錯誤信息:通過java向數據庫插入問題

Connected to the PostgreSQL server successfully. 
Naam van de garage: P_Erasmusbrug, Longditude: 4.482313155, Latitude: 51.91024645 
org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns: 0. 
at org.postgresql.core.v3.SimpleParameterList.bind(SimpleParameterList.java:65) 
at org.postgresql.core.v3.SimpleParameterList.setStringParameter(SimpleParameterList.java:128) 
at org.postgresql.jdbc.PgPreparedStatement.bindString(PgPreparedStatement.java:1023) 
at org.postgresql.jdbc.PgPreparedStatement.setString(PgPreparedStatement.java:374) 
at org.postgresql.jdbc.PgPreparedStatement.setString(PgPreparedStatement.java:358) 
at Database.ConnectDatabase.parser(ConnectDatabase.java:80) 
at Events.CSVReader.main(CSVReader.java:40) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 
Thank you for your service. 
Naam van de garage: P_St.Jacobsplaats, Longditude: 4.482054381, Latitude: 51.92410235 
Thank you for your service. 
Naam van de garage: P_Schouwburgplein, Longditude: 4.473618335, Latitude: 51.92102728 
org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns: 0. 

這對於數據的所有其他線路繼續。有沒有辦法解決這個問題,因爲我不明白錯誤信息包含哪些內容。 a,b2和c2是''name'',''londitude''和''latitude' 」。

package Database; 

import java.io.*; 
import java.sql.*; 
import java.util.HashMap; 
import java.sql.SQLException; 

public class ConnectDatabase { 
private final String url = "jdbc:postgresql://localhost/Project3"; 
private final String user = "postgres"; 
private final String password = "kaas123"; 
private Connection conn; 

public Connection connect() { 
    Connection conn = null; 
    try { 
     conn = DriverManager.getConnection(url, user, password); 
     System.out.println("Connected to the PostgreSQL server successfully."); 
    } catch (SQLException exception) { 
     System.out.println(exception.getMessage()); 
    } 

    this.conn = conn; 
    return conn; 
} 

public HashMap getGarages() { 
    HashMap<String, Double> newHashMap = new HashMap<String, Double>(); 
    try { 
     Statement stmt = conn.createStatement(); 
     ResultSet rs; 

     rs = stmt.executeQuery("SELECT deelgemeente, COUNT(garagenaam) FROM garages GROUP BY deelgemeente"); 
     while (rs.next()) { 
      String deelGemeenteNaam = rs.getString("deelgemeente"); 
      double garageNaamCount = rs.getDouble("COUNT"); 
      newHashMap.put(deelGemeenteNaam, garageNaamCount); 
     } 
    } catch (Exception e) { 
     System.err.println("Got an exception!"); 
     System.err.println(e.getMessage()); 
    } 
    return newHashMap; 
} 

public HashMap getTheftYear(int year) { 
    HashMap<String, Double> newHashMap = new HashMap<String, Double>(); 
    try { 
     Statement stmt = conn.createStatement(); 
     ResultSet rs; 

     rs = stmt.executeQuery("SELECT deelgemeente, percentagediefstal FROM autodiefstal WHERE jaar = " + year); 
     while (rs.next()) { 
      String deelGemeenteNaam = rs.getString("deelgemeente"); 
      double deelPercentage = rs.getDouble("percentagediefstal"); 
      newHashMap.put(deelGemeenteNaam, deelPercentage); 
     } 
    } catch (Exception e) { 
     System.err.println("Got an exception!"); 
     System.err.println(e.getMessage()); 
    } 
    return newHashMap; 
} 

public int parser(String a, float b2, float c2) { 
    int updated = 0; 
    Connection conn = null; 
    PreparedStatement stmt = null; 
    try { 

     conn = DriverManager.getConnection(url, user, password); 

     String insertSQL = "INSERT INTO testparser(garagenaam, xpos, ypos) VALUES(" + a + "," + b2 + "," + c2 + ")"; 
     stmt = conn.prepareStatement(insertSQL); 

     stmt.setString(1, a); 
     stmt.setFloat(2, b2); 
     stmt.setFloat(3, c2); 

     System.out.println("Inserted data into the database..."); 
     updated = stmt.executeUpdate(); 



    } catch (SQLException se) { 
     se.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      if (stmt != null) 
       conn.close(); 
     } catch (SQLException se) { 
     } 
     try { 
      if (conn != null) 
       conn.close(); 
     } catch (SQLException se) { 
      se.printStackTrace(); 
     } 
    } 
    System.out.println("Thank you for your service."); 
    this.conn = conn; 
    return updated; 

    } 
} 
+0

刪除不兼容MySQL的標籤 – Jens

回答

4

你沒有正確使用?佔位符系統,替換:

String insertSQL = "INSERT INTO testparser(garagenaam, xpos, ypos) VALUES(" + a + "," + b2 + "," + c2 + ")"; 

String insertSQL = "INSERT INTO testparser(garagenaam, xpos, ypos) VALUES(?,?,?)"; 
+1

太謝謝你了! :D''???''是我必須改變的,我忘了它會立即替換我的變量 –

+0

我會在接下來的9分鐘內接受你的回答,謝謝你的幫助=) –