刚开始出现了Access denied for user root@localhost (using password: YES)的错误,排了半天错原来是password和user内容写反了,寂寞。
package com.imooc.db;
import java.sql.*;
/**
*
*/
public class DBUtil {
private final static String URL="jdbc:mysql://127.0.0.1:3306/imooc?useUnicode=true&characterEncoding=utf-8";
private final static String PASSWORD="1234";
private final static String USER="root";
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获得数据库驱动
Connection con = DriverManager.getConnection(URL,USER,PASSWORD);
//3.通过数据库连接操纵数据库,实现增删该查
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select user_name,age from imooc_goddess");
while(rs.next()){
System.out.println(rs.getString("user_name")+","+rs.getInt("age"));
}
}
}