准备工具

  Eclipse-oxygen-64位  http://pan.baidu.com/s/1gf48FIj

  MySQL-5.7.17   http://pan.baidu.com/s/1skD14Cl

  MySQL连接驱动   http://pan.baidu.com/s/1qYSCOQo

  SQLyogEnt 中文破解版开发工具   http://pan.baidu.com/s/1dF2ANol

第一步:在Java项目中新建一个lib文件夹,将下载好的JDBC驱动文件复制到该目录下

 

第二步:右键该文件,添加至构建路径  

第三步:创建一个类来测试连接,输入以下代码

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Connect 
{
    public static void main(String[] args) throws SQLException        //声明异常
    {
        Connection con = null;        //声明Connection(连接)对象
        String driver = "com.mysql.jdbc.Driver";        //声明驱动程序名
        String url = "jdbc:mysql://localhost:3306/world";        //指向要访问的数据库名 此处world代表需要连接的表名
        String user = "root";        //MySQL配置时的用户名
        String password = "loyer";        //MySQL配置时的密码
        
        try 
        {
            Class.forName(driver);        //加载驱动程序
            con=DriverManager.getConnection(url, user, password);        //用getConnection()方法,连接MySQL数据库!
            if(!con.isClosed())
            {
                System.out.println("成功连接到数据库!");
            }
        } 
        catch (ClassNotFoundException e)        //加载驱动程序的异常处理
        {
            e.printStackTrace();
        } 
        catch (SQLException e)        //连接数据库失败的异常处理
        {
            e.printStackTrace();
        }

        
//---------------------------------------测试基本SQL语句---------------------------------------
// 查看表数据,也可放在最后面运行,方便在控制台查看输出结果
// Statement statement = con.createStatement(); //创建statement类对象,用来执行SQL语句 // String s = "select * from city"; //声明SQL语句 // ResultSet rs = statement.executeQuery(s); //ResultSet类,用来存放获取的结果集 // System.out.println("进行SQL语句操作,结果如下:"); //打印提示语句 // System.out.println("---------------------------------------"); // while(rs.next()) //循环输出表信息 // { // String s1 = rs.getString("ID"); //获取ID这列数据 // String s2 = rs.getString("Name"); //获取Name这列数据 // String s3 = rs.getString("CountryCode"); //获取CountryCode这列数据 // String s4 = rs.getString("District"); //获取District这列数据 // String s5 = rs.getString("Population"); //获取Population这列数据 // System.out.println(s1+"\t"+s2+"\t"+s3+"\t"+s4+"\t"+s5+"\t"); //输出结果 // } // rs.close(); //关闭 // // //添加数据 // PreparedStatement ps1; // //预处理添加数据 // ps1 = con.prepareStatement("insert into city (ID,Name,CountryCode,District,Population)"+ "values(?,?,?,?,?)"); // ps1.setString(1,"10088"); //设置参数1,创建ID列的数据为“10086” // ps1.setString(2, "Jack"); //设置参数2,创建Name列的数据为“Jack” // ps1.setString(3, "ABC"); //设置参数3,创建CountryCode列的数据为“ABC” // ps1.setString(4, "Phone"); //设置参数4,创建District列的数据为“Phone” // ps1.setString(5, "666666"); //设置参数5,创建Population列的数据为“666666” // ps1.executeUpdate(); //执行更新 // ps1.close(); //关闭 // // //更新数据 // PreparedStatement ps2; // //预处理更新(修改)数据 // ps2 = con.prepareStatement("update city set Name = ? where ID = ?"); // ps2.setString(1,"Phone"); // ps2.setString(2,"1"); // ps2.executeUpdate(); // ps2.close(); // // //删除数据 // PreparedStatement ps3; // //预处理删除数据 // ps3 = con.prepareStatement("delete from city where ID > ?"); // ps3.setFloat(1, 1000); // ps3.executeUpdate(); // ps3.close(); // // con.close(); //关闭数据库 } }

第四步:准备一个数据包用基本的SQL语句测试连接并读取数据、这里采用MySQL数据库自带的World数据表

执行上述注释的SQL语句,在数据库中核实结果: