如何从 h2 数据库中获取字符串 (Java, DBUtil)
How to get a String from h2 database (Java, DBUtil)
这是来自 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 的值,然后将其放入字符串名称中。然后地址相同。我只知道如何执行语句。非常感谢任何帮助!
您需要将查询结果存储在 ResultSet
中,然后迭代该 ResultSet
。您可以在遍历变量时将 ResultSet
中的值分配给变量。
下面的代码是迭代 a ResultSet
的示例,正如我在下面的评论中提到的那样。
while (rs.next()) {
// ResultSet columns: 1 = UserName 2 = FirstName 3 = LastName 4 = Organization
userInfo[0] = rs.getString(1); // assign UserName result to variable
userInfo[1] = rs.getString(2); // assign First_Name result to variable
userInfo[2] = rs.getString(3); // assign Last_Name result to variable
userInfo[3] = rs.getString(4); // assign Organization result to variable
}
当没有 ResultSet
行可供迭代时,while 循环将结束。
这是来自 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 的值,然后将其放入字符串名称中。然后地址相同。我只知道如何执行语句。非常感谢任何帮助!
您需要将查询结果存储在 ResultSet
中,然后迭代该 ResultSet
。您可以在遍历变量时将 ResultSet
中的值分配给变量。
下面的代码是迭代 a ResultSet
的示例,正如我在下面的评论中提到的那样。
while (rs.next()) {
// ResultSet columns: 1 = UserName 2 = FirstName 3 = LastName 4 = Organization
userInfo[0] = rs.getString(1); // assign UserName result to variable
userInfo[1] = rs.getString(2); // assign First_Name result to variable
userInfo[2] = rs.getString(3); // assign Last_Name result to variable
userInfo[3] = rs.getString(4); // assign Organization result to variable
}
当没有 ResultSet
行可供迭代时,while 循环将结束。