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