Summary of connecting eclipse to MySQL database

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.