Go MySQL's simple use and JDBC example

Source: Internet
Author: User

MySQL Simple operation
Start mysqlnetStart mysql//login Mysql-u root-p//Creating a DatabaseCreateDatabase mydb;CreateDatabasetest;//Deleting a databaseDropDatabasetest;//using the databaseUse mydb;//to create a tableCreateTable MyTable (Namevarchar), Sexchar);//Display all tables in the databaseShowtables;//Add a columnAlterTable MyTableAdd ageint;//InsertionInsert MyTableValue"Liu",' F ',22);Insert MyTableValue"Wang",' m ',22);Insert MyTable (Name,sex)Value"Li",' f '); Insert MyTable value ("Zhao",' m ',22);//delete item in table Delete from  mytable where name= "Liu" ; Delete from mytable where name= "li";//Updating update mytable Set age = All  where na me = "Wang";//Query SELECT * from mytable;           
Jdbc
    • The JDBC driver is divided into 4 categories
      • JDBC-ODBC Bridge
      • Part of the local API, some Java drivers
      • JDBC Network Pure Java Driver
      • Local protocol Java Driver

Examples of JDBC

Import java.sql.*;Import Com.mysql.jdbc.Driver;PublicClass Jdbcmysql {Public void Operatemysql()ThrowsSQLException, classnotfoundexception{String drivername ="Com.mysql.jdbc.Driver"; String URL ="Jdbc:mysql://127.0.0.1:3306/mydb"; String sql ="SELECT * from mydb.mytable"; String username ="Root"; String Password =""; Connection conn =Nulltry{Load the drive Driver.Class.Forname (drivername);Establish Connection conn = DriverManager.Getconnection (Url,username,password);Create Statement to execute the SQL statement Statement PS = conn.Createstatement ();Result set processing ResultSet rs = ps.ExecuteQuery (SQL);while (Rs.Next ()) {System.Out.println (Rs.GetString ("Name") +"," +rs.GetString ( "sex") + "age"));}} catch (classnotfoundexception e) {e.printStackTrace ( ); } catch (SQLException e) {e.printstacktrace ();} //close connection finally{ if (conn!=null) { try{conn.catch (SQLException e) {e.printstacktrace ();}}}}  

Set up a test case to verify

ImportStatic org.junit.assert.*;Import java.sql.SQLException;Import Org.junit.Before;Import Org.junit.Test;Publicclass jdbctest { @Test public < Span class= "hljs-function" >void test () span class= "kw" >throws ClassNotFoundException, SQLException {jdbcmysql JM = new jdbcmysql (); JM. operatemysql ()}}             

Execution results

Turn from: http://www.cnblogs.com/sweiqi/p/5936914.html;

Go MySQL's simple use and JDBC example

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.