繁体   English   中英

Java中的MySQL查询在数据库中不起作用

[英]mysql query in java not working in database

我这里有一个方法,应该从我的预设数据库中获取数据。 该表已经设置了信息。 但是我无法从Java程序中的表中检索数据。 其余的代码也可以像setter和getter一样正常工作。 我有另一种方法写入数据库中的特定行。 这工作正常,所以我知道eclipse和我的数据库之间存在连接。

我有的 :

public void readCard(int id){

  try{      
  java.sql.Statement statement=con.connection.createStatement();
  this.id=id;

  String gget;
  gget= "SELECT DISTINCT * FROM addresscard WHERE (id = \""+ 
  this.id +"\");"; 

  ResultSet rs= statement.executeQuery(gget);

  rs.next();

  this.id= rs.getInt(1);
  this.name= rs.getString(2);
  this.adress= rs.getString(3);
  this.postcode= rs.getString(4);
  this.place= rs.getString(5);
  this.telephone= rs.getString(6);

  System.out.println(this.id);

  }catch(Exception e){
  System.out.println(" did not read anything");
        }

我把String gget =“ SELECT DISTINCT * FROM adreskaart WHERE(id = \\”“ + this.id +” \\“);”;
在mysql中为:SELECT DISTINCT * FROM adreskaart WHERE id = 2;

选中与id 2对应的行。 所以我的数据库工作正常。 我的猜测是我在eclipse中的sql代码是错误的。 谁能看到为什么我无法从数据库中读取。

非常感谢

编辑在我changeCard()我应该能够在我的测试类像调整数据:Connectie CON =新Connectie(); AddressCard ac = new AddressCard();

    ak.readCard(2);
ak.setName("prof dr ir P chimp");   
ak.changeCard();

Vector tabel;

    tabel=ak.readAllCards();

当然还有所有的gui好东西。

public void changeCard(){

     try{
     java.sql.Statement statement= con.connection.createStatement();   

 String sset;  
 sset= "UPDATE adresKaart " + " SET naam = '" + this.naam + "', "     + "adres='" + this.adres + "'," +
         " postcode='" + this.postcode + "'," + "plaats='" +     this.plaats + "'," + "telefoon='" + this.telefoon
           + "' " + " WHERE adreskaart.id = " + this.id; 

         statement.executeUpdate(sset);      

     }catch(Exception e){
         System.out.println(" did not change anything");
         e.printStackTrace();
     } 
    }

似乎我的sql错误,但似乎无法弄清楚。 提前致谢 。

首先,请确保您的表名称。 是地址卡还是adreskaart?

如您所写,您的查询应为

SELECT DISTINCT * FROM addresscard WHERE id = 2

但是,现在是(加引号)

SELECT DISTINCT * FROM addresscard WHERE id = "2"

如果将id存储为整数,则不应使用引号。 如果id存储为varchar,则应该这样做。 此外,无需在查询末尾添加分号。 请尝试以下。

gget= "SELECT DISTINCT * FROM addresscard WHERE id = " + this.id;

我认为您的slq得到了arry的结果。因此,您应该查看该结果:

while(rs.next()){
   this.id= rs.getInt(1);
   this.name= rs.getString(2);
   this.adress= rs.getString(3);
   this.postcode= rs.getString(4);
   this.place= rs.getString(5);
   this.telephone= rs.getString(6);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM