Operation of the database MySQL
Additions and deletions to check and change
Mysql>Show Databases;
The display of the database mysql>Use java12
Use Database mysql> drop table person;
Delete a table
Query OK, 0 rows affected (0.01 sec)
Mysql>CREATE TABLE person (age int (4), name varchar (), telnum varchar (14));
Query OK, 0 rows affected (0.01 sec)
Create a table named Personmysql>INSERT into person (age,name,telnum) VALUES ("AA", "13603711234");
Query OK, 1 row affected (0.02 sec)
Inserting Data mysql>Update person Set telnum = "13838260132" WHERE name = "CC";
Query OK, 1 row Affected (0.00 sec)
Update Data mysql>Delete from person where age = 20;
Query OK, 1 row Affected (0.00 sec)
Table Data deletion//Update data table error, because no character encoding is specified when creating the database
mysql> Update person Set name = "Zhang San" where telnum = "18603710987";
ERROR 1366 (HY000): Incorrect string value: ' \xd5\xc5\xc8\xfd ' for column ' name '
At row 1
Create a database that can store Chinese characters//insert multiple records to error
mysql> INSERT into person (age,name,telnum) VALUES ("DD", "+") (120, "EE")
;
Error 1064 (42000): You have a error in your SQL syntax; Check the manual that
Corresponds to your MySQL server version for the right syntax to use near ' (23, "
EE "," + ") ' at line 1
Java connection to the database
First build the drive:
Drivermanager.registerdriver (New Driver ());
-----then establish the connection
Connection conn=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/computer", "root", "root");
Building SQL statements
Statement statement= (Statement) conn.createstatement ();
ResultSet resultset=statement.executequery ("SELECT * from user");Processing of data
Traverse the data and receive it
while (Resultset.next ()) {
String name=resultset.getstring ("name");
Double heigh=resultset.getdouble ("Heigh");
String sex=resultset.getstring ("sex");
int Weight=resultset.getint ("weight");
System.out.println ("+name+" is "+heigh+" with the Gender "+sex");
}
Create four accepted objects to accept the data.
Java Basic Learning Database connection