1. Installation of JDK
First download the appropriate JDK installation package, directly click on the installation, the middle can choose their own JDK installation directory, after installation, configure environment variables. Under Win7, click Computer, Properties, high -level system variables, environment variables, the following page appears
Create a new system variable, set the Java_home,java_home to install the JDK installation address, my computer is set to D:\Program files\java\jdk1.8.0_31,
Then configure the variable value for Classpath,classpath to.; %java_home%\lib;%java_home%\lib\tools.jar;
After the environment variable is configured, under DOS command window, enter java-version to verify that the Java installation is successful or not,
Indicates that the Java installation was successful and the machine attempt could be restarted without prompting.
2. Eclipse as a Java development tool, its easy to install, after the Eclipse website download the appropriate version, directly unzip, click on the Eclipse.exe in the extract file to start using
3. Installation of MySQL
MySQL installation process and use, not here to make a detailed introduction to refer to Http://jingyan.baidu.com/article/e52e3615a1128c40c70c5174.html
4.java connect MySQL need to install the corresponding JDBC software package, download the corresponding Zip software package on the MySQL official website, and unzip it to the location you want, Driver for MySQL (connector/j).
Open the Unpacked folder with the corresponding Jar package, as
Then configure the driver package environment variable, after classpath the location of the jar package that appears after the decompression, I configure the D:\mysql-connector-java-5.1.22\mysql-connector-java-5.1.22\ Mysql-connector-java-5.1.22-bin.jar;
5 Creating the appropriate test table
Create the appropriate table under MySQL and create a command for
CREATE TABLE test_table ( ID int (PRIMARY) KEY, name varchar (ten), class varchar (subject), varchar ( 10));
Then insert the appropriate data,
INSERT VALUES (1, "Xiao Hong", "third Grade 5 class", "Mathematics")
After the data is inserted, Java code is written below
6. Writing Java code
After adding a reference to Mysql-connector-java-5.1.22-bin.jar in Eclipse, start writing the following code
Import Java.sql.connection;import java.sql.drivermanager;import java.sql.resultset;import java.sql.SQLException; Import Java.sql.statement;public class Mysqltest {public static void main (string[] args) throws exception,sqlexception{ String driver= "Com.mysql.jdbc.Driver"; String ur= "Jdbc:mysql://127.0.0.1:3306/test"; Class.forName (driver); Connection connecter=drivermanager.getconnection (Ur, "tong", "123456"); if (!connecter.isclosed ()) System.out.println ("Success in Getconnetion"); Statement statement=connecter.createstatement (); ResultSet rs=statement.executequery ("SELECT * from test_table"); System.out.println ("number" + "\ T" + "name" + "\ T" + "class" + "\ T" + "subject"); String No=null,name=null,classn=null,subject=null; while (Rs.next ()) {no=rs.getstring ("id"); Name=rs.getstring ("name"); Classn=rs.getstring ("class"); Subject=rs.getstring ("subject"); System.out.println (no+ "\ t" +name+ "\ T" +classn+ "\ T" +subject); } }}
String ur= "Jdbc:mysql://127.0.0.1:3306/test", where test represents the corresponding database name,
Connection connecter=drivermanager.getconnection (Ur, "tong", "123456"), where Tong represents the database connection user name, 123456 is the connection password, Finally, the result of executing in eclipse is
Java Connection MySQL Database