Eclipse Connection MySQL Database summary
Create a database in MySQL and create a table to insert data into the table
1. Create a database
createdatabaseselect_test |
2. Create a table
createtable teacher_table( Id int, Name Varchar(20), Sex Varchar(2) ) |
3. Insert data into the table (three test data inserted here)
insert intoteacher_table values(1,‘zhangsan‘,‘ma‘);insertintoteacher_table values(2,‘lisi‘,‘fe‘);insert intoteacher_table values(3,‘wangwu‘,‘ma‘); |
Second, configure Eclipse.
Please download the Mysql-connector-java-5.1.15-bin.jar file before configuring.
Right-click the project package that contains the package (project), build path---> Configure build Path, and select Add External JARs in the popup window. Select the Mysql-connector-java-5.1.15-bin.jar you downloaded and extracted.
Third, write the connection code.
Database name: select_test
User name: Root
Password: 123456
The data in the Teacher_table table is displayed after the connection is successful.
import java.sql.*;class ConnMySql { /** * @param args * @throws Exception */ publicstaticvoid main(String[] args) throws Exception { // TODO Auto-generated method stub Class.forName("com.mysql.jdbc.Driver"); Connectionconn = DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3306/select_test", "root","123456"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from teacher_table"); while (rs.next()) { System.out.println(rs.getInt(1) + "\t" +rs.getString(2) + "\t" +rs.getString(3) ); } if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } }} |
Summary: The Elipse connection to the MySQL database requires the download of the Mysql-connector-java-5.1.15-bin.jar file.
Eclipse Connection MySQL Database summary