Chọn(Select) dữ liệu từ một bảng trong JSP
Đoạn mã sau sẻ lấy một record ra từ bảng dữ liệu. Code này đầy trên Net mình chỉ sưu tầm và chỉnh lại tí thôi. Chỉ mang tính tham khảo!!!
String id = cust_id.getText();
try
{
PreparedStatement prepstmt;
boolean found = false;
prepstmt = theConn.dbConn.prepareStatement
(“select custName, CustAddr from tCust where custId = ?”);
prepstmt.setString(1, id);
ResultSet rs;
rs = prepstmt.executeQuery();
found = rs.next();
if (found)
System.out.println(rs.getString(1));
else
System.out.println(“Customer ” + id + ” not found!”);
prepstmt.close();
}
catch (Exception e)
{
e.printStackTrace();
}
Để lấy nhiều mẫu tin hơn, hãy áp dụng tương tự như đoạn mã sau:
String name = cust_name.getText();
try
{
Statement stmt;
String sql;
sql = “select custName from tCust where custName = ” += “‘” + name + “‘”;
stmt = createStatement();
ResultSet rs;
rs = stmt.executeQuery();
while (rs.next())
{
System.out.println(rs.getString(“custName”));
}
stmt.close();
rs.close();
}
catch (Exception e)
{
e.printStackTrace();
}

Comment mới nhất