0

我正在制作一个程序,从我创建的数据库中检索输入的字段数据/值。但是当我运行它时,输出始终为空。我想知道怎么了?这是我的代码:

import java.sql.*;

public class GuestsInfo
{
private String  firstName, lastName;
private int  age;
private Connection con;
private PreparedStatement statement;

public GuestsInfo()
{
    try {
         Class.forName("com.mysql.jdbc.Driver");
         con = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/3moronsdb","root","");
    } catch (Exception e) {
         e.printStackTrace();
    }
}
public GuestsInfo(String firstName, String lastName, int age)
{
    this();
    try
    {   
        statement = con.prepareStatement("Insert into guest(firstName,lastName,age)values(?,?,?)");

        statement.setString(1, firstName);
        statement.setString(2, lastName);
        statement.setInt(3, age);

        statement.executeUpdate();
        con.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
        return;
    }   
}


public void setFirstName(String firstName)
{

    try{
        statement= con.prepareStatement("SELECT * FROM guest WHERE firstName = ?");
        statement.setString(1, firstName);
        ResultSet rs = statement.executeQuery();
        while(rs.next()){
            firstName = rs.getString(1);
        }
    }catch(Exception e){
        System.out.print(e);
    }
}

public String getFirstName(){
    return firstName;
}

public void setLastName(String lastName)
{

    try{
        statement= con.prepareStatement("SELECT * FROM guest WHERE lastName = ?");
        statement.setString(2, lastName);
        ResultSet rs = statement.executeQuery();
        while(rs.next()){
            lastName = rs.getString(2);
        }
    }catch(Exception e){
        System.out.print(e);
    }
}

public String getLastName(){
    return lastName;
}

public void setAge(int age){
    try{
        statement = con.prepareStatement("SELECT * FROM guest WHERE age=?");
        statement.setInt(3, age);
        ResultSet rs = statement.executeQuery();
        while(rs.next()){
            age = rs.getInt(3);
        }
    }catch(Exception e){
        System.out.print(e);
    }
}

public int getAge(){
    return age;
}

在我的主要课程中,我有以下代码:

 public class TestMain {
public static void main(String [] args){

    GuestsInfo gi = new GuestsInfo();
    gi.setFirstName("Ericka");
    System.out.println(gi.getFirstName());

}
 }

我想知道为什么当我运行它时它总是说 null 。

4

3 回答 3

1

你实际上并没有在你的二传手中“设置”任何东西。

public void setFirstName(String firstName)
{

    try{
    // Here you create a READing statement.
        statement= con.prepareStatement("SELECT * FROM guest WHERE firstName = ?");
        statement.setString(1, firstName);
        ResultSet rs = statement.executeQuery();
        while(rs.next()){
    // Here you set the >>method param<< to a non-existing String
            firstName = rs.getString(1);
        }
    }catch(Exception e){
       System.out.print(e);
    }
}
// After the method is done you have done nothing because you didn't even set your class member variable "firstName", which you would have set using "this.firstName".

最重要的是:我对 getter 和 setter 的理解是,您可以在 getter 中进行查询。

于 2012-10-06T19:02:49.723 回答
0

您似乎没有在 main 方法中插入任何数据。您的无参数构造函数不会创建任何数据。setFirstName 方法使用了一个 select 语句,所以它不会这样做。数据库中是否已经有一个名为 Ericka 的用户?因为如果没有,您将没有任何数据可以返回。

我认为您的意思是使用带有多个参数的构造函数:

GuestsInfo gi = new GuestsInfo("Ericka", "Something", 30);
于 2012-10-06T18:58:47.363 回答
0
GuestsInfo gi = new GuestsInfo();

你没有在你的 0-arg 构造函数中填充你的数据库......你只是用它来初始化connection......所以,你也不会得到任何数据......

尝试通过调用您的 3-arg 构造函数来创建对象..

 GuestsInfo gi = new GuestsInfo("Ericka", "lastName", 23);
于 2012-10-06T18:59:19.563 回答