java-用main方法连接数据库测试

main{
try {
Connection con = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
// mysql驱动
con = (Connection) DriverManager.getConnection("jdbc:p6spy:mysql://xxx/xx", 
user, name);

Statement ps =  (Statement) con.createStatement();
String sql = "select * from xx";
ResultSet rs = ps.executeQuery(sql);
while (rs.next()) {
// 循环输出结果集
String account = rs.getString("account");
System.out.println("account:" + account);
}
} catch (Exception e) {
System.out.println("MYSQL error" + e.getMessage());
}

或者调用PreparedStatement子接口来进行数据库的连接


你可能感兴趣的:(java)