After installing MySQL, Java uses JDBC to connect to the MySQL database

Source: Internet
Author: User
Tags ming stmt

1. Download and install MySQL, because open source free, directly to the official website to download and follow the prompts to install: https://www.mysql.com/downloads/

2. After the installation is complete, see if the process has MySQL enabled (MySQL Workbench is usually installed, this is the database graphics interface, we do not need to use Java)

Use cmd (command line) to operate MySQL, add database study in MySQL, and then add table to study database study

3. Download Java driver to connect mysql,:https://dev.mysql.com/downloads/connector/j/, download a jar package, which project to use to copy to the project directory, and then add Build joins the project (the jar package can be found in referenced libraries after joining the project)

4.java Code:

 Packagecom.cn.edu.szu.ming;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;/*** @ClassName: testdb.java*@author[email protected] @email. szu.edu.cn*@versioncreated: May 30, 2017 morning 11:41:40 * @Description:*/ Public classTestDB {Private Final StaticString url= "Jdbc:mysql://localhost:3306/study";//connection format jdbc:mysql://( IP address): (port)/(database name)    Private Final StaticString name= "Ming";//MySQL user name    Private Final StaticString pass= "qq147741";//the corresponding password    StaticConnection Conn; StaticStatement stmt; StaticPreparedStatement PST; StaticResultSet Res; /*** * @Tiltle main *@paramargv *@returnvoid*/     Public Static voidMain (String argv[]) {Try{class.forname ("Com.mysql.jdbc.Driver");//load driver, string for drive nameConn=drivermanager.getconnection (Url,name,pass);//connecting to a databaseSYSTEM.OUT.PRINTLN (conn); stmt=conn.createstatement ();                                    PrintTable (); //Modify the data to change the password for the first line to Line1Pst=conn.preparestatement ("Update study set Name=?where id=?")); Pst.setstring (1, "STU1");//The first parameter in an update's SQL statement is PAWD, set to YesPst.setint (2, 1);//the second parameter of the SQL statement for update is set toPst.executeupdate ();//Send Actionprinttable (); //Delete data, delete the first row of data specified in the tablePst=conn.preparestatement ("Delete from study where id=?")); Pst.setint (1, 3);//specifies that the first parameter of the preceding SQL is the ID value of 3pst.executeupdate ();                                    PrintTable (); //Increase RowPst=conn.preparestatement ("INSERT into study (ID,NAME,PAWD) VALUES (?,?,?)"); Pst.setint (1,4);//set the first parameter in the SQL statement (ID in the study table) to 4Pst.setstring (2, "STU3"); Pst.setstring (3, "123456789");            Pst.executeupdate ();                                PrintTable (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }         Public Static voidPrintTable ()throwssqlexception{//Querying tables, printing study tablesRes=stmt.executequery ("SELECT * from study");//Select all the information in the tableSystem.out.println ("number" + "\ T" + "name" + "\ T" + "password");  while(Res.next ()) {//Print All LinesSystem.out.print (Res.getint (1) + "\ T");//prints the first parameter value (that is, id) for each row in the study tableSystem.out.print (res.getstring (2) + "\ T");//Prints the second parameter value (that is, name) for each row in the study tableSystem.out.print (res.getstring (3) + "\ n");//prints the third parameter value (that is, pawd) for each row in the study table                    }    }}

More details recommended reading:

Detailed Description: http://www.jianshu.com/p/dc73ee0f2f83

SQL syntax: http://www.w3school.com.cn/sql/sql_syntax.asp

After installing MySQL, Java uses JDBC to connect to the MySQL database

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.