欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

IDEA實(shí)現(xiàn)JDBC的操作步驟

 更新時間:2022年01月29日 11:45:54   作者:薛定諤的痘痘  
JDBC提供了一種基準(zhǔn),據(jù)此可以構(gòu)建更高級的工具和接口,使數(shù)據(jù)庫開發(fā)人員能夠編寫數(shù)據(jù)庫應(yīng)用程序,本文給大家介紹IDEA實(shí)現(xiàn)JDBC的操作步驟,感興趣的朋友一起看看吧

什么是JDBC

  JDBC(Java Data Base Connectivity,java數(shù)據(jù)庫連接)是一種用于執(zhí)行SQL語句的Java API,可以為多種關(guān)系數(shù)據(jù)庫提供統(tǒng)一訪問,它由一組用Java語言編寫的類和接口組成。JDBC提供了一種基準(zhǔn),據(jù)此可以構(gòu)建更高級的工具和接口,使數(shù)據(jù)庫開發(fā)人員能夠編寫數(shù)據(jù)庫應(yīng)用程序。

JDBC本質(zhì)

JDBC接口:是由sun公司提供的一套jdbc接口,該接口由各大數(shù)據(jù)庫廠家實(shí)現(xiàn),最終向程序員和用戶提供和數(shù)據(jù)庫的交互。

驅(qū)動:驅(qū)動是由各數(shù)據(jù)庫廠家遵循接口所實(shí)現(xiàn)的各個實(shí)現(xiàn)類。

IDEA配置

1、IDEA新建工程

2、新建modle

3、添加mysql數(shù)據(jù)庫驅(qū)動

3.1鼠標(biāo)右鍵點(diǎn)集新建好的modle------>Open Module Setting

3.2 libraries—>±—>Java

3.3 添加mysql數(shù)據(jù)庫驅(qū)動

3.3 添加成功

3.4 查看驅(qū)動是否配置成功------> ExternalLibraries

JDBC簡單實(shí)現(xiàn)一條sql語句

代碼

package com.test.jdbc;
/**
 * @author pan
 * @date 2022/1/28 18:52
 */

import com.sun.java.util.jar.pack.DriverResource;
import java.sql.*;
import java.util.ResourceBundle;
 * @ClassName : com.test.jdbc.jdbcTest04
 * @Description : 類描述
public class JdbcTest04 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet  resultSet = null;
        try {
            ResourceBundle resourceBundle = ResourceBundle.getBundle("com\\test\\jdbc\\jdbc");
            //1、注冊驅(qū)動
            /*
            Driver driver = new com.mysql.cj.jdbc.Driver();
            DriverManager.registerDriver(driver);
            */
            String aClass = resourceBundle.getString("class");
            // 利用類的加載機(jī)制可以使完成注冊驅(qū)動,因?yàn)镈river類有一個靜態(tài)代碼塊,該代碼塊完成了上述的任務(wù),可以利用反射機(jī)制,在類加載的時候執(zhí)行靜態(tài)代碼塊
            Class.forName(aClass);
            //2、建立連接
            String url = resourceBundle.getString("url");
            String user = resourceBundle.getString("user");
            String password = resourceBundle.getString("password");
            connection = DriverManager.getConnection(url, user, password);
            //3、獲取數(shù)據(jù)庫操作對象
            statement = connection.createStatement();
            //4、執(zhí)行sql語句
            String sql = "select * from emp";
            resultSet = statement.executeQuery(sql);
            //5、操作結(jié)果集
            while(resultSet.next()){
                String ename = resultSet.getString("ename");
                String deptno = resultSet.getString("deptno");
                String sal = resultSet.getString("sal");
                System.out.println(ename+" " +deptno+" "+ sal);
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            //6、釋放資源
            if(resultSet != null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            if(statement != null){
                    statement.close();
            if(connection != null){
                    connection.close();
        }
    }
}

jdbc.properties

url = jdbc:mysql://localhost:3306/yinpan?useSSl=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
user = root
password =
class = com.mysql.cj.jdbc.Driver

執(zhí)行結(jié)果

到此這篇關(guān)于IDEA實(shí)現(xiàn)JDBC的文章就介紹到這了,更多相關(guān)IDEA實(shí)現(xiàn)JDBC內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論