Java connect MySQL database using JDBC Method and example--five parts

Source: Internet
Author: User

A: To import the Mysql-connector-java-5.1.20-bin.jar into the project directory, the steps are as follows:

(1) Create a new Web project item with the project name test

(2) Right click on Project JRE System Library---BUiD path--Configure Build path---Libraries--Add Library--User The Library----new--and named MySQL--checked MySQL--and add JRS--and finally to the directory to put Mysql-connector-java-5.1.20-bin.jar in.

Two: Java code implementation connection MySQL database

1. JDBC Meaning: JDBC is a Java API for executing SQL statements that provides unified access to a variety of relational databases, and consists of a set of classes and interface groups written in Java.
2, Java and MySQL full path, that is, the package name com.mysql.jdbc+ class name driver
3. Link database path Template: JDBC:MYSQL://IP: Port/Database name
4, (1) Insert Delect UPDATE statements are operated with the executeupdate () function
INSERT statements, such as: sql= "insert into user (name, password) VALUES (' Xiaoming ', ' 123456 ')";
DELETE statement, such as: sql= "Delete from user where id = 1";
UPDATE statement, such as: sql= "Update user set username =" XXX "WHERE id = 2";
ResultSet rs = st.executeupdate (sql);
(2) The SELECT statement is operated with the ExecuteQuery () function
SELECT statements, such as: sql= "select *from user";
ResultSet rs = st.executequery (sql);

1 Importjava.sql.Connection;2 ImportJava.sql.DriverManager;3 ImportJava.sql.ResultSet;4 Importjava.sql.SQLException;5 Importjava.sql.Statement;6 7  Public classConnectionmysql {8 9      Public Static voidMain (string[] args) {Ten          OneString driver= "Com.mysql.jdbc.Driver";//Drive Path AString url= "Jdbc:mysql://localhost:3306/eshop";//Database Address -String user= "root";//user name to access the database -String password= "123456";//User Password the         Try { -             //1. Load Driver - Class.forName (driver); -             //2. Link Database +Connection con =drivermanager.getconnection (URL, user, password); -             if(!con.isclosed ()) {//determine if the database is linked successfully +SYSTEM.OUT.PRINTLN ("The database has been successfully linked! "); A                 //3. Create a statement object atStatement st =con.createstatement (); -                 //4. Execute SQL statements -String sql= "Select *from user";//querying all the information of the user table -ResultSet rs = st.executequery (SQL);//return result set after query -                 //5. Print out the results -                  while(Rs.next ()) { inSystem.out.println (rs.getstring ("Id") + "\ T" +rs.getstring ("name") + "\ T" +rs.getstring ("password"));
}
} toRs.close ();//Close Resource +Con.close ();//Close the database - } the *}Catch(Exception e) { $ //TODO auto-generated Catch blockPanax Notoginseng e.printstacktrace (); - } the } +}

The results of the operation are as follows:

Java connect MySQL database using JDBC Method and example--five parts

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.