示例
id name surname
0 Alex A
1 Mark B
2 Bill C
假设我想在Java中获得id等于1的名称和姓氏,我如何获得每列的值
使用Statement或PreparedStatement,您可以检索如下字段,前提是您应该在项目中包含jdbc库
try{
Class.forName("com.mysql.jdbc.Driver"); // loading driver class
Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Database_name", username,password); // getting connection may change depending on database you are using
PreparedStatement ps = cn.prepareStatement("select name,sirname from user where id = ?");
ps.setString(1,id);
ResultSet rs = ps.executeQuery();
int i=0;
while(rs.next())
{
String name = rs.getString("name");
// retrieve your fields
}
}catch(Exception e)
{
e.printStackTrace();
}