二十六、數(shù)據(jù)庫操作基礎(chǔ)
- 數(shù)據(jù)庫系統(tǒng)概論在此先不過多敘述了,主要是學(xué)習(xí)用JAVA操作數(shù)據(jù)庫。
1. JDBC概述
JDBC是一種可用于執(zhí)行SQL語句的Java API(應(yīng)用程序設(shè)計(jì)接口),是連接數(shù)據(jù)庫和JAVA應(yīng)用程序的紐帶。
-
為什么要使用JDBC呢?JAVA可以利用JDBC驅(qū)動程序訪問數(shù)據(jù)庫。通過JDBC技術(shù)可以完成與數(shù)據(jù)庫建立連接、向數(shù)據(jù)庫發(fā)送SQL語句、處理數(shù)據(jù)庫返回的結(jié)果。
JDBC的作用 -
使用JDBC操作數(shù)據(jù)庫分為五個(gè)步驟:加載JDBC驅(qū)動程序、連接數(shù)據(jù)庫、發(fā)送SQL語言、處理結(jié)果集和關(guān)閉數(shù)據(jù)庫。
使用JDBC操作數(shù)據(jù)庫的步驟
2. 連接數(shù)據(jù)庫
-
要訪問數(shù)據(jù)庫,首先要加載數(shù)據(jù)庫的驅(qū)動程序(只需要在第一次訪問數(shù)據(jù)庫時(shí)加載一次),然后每次訪問數(shù)據(jù)庫時(shí)創(chuàng)建一個(gè)Connection對象,接著執(zhí)行操作數(shù)據(jù)庫的SQL語句,最后在完成數(shù)據(jù)庫操作后銷毀前面創(chuàng)建的Connection對象,釋放與數(shù)據(jù)庫的連接。
連接數(shù)據(jù)庫
下面編寫一個(gè)程序連接數(shù)據(jù)庫:
- 連接數(shù)據(jù)庫:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class lianjiesjk
{
public static void main(String[] args)
{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1:3306/database";
String username="root";
String password="123456";
Connection con= DriverManager.getConnection(url,username,password);
System.out.println(con);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
運(yùn)行結(jié)果:

- 如圖所示,數(shù)據(jù)庫連接成功。
3. JAVA對數(shù)據(jù)庫的基本操作(增、刪、查、改)
-
首先在mysql中建立一個(gè)數(shù)據(jù)表:
首先對數(shù)據(jù)庫進(jìn)行查詢操作:
import java.sql.*;
public class chaxun
{
public static void main(String[] args )
{
Connection con1=null;//鏈接接口
Statement stmt1=null;//發(fā)送sql接口
ResultSet rs1=null;//結(jié)果集接口
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con1= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/database","root","123456");
stmt1=con1.createStatement();
rs1=stmt1.executeQuery("select * from student1");
while (rs1.next())//下一行數(shù)據(jù)
{
int ID=rs1.getInt("ID");//獲取ID這一列的值
String name=rs1.getString(2);//獲取第二列的值
String sex=rs1.getString("sex");
String birthday=rs1.getString(4);
System.out.println("編號:"+ID+" 姓名:"+name+" 性別:"+sex+" 生日:"+birthday);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs1!=null)
{
try {
rs1.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt1!=null)
{
try {
stmt1.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con1!=null)
{
try {
con1.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
運(yùn)行結(jié)果:
查詢結(jié)果

查詢結(jié)果
- 由運(yùn)行結(jié)果可以看到,查詢得到的數(shù)據(jù)跟創(chuàng)建表的數(shù)據(jù)一樣,實(shí)現(xiàn)了對數(shù)據(jù)庫的查詢操作。
對數(shù)據(jù)庫實(shí)行增、刪、改操作
import java.sql.*;
public class chaxun
{
public static void main(String[] args )
{
Connection con1=null;//鏈接接口
Statement stmt1=null;//發(fā)送sql接口
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con1= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/database","root","123456");
stmt1=con1.createStatement();
//插入數(shù)據(jù)
String sq11="insert into student1(ID,name,sex,birthday) values('5','tom','男','20001122')";
int result1=stmt1.executeUpdate(sq11);
System.out.println("插入了"+result1+"行數(shù)據(jù)");
//修改數(shù)據(jù)
String sql2="update student1 set birthday = '19920404' where ID = 2";
int result2=stmt1.executeUpdate(sql2);
System.out.println("修改了"+result2+"行數(shù)據(jù)");
//刪除數(shù)據(jù)
String sql3="delete from student1 where ID=1";
int result3=stmt1.executeUpdate(sql3);
System.out.println("刪除了"+result3+"行數(shù)據(jù)");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
運(yùn)行結(jié)果:
操作情況

操作情況

操作后的數(shù)據(jù)表
- 由運(yùn)行結(jié)果可以看到,程序運(yùn)行之后,刪除了第一行的數(shù)據(jù),修改了小華的生日數(shù)據(jù),并且添加了一行新的數(shù)據(jù),至于為什么tom得性別是問號,那是由于在建立數(shù)據(jù)表的時(shí)候,選擇的編碼模式與IDEA的編碼模式不一樣導(dǎo)致的亂碼問題。



