【发布时间】:2018-02-01 19:05:43
【问题描述】:
这是来自 DBUtil 的代码
public class DBUtil {
static Connection conn = null;
public static Connection getConnected() {
try {
Class.forName("org.h2.Driver");
conn = DriverManager.getConnection("jdbc:h2:tcp://localhost/~/test", "sa", "");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}//end method
}//end DBUtil
这是我尝试使用的方法的代码:
private class SearchForEdit implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
String clientNumber = tfieldMiniSearchNumber.getText();
int number;
conn = DBUtil.getConnected();
if(conn == null) return;
String names=null, address=null;
String sql ="select names from clients where client_number = ?;";
String sql2 ="select address from clients where client_number = ?;";
try {
state = conn.prepareStatement(sql);
state.setInt(1, number);
state.execute();
state = conn.prepareStatement(sql2);
state.setInt(1, number);
state.execute();
labelWarning.setForeground(new Color(22, 205, 5));
labelWarning.setText("<html>TEXT SUCCESS</html>");
//names = select names from clients where client_number = number;
//address = select address from clients where client_number = number;
//if(names!=null && address!=null)
//tfieldEditClientNumber to be number
tfieldEditClientNumber.setText(""+number);
//tfieldEditNames to be names, where names = select names from clients where client_number = number;
//tfieldEditAddressда to be address, where address = select address from clients where client_number = number;
} catch (SQLException e1) {
labelWarning.setForeground(Color.RED);
labelWarning.setText("<html>Try again later</html>");
e1.printStackTrace();
}
}
}//end
我想从数据库中取出 NAME 列的值,然后输入字符串名称。然后地址也一样。我只知道如何执行语句。非常感谢任何帮助!
【问题讨论】:
标签: java database h2 statements