1. First install MySQL database, the specific installation method can be on Baidu search, you can also follow the following method to install MySQL:
Install MySQL:
Download xampp, install;
Open the Xampp Control Panel and start MySQL
Add the MySQL executable file path to the PATH environment variable
MySQL Installation complete
2. Download the MySQL connection jar package:Mysql-connector-java-5.1.22-bin.jar
http://download.csdn.net/detail/liujan511536/8972159
3. After starting MySQL, go to the root user of MySQL at the command line:
Mysql-u Root
Then create a new database:
Create database db;
New Table User:
Use db;create table User (ID int., name varchar), insert into user (ID, name) VALUES (1, ' hello '); insert into user (ID, NAM e) VALUES (2, ' good ');
4.Create a new Java project in Eclipse, and then add it to the projectMysql-connector-java-5.1.22-bin.jar;
5. Then create a new class Conn in the project you just completed 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.getstr ING (2));} if (rs! = null) rs.close (); if (stmt! = null) stmt.close (); if (conn! = null) Conn.close ();}}
You can connect to the database by running the project.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Eclipse Connection MySQL