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"));
}
}
}
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();
}
}
}
除了账号密码,数据库,改了的地方都加粗了,我这样是没问题的
已经解决了,谢谢大家
把整个java项目的名字换了就行了,不要用老师的jdbc_imooc。
如果你是就上面代码运行的话,那可能是因为没有main函数