Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Database connection for Java basic Learning

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report