Java Basic Learning Database connection

Source: Internet
Author: User

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

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.