Eclipse Connection MySQL Database summary

Source: Internet
Author: User

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

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.