Java 如何从我的SQL数据库中获取信息

Java 如何从我的SQL数据库中获取信息,java,mysql,sql,Java,Mysql,Sql,我尝试从GUI中获取artikelID的目的地(zielot)。不幸的是,我总是收到错误消息 com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:未知列 “where子句”中的“id” 在我的数据库中有一个ID为1234的ArtikelID。在我的测试课上,我试图获得我的ArtikelID1234的目的地(zielot)。因此,我应该得到E1.R10.D2,但是我得到了错误消息 我希望你能帮我找到我的错误 **Test Class:** pub

我尝试从GUI中获取
artikelID
目的地(zielot)
。不幸的是,我总是收到错误消息

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:未知列 “where子句”中的“id”

在我的数据库中有一个ID为1234的ArtikelID。在我的测试课上,我试图获得我的
ArtikelID
1234的
目的地(zielot)
。因此,我应该得到
E1.R10.D2,
但是我得到了错误消息

我希望你能帮我找到我的错误

**Test Class:**
public static void main(String[] args) {
        // TODO Auto-generated method stub
        //DBConnect connect = new DBConnect();
        //String s = connect.getLoginDaten(1);
        //System.out.print(s);


    DBConnect connect = new DBConnect();
    String s= connect.getZielort(1234);
    System.out.print(s);
}


**DB-Connect Class:**
public String getZielort(int arikel_ID) {
        String zielort="";
        try {
            String query = "select zielort from warenliste where id="+arikel_ID;
            rs= st.executeQuery(query);
            while (rs.next()) {
                zielort = rs.getString("zielort");  
            }

        }
        catch(Exception e) {
            System.out.println(e);
        }
        return zielort;
    }

    public int getArtikel(int arikel_ID) {
        int artikelid=0;
        try {
            String query = "select Artikel_ID from warenliste where id="+arikel_ID;
            rs= st.executeQuery(query);
            while (rs.next()) {
                artikelid = rs.getInt("Artikel_ID");    
            }

        }
        catch(Exception e) {
            System.out.println(e);
        }
        return artikelid;
    }

该异常意味着您的表没有名为
id
的列。根据你的询问

"select Artikel_ID from warenliste where id="+arikel_ID;
我推断coulmn被称为
Artikel\u ID
,因此您的查询应该如下所示:

"select Artikel_ID from warenliste where Artikel_ID="+arikel_ID;
当然,您可能有不同的模式。查看您的数据库,查看表
warenste
的列是如何命名的,然后用实际的列名替换
Artikel\u ID


顺便说一句,如果您使用的是用户输入,请查看安全原因(如SQL注入)。

出于安全原因,您甚至不应该使用MySQL用户名和密码,这是用Java硬编码的,可以进行非常复杂的反编译。。但是您应该使用带有套接字的网关,或者使用REST/SOAP/XML或JSON RPC/GrapQL协议的webservice访问。。。