【发布时间】:2016-11-27 15:53:28
【问题描述】:
例子
id name surname
0 Alex A
1 Mark B
2 Bill C
假设我想在Java中获取id等于1的姓名和姓氏,如何获取每一列的值
【问题讨论】:
例子
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();
}
【讨论】:
cn 是什么。但是,由于 StackOverflow 不是教学网站,所以无论如何这都是题外话。