Home >php教程 >PHP开发 >Summary of Eclipse connection to Mysql database operations

Summary of Eclipse connection to Mysql database operations

高洛峰
高洛峰Original
2016-12-27 13:17:511704browse

(I personally tested it and started learning Eclipse (my Eclipse version is 4.5.2, and the jar package version of the Jdbc driver is 5.1.7. It can be used for personal testing). When I connected to the database, I found that there are a lot of experiences on the Internet, but I found a lot of errors on the Internet, so I published this blog. I hope it will be helpful to everyone)

#1: First, you need to download the Jdbc driver (mysql-connector-java-5.1.7-bin.jar )This file

Downloading the above file is also very difficult, so for your convenience, I share it here.

2: After downloading the above Jdbc driver, you can start the hands-on operation. First open Eclipse, create a Project, the name of my project is demo, then right-click src and continue to find new , find Floder, and then right-click on the src under the project as shown in the figure

Summary of Eclipse connection to Mysql database operations

The next step is to paste the downloaded Jdbc driver under the lib under the demo project, and then Click on the jar package you just pasted, find the build path and continue to find the add to build path,

Summary of Eclipse connection to Mysql database operations

The result that appears is as shown in the picture above. It can only be used after the addition is completed. Eclipse connects to My sql database

The code to connect to the database is as follows (it is important to note that Connection connect=DriverManage.getConnection("jdbc:mysql://localhost:3306/test","root"," Password"))

The "password" in the above sentence is your database's own password; you need to modify it yourself, the one in "jdbc:mysql://localhost:3306/test" above test is a table created by myself using mysql. It is created by myself and requires extra attention; (the statement to create my sql will be found elsewhere in this blog, so please pay attention)

package com.ningmengxueyuan;
import java.sql.*;
public class MysqlJdbc{ 
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/test1","root","123456"); 
//连接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")); 
} 
}catch(Exception e) { 
System.out.print("get data error!"); 
e.printStackTrace(); 
} 
} 
}

The above is the editor's I would like to introduce to you a summary of the operation of connecting Eclipse to Mysql database. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!

For more articles related to the summary of Eclipse connection to Mysql database operations, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn