eclipse java 连接mysql

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
import com.mysql.jdbc.Connection;

public class MySql {
    public static void main(String[]args) throws SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String url="jdbc:mysql://localhost:3306/mysql";
        String username="root";
        String password="root";
         
        Connection con=(Connection) DriverManager.getConnection(url,username,password);
        if(con!=null) {
         
            System.out.println("成功");
      }
        Statement stmt;
        ResultSet res;
        stmt=con.createStatement();
        res=stmt.executeQuery("select *from user");
        while(res.next()){
            String name=res.getString("User");
            System.out.println(name);
        }
        res.close();
    }

}

原文地址:https://www.cnblogs.com/dlutxm/p/2143442.html