问答详情
源自:2-2 JDBC实战---打通数据库

错误: 找不到或无法加载主类 com.imooc.db.DBUtil实在不知道怎么回事???

package com.imooc.db;


import java.sql.*;

public class DBUtil {

public static void main(String[] args) throws Exception{

   final String URL = "jdbc:mysql://127.0.0.1:3306/imooc";

final String USER = "root";

final String PASSWORD = "duzhaoteng110";

Class.forName("com.mysql.jdbc.Driver");

Connection connection = DriverManager.getConnection(URL,USER,PASSWORD);

Statement statement = connection.createStatement();

ResultSet resultset = statement.executeQuery("select * from imooc_goddess");

while(resultset.next()){

System.out.println(resultset.getString("user_name")+","+resultset.getInt("age"));

}

}

}


提问者:剑殇 2017-04-30 21:41

个回答

  • 冰封一切1
    2017-05-02 16:47:45
    已采纳

    package jdbc_test1.com.mcr.test;


    import java.sql.*;

    public class test {

    public static void main(String[] args) {

    final String URL = "jdbc:mysql://127.0.0.1:3306/mcr?useUnicode=true&characterEncoding=utf-8";

    final String USER = "root";

    final String PASSWORD = "123456";

    try{

    Class.forName("com.mysql.jdbc.Driver");

    Connection connection = DriverManager.getConnection(URL,USER,PASSWORD);

    Statement statement = connection.createStatement();

    ResultSet resultset = statement.executeQuery("select * from test1");

    while(resultset.next()){

    System.out.println(resultset.getString("user_name")+","+resultset.getInt("age"));

    }

    } catch (ClassNotFoundException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    } catch (SQLException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }


    }

    }

    除了账号密码,数据库,改了的地方都加粗了,我这样是没问题的

  • 剑殇
    2017-08-25 13:01:33

    已经解决了,谢谢大家

  • 地狱炎黄
    2017-07-09 19:12:51

    把整个java项目的名字换了就行了,不要用老师的jdbc_imooc。

  • 冰封一切1
    2017-05-02 16:36:50

    如果你是就上面代码运行的话,那可能是因为没有main函数