Get the App
SLTechnology News&Howtos  ›  Database  › 

Database connection for Java basic Learning

Shulou Source: shulou.com Published: 2022-06-01 18:37:00 09月25日 Update

Database operation mysql

Add, delete, check and correct

Mysql > show databases

Database display mysql > use java12

Use the database mysql > drop table person

Delete form

Query OK, 0 rows affected (0.01 sec)

Mysql > create table person (age int (4), name varchar (20), telNum varchar (14))

Query OK, 0 rows affected (0.01 sec)

Create a table named personmysql > insert into person (age,name,telNum) values (20, "AA", "13603711234")

Query OK, 1 row affected (0.02 sec)

Insert 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)

Delete of table data / / update data table Chinese error because no character encoding was specified when the database was created

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 report an error

Mysql > insert into person (age,name,telNum) values (22, "DD", "110") (23," EE "," 120")

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that

Corresponds to your MySQL server version for the right syntax to use near'(23, "

EE "," 120s ")'at line 1

Connection between Java and database

First, set up the driver:

DriverManager.registerDriver (new Driver ())

-then establish a connection

Connection conn=DriverManager.getConnection ("jdbc:mysql://localhost:3306/computer", "root", "root")

Establish a SQL statement

Statement statement= (Statement) conn.createStatement ()

ResultSet resultSet=statement.executeQuery ("select * from user"); data processing

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" + name+ "height" + heigh+ "gender" + sex)

}

Create four accepted objects to accept the data.

Tags: Data database table update multiple character object gender datasheet time code statement height Zhang San Chinese Jiansi Chinese character processing storage driver Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology NVidia Shulou Tech Info vpn Microsoft