Summary of connecting eclipse to MySQL database
1. Create a database in MySQL, create a table, and insert data into the table
1. Create a database
create database select_test
2. Create a table
create table teacher_table( Id int, Name Varchar(20), Sex Varchar(2) )
3. insert data to the table (three test data items are inserted here)
insert into teacher_table values(1,'zhangsan','ma');insert into teacher_table values(2,'lisi','fe');insert into teacher_table values(3,'wangwu','ma');
2. Configure eclipse.
Download the mysql-connector-java-5.1.15-bin.jar file before configuring.
Right-click the project where the package is located, build path ---> Configure build path, and select Add external jars in the pop-up window. Select the mysql-connector-java-5.1.15-bin.jar you downloaded and extracted.
3. Write the connection code.
Database Name: select_test
Username: Root
Password 123456
After the connection is successful, data in the teacher_table table is displayed.
import java.sql.*;class ConnMySql {/** * @param args * @throws Exception */public static void main(String[] args) throws Exception {// TODO Auto-generated method stubClass.forName("com.mysql.jdbc.Driver");Connection conn = 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: Elipse connecting to the MySQL database requires the download of the mysql-connector-java-5.1.15-bin.jar file.