在java中从数据库中检索值 [英] retrieving values from database in java

查看:69
本文介绍了在java中从数据库中检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a program that retrieves the inputted data/values of fields from the database I created. But when I'm running it the output is always null. I wonder what's wrong? Here's my code:

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;
}

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

In my main class I have this codes:

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

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

}
 }

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

I wonder why it always says null when I'm running it.

推荐答案

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

You are not actually "setting" anything in your setters.

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的理解是这样的,你可以在你的吸气。

On top of that: My understanding of getters and setter is such that you would do the query in your getter.

这篇关于在java中从数据库中检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆