(3) Select the left MySQL Servers, find the bottom layer, click the MySQL Server 5.7.21-x64, click the arrow to add the middle to the right. MySQL Connectors, and a connector (compiler drives) connected to other programming languages, Java language as an example need to add connector / J5.1.44-x86. Java project development tool can be used to import mysql-connector-java-bin.jar package MySQL installation package of the development. Now you can not choose, click Next in the following figure:
(7) Type Select Development Machine (development machine), strong port number selected by default, other defaults, click Next. Figure:
If you have any version of the MySQL database installation, have taken up the listening port, an error occurs, resolved: (1) modify the listening port number, MySQL database previously installed version (2) or unloaded.
(8) Set yourself a login password database client, the best down, you need a password to login before each use, you need to define a user name, select the server, the general election localhost (local server / local host), enter the password again, click Next. As shown below:
(9) choose to start with the system MySQL server, Start the MySQL Sever at System Startup, other defaults, click Next. As shown below:
(10) check on the Enable X Protocol / MySQL as a Document Stop, click Next. As shown below:
(11) Click Execute all the green check mark, if you have the wrong number, uninstall, or reinstall deleted, click Finish. As shown below:
(12)MySQL Server 5.7.21 显示 Configuration Complete,点击Next,点击Finish,全部安装完毕。
下载地址:http://dev.mysql.com/downloads/connector/j/
新建的项目就有了jar包
好了,有了jar包,也就只剩下最后关键的一步,eclipse与mysql的通信。
这里我们要先在mysql控制台下创建一个数据库,以便后期测试与eclipse的通信是否成功。
如何启动mysql?找到mysql的安装目录,然后进入bin目录,打开mysql.exe即可,但是我和同学都出现了闪退情况,如果有大佬知道如何解决闪退问题可直接评论回复:
配置环境变量,变量值为MySQL安装目录(默认安装目录为C盘Programs Files/MySql)
输入之前设置的密码,空密码的可以直接回车进入。(英文输入,中文输入如果有字母和数字的组合你懂得)
首先,我们通过执行sql语句来创建一个数据库:
create database demo;//创建一个名为demo的数据库,
use demo;//指定demo为当前要操作的数据库
create table user (name varchar(20),password varchar(20));//创建一个表user,并为其设置两个字段(name和password)
insert into user values('liber','libo123456');//插入一条数据到user表中
select * from user;//查看数据表
这时我们看到,刚才插入的已经显示在user表中了,此时创建测试数据库就成功了,
4,我们开始进行eclipse与mysql的配置。
启动eclipse,依次找到file - new - java project(JAVA EE如果找不到java project就从other中找到java文件夹可选择java project),输入项目名称,这里我暂用jdbc_demo
点击finish提交即可。
可以看到刚才的项目已经创建成功。
然后右击项目名称,依次选择buili path - add external archives,如图所示:
找到刚才所得的jar文件后,添加即可,如图所示
:
目前所有的部分都已配置完成,最后我们来测试一下:
首先新建一个类,右击src - new - class 命名为demo(自定义)后提交即可。
这时我们已经可以看到一个名为my的java文件了,还记得刚才控制台下我们创建的数据库和user表吗?配合在一起,我们通过编写这个文件来测试数据库是否连接成功。
package jdbc_demo; import java.sql.*; public class demo{ public static void main(String args[]) { try { Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序 //Class.forName("org.gjt.mm.mysql.Driver"); System.out.println("Success loading Mysql Driver!"); } catch (Exception e) { System.out.print("Error loading Mysql Driver!"); e.printStackTrace(); } try { Connection connect = DriverManager.getConnection( "jdbc:mysql://localhost:3306/demo","liber","libo123456"); //连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码 必须自定义 System.out.println("Success connect MySql server!"); Statement stmt = connect.createStatement(); ResultSet rs = stmt.executeQuery("select * from user"); //user 为你表的名称 while (rs.next()) { System.out.println(rs.getString("name")); System.out.println(rs.getString("password")); } } catch (Exception e) { System.out.print("get data error!"); e.printStackTrace (); } } }
If there is no error, we run directly change the program:
Click the Run button:
Final Results:
User table shows the values of this we have the perfect java configuration is successful.