eclipse连接mysql数据库

利用eclipse连接数据库 
第一步:先下载mysql 
网址是:https://dev.mysql.com/downloads/installer/ 
下载之前需要登录,这里我下载的是第一个。点击download即可完成下载
第二步:下载完成后就可以安装了 
1.双击下载的mysql-installer-web-community-5.7.17.0.msi文件 
进行安装。 
2.安装过后打开cmd输入net srart mysql57 
出现以下就表明mysql安装成功 
第三步:下载jdbc 

1.这是下载jdbc的网址:

https://www.mysql.com/products/connector/点击download下载 

2. 这里我选择下载了第一个,具体为什么我也不知道。最后测试可以
第四步:下载成功后这里就要使用eclipse连接数据库了 
1.Window-preferences-java-Build Path-User Libraries 
 
2.点击右侧的new按钮, 
 
3.在这里输入jdbc,选中对勾,点击ok 
 
4.回到上一级界面,点击Add External JARs,打开到你的jdbc存放的目录,打开-ok。 

5.接下来是项目导入jar包,项目右键-Build Path-Configure Build Path 
 
6.点击右侧Add Library… -User Library-Next。打上对勾点击finish 
 
7.回到上一级界面就可以看到你添加的jdbc,点击Apply再点击ok。 
 
8.这样在你的项目下就可以看到你导入的jdbc了 
 
第五步就是利用cmd打开mysql创建一个表 
1.打开cmd输入mysql -uroot -p123456 
 
2.然后再输入show databases; 
这里记着一定要加分号;英文状态下的分号。 
 
3.创建一个数据库 
create database user_cmx; 
创建一个表 
use user_cmx; 
为数据库表添加字段 
create table user(name varchar(20),age varchar(10));

 
第六步:创建完成后,我们就可以使用eclipse连接数据库了。 
最后一步就是我们的代码了

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Demo {
    // 加载数据库驱动 //用最新的驱动 所以加了cj   com.mysql.cj.jdbc.Driver 
        private static String dbdriver = "com.mysql.cj.jdbc.Driver";
        // 获取mysql连接地址, cmxDatabaseName 是你的数据库的名称,需要添加时区 serverTimezone=GMT  
        private static String dburl = "jdbc:mysql://127.0.0.1:3306/cmxDatabaseName?&useSSL=false&serverTimezone=GMT";
        // 数据名称
        private static String username = "root";
        // 数据库密码
        private static String userpassword = "123456";
        // 获取一个数据的连接
        public static Connection conn = null;
        // 获取连接的一个状态

        public static void main(String[] args) throws SQLException {
            List<List<Object>> x = getData("user_cmx",
                    "select name,age from user");
            System.out.println("x=" + x);
        }

    /**
     * 获取数据库连接
     * 
     * @param myProjName
     * @return
     */
    private static Connection getConn(String myProjName) {
        Connection conn = null;
        try {
            Class.forName(dbdriver);            
            String myjdbcUrl = dburl.replace("cmxDatabaseName", myProjName);
            conn = DriverManager.getConnection(myjdbcUrl, username, userpassword);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    /**
     * 关闭数据库连接
     * 
     * @param rs
     * @param ps
     * @param conn
     */
    private static void closeAll(ResultSet rs, PreparedStatement ps,
            Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn == null)
            return;
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查表,返回行的列表,每个列表中包含列的列表。
     * 
     * @param ProjName
     * @param sql
     * @return
     */
    public static List<List<Object>> getData(String ProjName, String sql) {
        Connection conn = getConn(ProjName);
        PreparedStatement ps = null;
        List<List<Object>> list = new ArrayList<List<Object>>();
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            ResultSetMetaData md = rs.getMetaData();
            int columnCount = md.getColumnCount();
            while (rs.next()) {
                List<Object> lst = new ArrayList<Object>();
                for (int i = 1; i <= columnCount; ++i) {
                    lst.add(rs.getObject(i) == null ? "" : rs.getObject(i));
                }
                list.add(lst);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(rs, ps, conn);
        }
        return list;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114

这里的dbdriver和dburl一定要写正确。如果驱动名称dbdriver写错会报一个异常,假如我少写了一个单词字母,异常如下 
 
如果我的dburl出错了,会抛出以下异常 

如果是dburl的最后“?&useSSL=false”没有写的话会报出一个警告,如下: 

警告全部内容: 

Sun Feb 05 17:15:15 CST 2017 WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. 

如果在useSSL后面没有加 "&serverTimezone=GMT"会报出一个警告,如下:


好了,到这里我们的数据库就连接成功了。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值