Java netbeans数据库检索数据并将其显示在另一个jframe文本字段中

Java netbeans数据库检索数据并将其显示在另一个jframe文本字段中,java,netbeans,Java,Netbeans,我想检索我的数据,并在另一个jframe中显示它。如何将值传递到该点 public void connectDb() { try { //setting up driver for Java Derby - select the Driver Class from connection properties Class.forName("org.apache.derby.jdbc.ClientDriver"); //setting up

我想检索我的数据,并在另一个jframe中显示它。如何将值传递到该点

public void connectDb() {
    try {
        //setting up driver for Java Derby - select the Driver Class from connection properties
        Class.forName("org.apache.derby.jdbc.ClientDriver");

        //setting up Database connection through DriverManager.getConnection(dbUrl, DBusername, DBpwd)
        con = DriverManager.getConnection(
                        "jdbc:derby://localhost:1527/dbshop", //DB URL from connection properties
                        "apiit", //DB username you gave when creating db
                        "apiit");   //DB password you gave when creating db

        //create java.sql.Statement from db connection to execute SQL queries
        stmt = con.createStatement();
    } catch (ClassNotFoundException ex) {

    } catch (SQLException ex) {
        System.out.println("Error in file");
    }
}//end connectDb()

public void updatePassword(String u) {
    boolean flag = false;
    try {

        //Sql UPDATE         column name   tabl name             clmn          equlng vrbl
        String sql = "select lastname from CUSTOMERDETAILS where FIRSTNAME='" + u + "'";

        //if the update query was successful it will return the no. of rows affected
        ResultSet rs = stmt.executeQuery(sql);
        System.out.println(sql);
    } catch (SQLException ex) {
        System.out.println("Error in updatePasswrd");
    }
    // return flag;
}
  • 语句
    上使用
    PreparedStatement
    。有关更多详细信息,请参阅
  • 从查询中返回所需的值
  • 例如

    public String getLastName(String u) throws SQLException {
        String lastName = u;
        try (PreparedStatement ps = con.prepareStatement("select lastname from CUSTOMERDETAILS where FIRSTNAME=?")){
            ps.setString(1, u);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    lastName = rs.getString(1);
                }
            }
        }
        return lastName;
    }
    
    这只是返回客户的姓氏和匹配的名字。如果有多个,则只返回第一个结果


    您可能还想了解一些关于更好的资源管理的想法

    没关系,但我想在另一个类中执行此操作,我如何将这些值传递到另一个JFrame?也许你应该有一个中央数据工厂,你可以从任何地方查询,那么为什么你不能调用getLastName方法并将结果应用到文本字段呢?