1. Download the driver.
2.eclipse->add extend jars, add driver.
3. Test:
Build databases and tables in MySQL, and operate the database in eclipse.
Code:
Mysql:
Create Database test;create table user (name, varchar (), password varchar); INSERT into user values (' Xiaotao ', ' 1234 56 ');
Java (View and add Records):
Package Sql;import Java.lang.reflect.executable;import java.sql.*;p ublic class Connector {public static void main ( String[] args) {try {class.forname ("com.mysql.jdbc.Driver"); System.out.println ("Success loading Mysql Driver");} catch (Exception e) {System.out.println ("Error loading Mysql driver!"); E.printstacktrace ();} try {Connection connect = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test", "root", "123456"); System.out.println ("Success connect Mysql server!"); Statement stmt = Connect.createstatement (); ResultSet rs = stmt.executequery ("Select *from user"), while (Rs.next ()) {System.out.println (rs.getstring ("name"));} PreparedStatement stmt2 = connect.preparestatement ("INSERT into user value (?,?)"); Stmt2.setstring (1, "Sunhongtao"); Stmt2.setstring (2, "123"); Stmt2.executeupdate ();} catch (Exception e) {System.out.println ("Get Data error!"); E.printstacktrace ();}}}
Eclipse Connection MySQL