Eclipse connects to mysql and eclipsemysql
1. First install the mysql database. You can search for the specific installation method on a hundred degrees or install mysql as follows:
Install Mysql:
Download and install xampp;
Open the xampp control panel and start mysql
Add the Mysql execution file PATH to the PATH environment variable.
Mysql Installation Complete
2. Download mysql connection jar package: mysql-connector-java-5.1.22-bin.jar
Http://download.csdn.net/detail/liujan511536/8972159
3. After mysql is started, enter the root user of mysql in the command line:
mysql -u root
Create a database:
create database db;
Create a table user:
use db;create table user(id int, name varchar(20));insert into user(id, name) values (1, 'hello');insert into user(id, name) values(2, 'good');
4. Create a Java Project in eclipse and add a mysql-connector-java-5.1.22-bin.jar to the Project;
5. Create a class Conn in the project just now and add the following code:
Conn. java:
import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import com.mysql.jdbc.Connection;public class Conn {public static void main(String[] args) throws ClassNotFoundException, SQLException{Class.forName("com.mysql.jdbc.Driver");Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root", "");Statement stmt = conn.createStatement();ResultSet rs = stmt.executeQuery("select * from user");while(rs.next()){System.out.println(rs.getInt(1) + " " + rs.getString(2));}if (rs != null)rs.close();if (stmt != null)stmt.close();if (conn != null)conn.close();}}
Run the project to connect to the database.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.