(JAVA项目)连接数据库 报错Exception in thread "main" : .

时间:2024-11-16 17:45:54
package ;
import ;
import;
import;
import;
 
publicclass DBUtil {
      
       //创建三个常量来存放地址、用户名、和密码   ctrl +shift+x   变为大写
       privatestaticfinal String URL="jdbc:mysql://127.0.0.1:3306/imooc";
       privatestaticfinal String NAME="root";
       privatestaticfinal String PASSWORD="5220";
      
                                                                             //这里我们首先抛出一个异常
       publicstaticvoid main(String[] args) throws Exception {
              //1.加载驱动程序
              Class.forName("");
              //2.获取数据库的连接   (数据库的地址、用户名 、密码来连接我们的数据库)
              Connectionconn=DriverManager.getConnection(URL, NAME, PASSWORD);
              //3.通过数据库的连接操作数据库,实现增删改查
              stmt =conn.createStatement();
              ResultSetrs=stmt.executeQuery("selectuser_name,age from imooc_goddess");
              while(rs.next()) {
                     System.out.println(rs.getString("user_name")+","+rs.getInt("age"));
              }
       }
 
}