String user = "root";
String password = "123456";
String host = "localhost:3306";
String database = "lportal";
Statement stmt;
String url ="";
Connection con = null;
url = "jdbc:mysql://" + host + "/" + database + "?useUnicode=true&characterEncoding=utf8";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
con = (Connection) DriverManager.getConnection(url, user, password);
stmt = (Statement) con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = null;
rs = (ResultSet) stmt.executeQuery("select * from user_");
while(rs.next()){
System.out.println(rs.getInt("userId"));
}
con.close();
stmt.close();
rs.close();
获取各个字段
while(rs.next()){
System.out.print("id", rs.getObject("id"));
System.out.print("type", rs.getObject("type").toString());
System.out.print("fn", rs.getObject("fn").toString());
System.out.print("sn", rs.getObject("sn").toString());
System.out.print("email", rs.getObject("email").toString());
}