java - 通过java将问题插入数据库

标签 java postgresql jdbc

我在向数据库中插入一些数据时遇到了一些问题。 CSV 解析器正在读取数据并将其更改为数据,除此之外,我继续收到此错误消息:

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;

    }
}

最佳答案

您没有正确使用 ? 占位符系统,替换为:

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

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

关于java - 通过java将问题插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43516152/

相关文章:

java - 列名中包含 "?"的 JDBCPreparedStatement

java - 如何使用ibatis注释进行批量插入

java - JDBC-接口(interface)的实现

java - 使用 MySQL 存储和检索 Word 文档

java - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : Unknown database 'xyz' . hbm2ddl.auto=更新不工作

django - 在数据库中存储对 AWS S3 对象的引用的最佳实践?

sql - 查询通过键值对链接到实际匹配条件的记录的记录

java - stdin 数据流的第一个字节是否会触发 bash 命令运行?

java - Apache POI 超链接无法在 Microsoft Excel 中打开

php - postgres/pdo 查询绑定(bind)值错误