MySQL section
1. First create a mysql database data table
① Run -mysql -u root -p Enter the password (set when installing mysql) to open mysql
② create database create database database name;
show databases ; view databases
drop database database name; drop database
③ Select the database use database name;
④ Create a data table
create table table name
(
Field Name 1 Data Type Constraint,
field name 2 data type constraints,
... ...
field name n data type constraints
);
Mysql data type
Character: char (number of character data size) immutable character / varchar (data size) variable character / text
Number: float/double(7,2) int
日期:date / time / datetime / timestamp
Mysql constraints
Primary key constraint: primary key -- a flag that identifies the uniqueness of all records, the corresponding data cannot be empty and cannot be repeated
Non-null constraint: not null -- the field information must be filled in and cannot be filled with null
Unique constraint: unique -- the field information cannot have new information
Foreign key constraints: foreign key -- reference data information in the main table from the slave table
Check constraints: check -- check the current input input validity
Default value: default -- when the user does not add data, the default value is used for filling
Eg:
create table userInfo ( id int primary key auto_increment, #Number, shape, primary key, auto-increment username varchar ( 20 ) unique , #unique password varchar(20) not null , #非空 sex char ( 1 ) default ' demon ' , #default value age int , birth date null #Allow empty );
⑤ View the existing table show tables in the current database;
⑥ View table structure information desc table name;
⑦ delete the data table drop table table name;
⑧ Add data
inset into tablename(fieldname1, fieldname2,…..) values(value1, value2,…..);
insert into userinfo(username,password,sex,age,birth) values('张三','111','男',22,'2000-1-1') ;
⑨ View data
select field name 1, field name 2,…..from table name;
select id,username,password,sex,age,birth from userinfo ;
⑩ Delete data
delete from table name where conditional data;
delete from userinfo where id=1;
JAVA section
2. Write java files
The main steps:
// 1 add driver
// 2 link database
// 3 manipulate data
// 4 close the link
① Add a driver: import the mysql-connector-java-bin.jar file, right-click the build path and refer to the external jar package
try { // class.forname gets the class object Class.forName( "com.mysql.jdbc.Driver" ); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace (); System.out.println( "Failed to load database driver..." ); }
② Connect to the database:
try { // DirverManager.getConnection(); connect to database conn = DriverManager.getConnection(url,user,pwd); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace (); System.out.println( "Database connection failed" ); }
③ Operating the database
//add operation
//String sql = "insert into userinfo(username,password,sex,age,birth) values('zere','666','女',24,'1994-4-17')"; // Delete operation String sql = "delete from userinfo where id=1" ; try { // The statement method creates an object that executes the SQL statement and returns the result it produces. Use the connection method createstatement to create sta = conn.createStatement(); // There is executeUpdate(String sql) in the statement interface; the method sends the sql statement and returns the number of records that were executed successfully int num = sta.executeUpdate(sql); if(num >0){ System.out.println( "Database operation succeeded" ); }else{ System.out.println( "Database operation failed 1" ); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace (); System.out.println( "Database operation failed 2" ); }
④ Close the database operation object
try { sta.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace (); }
⑤ Run the database connection method
public static void main(String[] args){ MysqlLink link = new MysqlLink(); link.init(); }
Integration code:
package com.cz.link; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class MysqlLink { /* link step 1. Add driver 2. Connect to the database 3. Manipulate data 4. Close the link */ String url = "jdbc:mysql://localhost:3306/javass"; String user = "root"; String pwd = "123456"; // declare the link object Connection conn = null ; String sql = null; public Statement sta = null; public void init(){ // 1. Load the driver try { // class.forname gets the class object Class.forName( "com.mysql.jdbc.Driver" ); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace (); System.out.println( "Failed to load database driver..." ); } // 2. Connect to the database try { // DirverManager.getConnection(); connect to database conn = DriverManager.getConnection(url,user,pwd); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace (); System.out.println( "Database connection failed" ); } // 3. Data manipulation // add operation //String sql = "insert into userinfo(username,password,sex,age,birth) values('zere','666','女',24,'1994-4-17')"; // Delete operation String sql = "delete from userinfo where id=1" ; try { // The statement method creates an object that executes the SQL statement and returns the result it produces. Use the connection method createstatement to create sta = conn.createStatement(); // There is executeUpdate(String sql) in the statement interface; the method sends sql and returns the number of records that were executed successfully int num = sta.executeUpdate(sql); if(num >0){ System.out.println( "Database operation succeeded" ); }else{ System.out.println( "Database operation failed 1" ); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace (); System.out.println( "Database operation failed 2" ); } // 4. Close the database operation object try { sta.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace (); } } public static void main(String[] args){ MysqlLink link = new MysqlLink(); link.init(); } }