JDBC數(shù)據(jù)庫的使用操作總結(jié)建站知識(shí)
導(dǎo)讀:1建站知識(shí)JDBC是一組能夠執(zhí)行SQL語句的API 由于傳統(tǒng)的數(shù)據(jù)庫操作方式需要程序員掌握各個(gè)不同的數(shù)據(jù)庫的API,極其不便 因此java定義了JDBC這一標(biāo)準(zhǔn)的接口和類網(wǎng)站建設(shè)建設(shè)網(wǎng)站。
JDBC是一組能夠執(zhí)行SQL語句的API
由于傳統(tǒng)的數(shù)據(jù)庫操作方式需要程序員掌握各個(gè)不同的數(shù)據(jù)庫的API,極其不便
因此java定義了JDBC這一標(biāo)準(zhǔn)的接口和類,為程序員操作數(shù)據(jù)庫提供了統(tǒng)一的方式
JDBC的操作方式比較單一,由五高端網(wǎng)站建設(shè)個(gè)流程組成:
1.通過數(shù)據(jù)庫廠商提供的JDBC類庫向DriverManager注冊(cè)數(shù)據(jù)庫驅(qū)動(dòng)
2.使用DriverManager提供的getConnection()方法連接到數(shù)據(jù)庫
3.通過數(shù)據(jù)庫的連接對(duì)象的createStatement方法建立SQL語句對(duì)象
4.執(zhí)行SQL語句,并將結(jié)果集合返回到ResultSet中
5.使用while循環(huán)讀取結(jié)果
6.關(guān)閉數(shù)據(jù)庫資源
下面來看看具體操作Mysql數(shù)據(jù)庫的方法
準(zhǔn)備工作
首先我們需要建立一個(gè)數(shù)據(jù)庫和一張簡單的表
復(fù)制代碼 代碼如下:
mysql> create database person;
Query OK, 1 row affected (0.00 sec)
mysql> use person;
Database changed
mysql> create table student(
-> id int,
-> name varchar(20),
-> birth year
-> ) default charset=utf8;
Query OK, 0 rows affected (0.10 sec)
然后往里面插入幾條數(shù)據(jù)
復(fù)制代碼 代碼如下:
mysql> insert into student values
-> (1,'張三',1990),
-> (2,'李四',1991),
-> (3,'王五',1992);
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
這樣一張簡單的表就建好了
復(fù)制代碼 代碼如下:
mysql> select * from student;
+------+--------+-------+
| id | name | birth |
+------+--------+-------+
| 1 | 張三 | 1990 |
| 2 | 李四 | 1991 |
| 3 | 王五 | 1992 |
+------+--------+-------+
rows in set (0.00 sec)
接下來,去mysql官網(wǎng)下載數(shù)據(jù)庫連接器這個(gè)包
其中這個(gè)包里面含有一份文檔,里面列舉了基本的使用方法,可以參考
我們的操作也是按照這份文檔中的內(nèi)容進(jìn)行,然后最主要的地方就是導(dǎo)入這個(gè)jar包
為了操作方便,這里使用eclipse來導(dǎo)入
右鍵項(xiàng)目-->構(gòu)件路徑-->添加外部歸檔,添加好了之后如下所示
JDBC數(shù)據(jù)庫的使用操作總結(jié)
現(xiàn)在我們正式開始使用java來操作mysql數(shù)據(jù)庫
JDBC操作實(shí)例1:最簡單的查詢操作
復(fù)制代碼 代碼如下:
import java.sql.*;
public class Demo {
//為了代碼緊湊性,暫時(shí)拋出所有異常
public static void main(String[] args) throws Exception {
//注冊(cè)數(shù)據(jù)庫驅(qū)動(dòng)
Class.forName("com.mysql.jdbc.Driver");
//建立數(shù)據(jù)庫連接
//參數(shù)一:jdbc:mysql//地址:端口/數(shù)據(jù)庫,參數(shù)二:用戶名,參數(shù)三:密碼
Connection conn = DriverManager.getConnection
("jdbc:mysql://localhost:3306/person","root","admin");
//創(chuàng)建SQL語句
Statement st = conn.createStatement();
//執(zhí)行語句,返回結(jié)果
ResultSet rt = st.executeQuery("show tables");
//循環(huán)取出結(jié)果
while(rt.next()) {
//獲取字段
System.out.println(rt.getString("Tables_in_person"));
}
//關(guān)閉資源,最先打開的最后關(guān)
rt.close();
st.close();
conn.close();
}
}
運(yùn)行結(jié)果:student
如此便可執(zhí)行show tables語句查詢出當(dāng)前數(shù)據(jù)庫含有多少張表
其中rt.getString()方法是獲取字段,這點(diǎn)需要注意
關(guān)閉資源的方式也與以往相反
不過,上面的操作方式靈活性不大,并且不嚴(yán)謹(jǐn)
實(shí)例2:優(yōu)化的查詢操作
復(fù)制代碼 代碼如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Dem建設(shè)網(wǎng)站o {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/person";
String user = "root";
String pwd = "admin";
String sql = "select * from student";
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,user,pwd);
st = conn.createStatement();
//執(zhí)行查詢語句,另外也可以用execute(),代表執(zhí)行任何SQL語句
rs = st.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getObject(1) + " " +
rs.getObject(2) + " " + rs.getInt("birth"));
}
//分別捕獲異常
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
//判斷資源是否存在
if(rs != null) {
rs.close();
//顯示的設(shè)置為空,提示gc回收
rs = null;
}
if(st != null) {
st.close();
st = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
運(yùn)行結(jié)果:
JDBC數(shù)據(jù)庫的網(wǎng)站seo優(yōu)化使用操作總結(jié)
這里把異常給分別捕獲了,并且相關(guān)的字符串全部用變量定義
需要注意下循環(huán)取出數(shù)據(jù)里面的getInt()方法,此處必須知道類型和字段才能取出
如果不知道可以使用getObject(1)取出第一列,getObject(2)取出第二列,以此類推
實(shí)例3:自定義變量插入到數(shù)據(jù)庫
復(fù)制代碼 代碼如下:相關(guān)網(wǎng)站建設(shè)建設(shè)網(wǎng)站。
聲明: 本文由我的SEOUC技術(shù)文章主頁發(fā)布于:2023-05-24 ,文章JDBC數(shù)據(jù)庫的使用操作總結(jié)建站知識(shí)主要講述標(biāo)簽,操作,JDBC數(shù)據(jù)庫的使用操作總結(jié)建站知識(shí)1網(wǎng)站建設(shè)源碼以及服務(wù)器配置搭建相關(guān)技術(shù)文章。轉(zhuǎn)載請(qǐng)保留鏈接: http://www.bifwcx.com/article/web_6590.html
為你推薦與JDBC數(shù)據(jù)庫的使用操作總結(jié)建站知識(shí)相關(guān)的文章
-
通王TWCMS 2.0.3網(wǎng)站模板程序下載
(126)人喜歡 2024-01-15 -
Windows官方原版在哪里下載
(175)人喜歡 2024-01-15 -
WordPress網(wǎng)站模板發(fā)帖標(biāo)題顏色設(shè)置
(131)人喜歡 2024-01-07 -
修改discuz論壇帖子標(biāo)題80字符的長度限制
(249)人喜歡 2024-01-07 -
wordpress程序調(diào)用不帶超鏈接的Tag標(biāo)簽
(234)人喜歡 2024-01-05 -
網(wǎng)站在不同時(shí)期需調(diào)整內(nèi)容更新的方向
(112)人喜歡 2023-08-12