Kingsman
Kingsman

Reputation: 11

netbeans DataBase retrieve data and display it another jframe textfield

i want to retrieve my data and i want to display it another jframe . how to pass values to that point .

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

Upvotes: 2

Views: 16166

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347314

  1. Use PreparedStatement over Statement. See Using Prepared Statements for more details
  2. Return the value from the query which you need

For example...

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

This simply returns the last name for the customer with the matching first name. If there are more then one, then only the first result is returned.

You might like to also have a look at The try-with-resources Statement for some ideas about better resource management

Upvotes: 1

Related Questions