show databases;數(shù)據(jù)庫的展示 mysql> use java12使用數(shù)據(jù)庫 mysql> drop table person;刪除表格 Query OK, 0 rows affected (..."/>
溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java基礎(chǔ)學(xué)習(xí)之?dāng)?shù)據(jù)庫連接

發(fā)布時(shí)間:2020-05-26 02:45:20 來源:網(wǎng)絡(luò) 閱讀:304 作者:wx59475d497406c 欄目:MySQL數(shù)據(jù)庫

數(shù)據(jù)庫的操作mysql
增刪查改

mysql> show databases;
數(shù)據(jù)庫的展示

mysql> use java12
使用數(shù)據(jù)庫

mysql> drop table person;
刪除表格

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)

創(chuàng)建表格名為person

mysql> insert into person(age,name,telNum) values(20,"AA","13603711234");
Query OK, 1 row affected (0.02 sec)

插入數(shù)據(jù)

mysql> update person set telNum = "13838260132" where name = "CC";
Query OK, 1 row affected (0.00 sec)

更新數(shù)據(jù)

mysql> delete from person where age = 20;
Query OK, 1 row affected (0.00 sec)

表格數(shù)據(jù)的刪除

//更新數(shù)據(jù)表中文報(bào)錯(cuò),因?yàn)閯?chuàng)建數(shù)據(jù)庫的時(shí)候并沒有指定字符編碼
mysql> update person set name ="張三" where telNum = "18603710987";
ERROR 1366 (HY000): Incorrect string value: '\xD5\xC5\xC8\xFD' for column 'name'
at row 1

創(chuàng)建一個(gè)可以存儲(chǔ)漢字的數(shù)據(jù)庫

//插入多條記錄報(bào)錯(cuò)
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","120")' at line 1
Java與數(shù)據(jù)庫的連接

首先建立驅(qū)動(dòng):
DriverManager.registerDriver(new Driver());

-----然后建立連接
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/computer","root","root");
建立SQL語句
Statement statement=(Statement) conn.createStatement();
ResultSet resultSet=statement.executeQuery("select * from user");

數(shù)據(jù)的處理
遍歷數(shù)據(jù)并且進(jìn)行接收

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+"身高為"+heigh+"性別為"+sex);
}
創(chuàng)建四個(gè)接受的對(duì)象,進(jìn)行數(shù)據(jù)的接受。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI