ECLIPSE/MYSQL/JDBC (ODBC) configuration and testing
First step MySQL Installation
01:http://dev.mysql.com/downloads/mysql/5.6.html
02download:
03 Unzip to the installation directory. (My installation directory is D:\App)
04 Configuring the environment variables for MySQL
Add the Bin folder path under the MySQL installation directory to the PATH environment variable.
(My path: D:\App\mysql-5.6.19-win32\bin).
05 Open the command line, enter the \ Bin directory, enter Mysql–install
06 Start the MySQL service, enter: net start MySQL if there is a problem, close the command line to reopen the service at startup.
07 Login database, enter command: Mysql–u root–p (default no password, direct carriage return).
08 Create a database, add a table, add data from a table
Mysql>create database test;//creates a db, and sometimes a Test database already exists, without a tube, to proceed to the next step.
Mysql>use test//Specify the database to manipulate
Mysql>create table User (name varchar), phone varchar (20));//create User table and set field.
Mysql>insert into user values ("Zhangsan", "13838785678");
Mysql>insert into user values ("Lisi", "13932562412");
Mysql>insert into user values ("Wangwu", "13000785600");
09 Exiting the Database
Mysql>quit
10 validating data Some as in the database
Mysql>mysql-u root-p
Mysql>use Test
Mysql>select * from user;
Will output the data that has been deposited, and MySQL is fully loaded.
Step two: Set up MySQL in Eclipse and call MySQL query data using Java
01 Download MySQL Java version of the drive MySQL connector
Official website Download: http://dev.mysql.com/downloads/connector/
Select CONNECT/ODBC (i download the install-free version here)
02 Unzip to your installation directory
03 Create a new Java project in eclipse
04 Importing the jar package under the connector installation package
05 Create a new Java source file with the following contents:
import java.sql.Connection;
import Java.sql.DriverManager;
import Java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Public class Mysqldemo {
Public Static void Main (string[] args) {
TODO auto-generated Method stub
Try {
Class.forName ("Com.mysql.jdbc.Driver");
Class. forname ("Org.gjt.mm.mysql.Driver");
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Try {
Connection connect = (Connection) DriverManager. getconnection ("Jdbc:mysql://localhost:3306/test", "Root", "" ");
System. out. println ("Connect MySQL server succcessfully!");
Statement state = Connect.createstatement ();
ResultSet result = State.executequery ("SELECT * from user");
Traversing output Query Results
while (Result.next ()) {
System. out. println (result.getstring ("name") + "\ T" +result.getstring ("phone");
}
} catch (SQLException e) {
TODO auto-generated Catch block
System. out. println ("Get Data erro!");
E.printstacktrace ();
}
}
}
06 Run the program, if the test successfully will output the stored data;