java netbeans 数据库检索数据并将其显示在另一个 jframe 文本字段中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30229991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
netbeans DataBase retrieve data and display it another jframe textfield
提问by Kingsman
i want to retrieve my data and i want to display it another jframe . how to pass values to that point .
我想检索我的数据,我想显示另一个 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;
}
回答by MadProgrammer
- Use
PreparedStatement
overStatement
. See Using Prepared Statementsfor more details - Return the value from the query which you need
- 使用
PreparedStatement
过Statement
。有关更多详细信息,请参阅使用准备好的语句 - 从您需要的查询中返回值
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 Statementfor some ideas about better resource management
您可能还想查看try-with-resources 声明,了解有关更好的资源管理的一些想法