netbeans DataBase检索数据并将其显示为另一个jframe文本字段

时间:2015-05-14 05:37:43

标签: 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 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;
}

1 个答案:

答案 0 :(得分:1)

  1. 使用PreparedStatement而不是Statement。有关详细信息,请参阅Using Prepared Statements
  2. 从您需要的查询中返回值
  3. 例如......

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

    这只返回具有匹配名字的客户的姓氏。如果有多个,那么只返回第一个结果。

    您可能还希望了解The try-with-resources Statement有关更好资源管理的一些想法