更新時(shí)間:2021-02-23 17:21:02 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1770次
JDBC可以實(shí)現(xiàn)SQL語(yǔ)句在數(shù)據(jù)庫(kù)中的執(zhí)行,也就是說,數(shù)據(jù)庫(kù)的所有操作,包括對(duì)數(shù)據(jù)庫(kù),對(duì)表格,對(duì)記錄都可以進(jìn)行相應(yīng)的操作,但是其實(shí)原則上,JDBC只應(yīng)該對(duì)記錄進(jìn)行操作,不能對(duì)表格乃至數(shù)據(jù)庫(kù)進(jìn)行更改操作。下面就為大家介紹一些JDBC常用操作。
1.獲取數(shù)據(jù)庫(kù)連接
一般為了方便建立數(shù)據(jù)庫(kù)鏈接通常會(huì)創(chuàng)建一個(gè)類,里面封裝好實(shí)現(xiàn)數(shù)據(jù)庫(kù)鏈接的函數(shù),方便調(diào)用實(shí)現(xiàn)數(shù)據(jù)庫(kù)連接。
?import java.sql.Connection;
?import java.sql.DriverManager;
?import java.sql.SQLException;
??public class DBUtil {
?
??????private static final String URL="jdbc:mysql://127.0.0.1:3306/imooc?useUnicode=true&characterEncoding=utf-8";
?????private static final String USER="root";
?????private static final String PASSWORD="tiger";
?????
?????private static Connection conn=null;
?????
?????static {
?????????try {
?????????????//1.加載驅(qū)動(dòng)程序
?????????????Class.forName("com.mysql.jdbc.Driver");
?????????????//2.獲得數(shù)據(jù)庫(kù)的連接
?????????????conn=DriverManager.getConnection(URL, USER, PASSWORD);
?????????} catch (ClassNotFoundException e) {
?????????????e.printStackTrace();
?????????} catch (SQLException e) {
????????????e.printStackTrace();
?????????}
?????}
?????//將獲得的數(shù)據(jù)庫(kù)與java的鏈接返回(返回的類型為Connection)
?????public static Connection getConnection(){
?????????return conn;
?????}
?}
2、增加表中記錄
??import java.sql.DriverManager;
??import java.sql.PreparedStatement;
??import java.sql.ResultSet;
??import java.sql.SQLException;
??import java.sql.Statement;
??import java.sql.Connection;
??import java.sql.DriverManager;
??import java.sql.SQLException;
??
?
?public class javaTest {
????
?????public static void main(String[] args) throws ClassNotFoundException, SQLException ?{
?????????String URL="jdbc:mysql://127.0.0.1:3306/imooc?useUnicode=true&characterEncoding=utf-8";
?????????String USER="root";
?????????String PASSWORD="tiger";
?????????//1.加載驅(qū)動(dòng)程序
?????????Class.forName("com.mysql.jdbc.Driver");
?????????//2.獲得數(shù)據(jù)庫(kù)鏈接
?????????Connection conn=DriverManager.getConnection(URL, USER, PASSWORD);
?????????//3.通過數(shù)據(jù)庫(kù)的連接操作數(shù)據(jù)庫(kù),實(shí)現(xiàn)增刪改查(使用Statement類)
????????String s=""+"insert into user(id,user_name,user_password) values("+"2,?,123)";
?????????PreparedStatement pst=conn.prepareStatement(s);
?????????
?????????pst.setString(1, "xiaoshuai1");
?????????//pst.setString(2, "123");
?????????????
?????????pst.execute(); ???????
?????????//關(guān)閉資源 ???????
?????????pst.close();
?????????conn.close();
?????}
?}
3、刪除數(shù)據(jù)庫(kù)表格記錄
?import java.sql.DriverManager;
??import java.sql.PreparedStatement;
??import java.sql.ResultSet;
??import java.sql.SQLException;
??import java.sql.Statement;
??import java.sql.Connection;
??import java.sql.DriverManager;
??import java.sql.SQLException;
??
?
?public class javaTest {
????
?????public static void main(String[] args) throws ClassNotFoundException, SQLException ?{
?????????String URL="jdbc:mysql://127.0.0.1:3306/imooc?useUnicode=true&characterEncoding=utf-8";
?????????String USER="root";
?????????String PASSWORD="tiger";
?????????//1.加載驅(qū)動(dòng)程序
?????????Class.forName("com.mysql.jdbc.Driver");
?????????//2.獲得數(shù)據(jù)庫(kù)鏈接
?????????Connection conn=DriverManager.getConnection(URL, USER, PASSWORD);
?????????//3.通過數(shù)據(jù)庫(kù)的連接操作數(shù)據(jù)庫(kù),實(shí)現(xiàn)增刪改查(使用Statement類)
?????????String s="delete from user where id=2 and user_name=?";
?????????PreparedStatement pst=conn.prepareStatement(s);
?????????
?????????pst.setString(1, "xiaoshuai1");
?????????//pst.setString(2, "123");
?????????????
?????????pst.execute(); ???????
?????????//關(guān)閉資源 ???????
????????pst.close();
????????conn.close();
?????}
?}
4、更新數(shù)據(jù)庫(kù)中表格記錄信息
?import java.sql.DriverManager;
??import java.sql.PreparedStatement;
??import java.sql.ResultSet;
??import java.sql.SQLException;
??import java.sql.Statement;
??import java.sql.Connection;
??import java.sql.DriverManager;
??import java.sql.SQLException;
?
?
?public class javaTest {
????
?????public static void main(String[] args) throws ClassNotFoundException, SQLException ?{
?????????String URL="jdbc:mysql://127.0.0.1:3306/imooc?useUnicode=true&characterEncoding=utf-8";
?????????String USER="root";
?????????String PASSWORD="tiger";
?????????//1.加載驅(qū)動(dòng)程序
?????????Class.forName("com.mysql.jdbc.Driver");
?????????//2.獲得數(shù)據(jù)庫(kù)鏈接
?????????Connection conn=DriverManager.getConnection(URL, USER, PASSWORD);
?????????//3.通過數(shù)據(jù)庫(kù)的連接操作數(shù)據(jù)庫(kù),實(shí)現(xiàn)增刪改查(使用Statement類)
?????????String s="update user set user_name=? where id=1 ";
?????????PreparedStatement pst=conn.prepareStatement(s);
?????????
?????????pst.setString(1, "xiaoshuaige");
?????????//pst.setString(2, "123");
?????????????
?????????pst.execute(); ???????
?????????//關(guān)閉資源 ???????
?????????pst.close();
?????????conn.close();
?????}
?}
5、調(diào)用存儲(chǔ)過程
(1)調(diào)用無(wú)參存儲(chǔ)過程
1)存儲(chǔ)過程代碼
?CREATE ?PROCEDURE SP_select_nofilter3()
?BEGIN
??????SELECT * FROM file;
?END;
2)調(diào)用代碼
?import java.sql.DriverManager;
??import java.sql.PreparedStatement;
??import java.sql.ResultSet;
??import java.sql.SQLException;
??import java.sql.Statement;
??import java.sql.CallableStatement;
??import java.sql.Connection;
??import java.sql.DriverManager;
??import java.sql.SQLException;
?
?
?public class javaTest {
????
?????public static void main(String[] args) throws ClassNotFoundException, SQLException ?{
?????????String URL="jdbc:mysql://127.0.0.1:3306/imooc?useUnicode=true&characterEncoding=utf-8";
?????????String USER="root";
?????????String PASSWORD="tiger";
????????//1.加載驅(qū)動(dòng)程序
?????????Class.forName("com.mysql.jdbc.Driver");
?????????//2.獲得數(shù)據(jù)庫(kù)鏈接
?????????Connection conn=DriverManager.getConnection(URL, USER, PASSWORD);
?????????//3.通過數(shù)據(jù)庫(kù)的連接操作數(shù)據(jù)庫(kù),實(shí)現(xiàn)增刪改查(使用Statement類)
?????????String s="call SP_select_nofilter3() ";
?????????CallableStatement cst=conn.prepareCall(s);
?????????????
?????????ResultSet rs=cst.executeQuery();
?????????
?????????while(rs.next()){
?????????????System.out.println(rs.getString("user_name"));
????????}
?????????//關(guān)閉資源 ???????
?????????cst.close();
?????????conn.close();
?????}
?}
以上就是JDBC常見操作,主要還是關(guān)于數(shù)據(jù)庫(kù)中表格的數(shù)據(jù)的記錄相關(guān)的,所以我們只要弄清楚數(shù)據(jù)庫(kù)中的數(shù)據(jù)的記錄情況,對(duì)于我們使用JDBC有很大的幫助。本站的JDBC教程中也會(huì)給大家很多使用JDBC的技巧,想要掌握的小伙伴趕快行動(dòng)吧!
Java實(shí)驗(yàn)班
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
Java就業(yè)班
有基礎(chǔ) 直達(dá)就業(yè)
Java夜校直播班
業(yè)余時(shí)間 高薪轉(zhuǎn)行
Java在職加薪班
工作1~3年,加薪神器
Java架構(gòu)師班
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)