008--JDBC連接數(shù)據(jù)庫Java實現(xiàn)

1、線程池進行數(shù)據(jù)庫查詢-Java實現(xiàn)

調(diào)用方法

public static void main(String[] args)throws Exception {
    Pool.connectDateBase();
}

創(chuàng)建線程池

public static BasicDataSource createConnectionPool () throws Exception {
        BasicDataSource bds = new BasicDataSource(); //直接new連接池
        bds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
        bds.setUrl(url);//連哪里
        bds.setUsername("scott");//用戶名
        bds.setPassword("tiger");//密碼
        bds.setInitialSize(3);//啟動連接池后,初始的連接數(shù)
        bds.setMaxActive(100);//最大活動連接數(shù)
        bds.setMaxIdle(30);//最大空閑連接數(shù)
        bds.setMaxWait(10000);//最長等候時間,過時就連接失敗
        bds.setMinIdle(1);//最小連接數(shù)
        return bds;
    }

連接服務(wù)的方法

public static void connectDateBase()throws Exception {
       BasicDataSource bds = Pool.createConnectionPool();
       //連接池的屬性一般都是由項目經(jīng)理在配置文件中設(shè)置好
        Connection conn = bds.getConnection();
        Statement st = conn.createStatement();
        String sql = "select * from emp";
        ResultSet rs = st.executeQuery(sql);
        while(rs.next()){
            int id = rs.getInt("empno");
            String name = rs.getString(2);
            double sal = rs.getDouble("sal");
            System.out.println(id+","+name+","+sal);
        }
        rs.close();
        st.close();
        conn.close();
    }

2、存儲過程學(xué)習(xí)-Java實現(xiàn)

數(shù)據(jù)庫創(chuàng)建

drop table users;
create table users(
    u_id number(37) primary  key,
    uname varchar2(200) unique not null ,
    upass varchar2(200) not null
);
drop sequence user_squ;
create sequence user_squ;

存儲過程創(chuàng)建

drop procedure getMax;
create or replace procedure getMax
is 
begin
    insert into users values (user_squ.nextval,user_squ.nextval,'123||user_squ.currval');
    dbms_output.put_line('3123312');
end;

方法調(diào)用

public static void main(String[] args) throws Exception {
    //Login.commitSignal();
    //Login.commitSome();
    //Login.commitMyself();
    Login.commitProduce();      
}

JDBC插入SQL語句插入數(shù)據(jù)庫

"insert into student values (null,'"+name+"','"+value+"')"單括號里面先寫兩個",里面再寫兩個+,里面再寫變量名

PrepareStatement進行setString的時候,只能是數(shù)字,不能像Statement的隨意拼接了

錯誤示例: preparedStatement.setString(1, "user_squ.nextval");

刪除存儲過程

drop procedure 名

清除表的數(shù)據(jù)

truncate table 名

創(chuàng)建鏈接服務(wù)的方法

public static Connection connectDateBase(Properties properties)throws Exception {
        FileInputStream inputStream = new FileInputStream("./src/db.properties");
        properties.load(inputStream);
        inputStream.close();
        String url = properties.getProperty("url");
        String username = properties.getProperty("username");
        String passwd = properties.getProperty("passwd");
        Connection conn = DriverManager.getConnection(url, username, passwd);
        return conn;
    }

單個數(shù)據(jù)提交

public static void commitSignal() throws Exception{
        Properties properties = new Properties();
        Connection connection = Login.connectDateBase(properties);
        String sql = "insert into users values (user_squ.nextval,?,'123||user_squ.currval')";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, "小青");
        preparedStatement.executeUpdate();
        preparedStatement.close();
        connection.close();
    }

多個數(shù)據(jù)提交

public static void commitSome() throws Exception{
        Properties properties = new Properties();
        Connection connection = Login.connectDateBase(properties);
        String sql = "insert into users values (user_squ.nextval,'123||user_squ.currval',?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < 10000; i++) {
            preparedStatement.setString(1, "小青");
            preparedStatement.addBatch();
            if (i%100==99)  preparedStatement.executeBatch();
        }
        preparedStatement.close();
        connection.close();
    }

設(shè)置手動提交

public static void commitMyself() throws Exception{
        Properties properties = new Properties();
        Connection connection = Login.connectDateBase(properties);
        connection.setAutoCommit(false);
        String sql = "insert into users values (user_squ.nextval,?,'123||user_squ.currval')";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, "小青");
        preparedStatement.executeUpdate();
        preparedStatement.close();
        connection.commit();
        connection.close();
    }

提交事務(wù)

public static void commitProduce() throws Exception{
        Properties properties = new Properties();
        Connection connection = Login.connectDateBase(properties);
        String sql = "call getMax()";
        CallableStatement callableStatement = connection.prepareCall(sql);
        for (int i = 0; i < 200; i++) {
            callableStatement.execute();
        }
        callableStatement.close();
        connection.close();
    }

3、properties使用-Java實現(xiàn)

基礎(chǔ)使用

properities 屬性設(shè)置

url=jdbc:oracle:thin:@127.0.0.1:1521:XE
username=scott
passwd=tiger

簡版JDBC連接數(shù)據(jù)庫,使用DML

public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        //  先明確我們要干什么?
        //  1.數(shù)據(jù)庫管理
        //  2.數(shù)據(jù)庫連接
        //  3.建立執(zhí)行平臺
        //  4.執(zhí)行SQL語句
        //  5.平臺關(guān)閉
        
        //1.
        Class.forName("oracle.jdbc.driver.OracleDriver");

                //2.
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
        Connection conn = DriverManager.getConnection(url, "scott", "tiger");
                //3.
        Statement st = (Statement) conn.createStatement();
        String sql ="insert into dept values (88,'NetWork','BOSTON')";
                //4.
        st.executeUpdate(sql);
                //5.
        st.close();
        conn.close();

}

簡版JDBC連接數(shù)據(jù)庫,使用Select

public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        //  先明確我們要干什么?
        //  1.數(shù)據(jù)庫管理
        //  2.數(shù)據(jù)庫連接
        //  3.建立執(zhí)行平臺
        //  4.執(zhí)行SQL語句
        //  5.平臺關(guān)閉

        //1.
        Class.forName("oracle.jdbc.driver.OracleDriver");
                //2.
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
        Connection conn = DriverManager.getConnection(url, "scott", "tiger");
                //3.
        Statement st = (Statement) conn.createStatement();
        String sql ="select * from dept";
        st.executeQuery(sql);
                //4.
        ResultSet resultSet = st.getResultSet();
        while (resultSet.next()) {
            String num = resultSet.getString(1);
            String work = resultSet.getString(2);
            String loc = resultSet.getString("loc");
            System.out.println(num+","+work+","+loc);
        }
        resultSet.close();      
        st.close();
        conn.close();

}

簡版JDBC連接數(shù)據(jù)庫,無沖突版使用Select

public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        //  先明確我們要干什么?
        //  1.數(shù)據(jù)庫管理
        //  2.數(shù)據(jù)庫連接
        //  3.建立執(zhí)行平臺
        //  4.執(zhí)行SQL語句
        //  5.平臺關(guān)閉


        //1.
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //2.
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
        Connection conn = DriverManager.getConnection(url, "scott", "tiger");
        //3.
        Statement st = (Statement) conn.createStatement();
        String sql ="select * from emp";
        st.executeQuery(sql);
        //4.
        ResultSet resultSet = st.getResultSet();
        ResultSetMetaData date = resultSet.getMetaData();
        int count = date.getColumnCount();
        while (resultSet.next()) {
            for (int i = 1; i <= count; i++) {
                String string = date.getColumnName(i);
                String value = resultSet.getString(i);
                System.out.print(string+","+value+"  ");
            }
            System.out.println();
        }
        //5.
        resultSet.close();      
        st.close();
        conn.close();
    
}

簡版JDBC連接數(shù)據(jù)庫,進行項目分級,配置文件路徑(properties)

public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        //  先明確我們要干什么?
        //  1.找路徑
        //  2.取得屬性
        //  3.數(shù)據(jù)庫管理
        //  4.數(shù)據(jù)庫連接
        //  5.建立執(zhí)行平臺
        //  6.執(zhí)行SQL語句
        //  7.平臺關(guān)閉

        //1.
        File file =new File(".");
        System.out.print(file.getAbsolutePath());
        //2.
        Properties properties = new Properties();
        FileInputStream inputStream = new FileInputStream("./src/db.properties");
        properties.load(inputStream);
        inputStream.close();
        String url = properties.getProperty("url");
        String username = properties.getProperty("username");
        String passwd = properties.getProperty("passwd");
        //3.
        Connection conn = DriverManager.getConnection(url, username, passwd);
        //4.
        Statement st = (Statement) conn.createStatement();
        //5.
        String sql ="select * from emp";
        st.executeQuery(sql);
        //6.
        ResultSet resultSet = st.getResultSet();
        ResultSetMetaData date = resultSet.getMetaData();
        int count = date.getColumnCount();
        while (resultSet.next()) {
            for (int i = 1; i <= count; i++) {
                String string = date.getColumnName(i);
                String value = resultSet.getString(i);
                System.out.print(string+","+value+"  ");
            }
            System.out.println();
        }
        //7.
        resultSet.close();      
        st.close();
        conn.close();
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,933評論 18 139
  • 從三月份找實習(xí)到現(xiàn)在,面了一些公司,掛了不少,但最終還是拿到小米、百度、阿里、京東、新浪、CVTE、樂視家的研發(fā)崗...
    時芥藍閱讀 42,366評論 11 349
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,316評論 25 708
  • "跟妳認識四年多了吧?!我們好像沒這樣一起散步小走一段過哦!"我手插著口袋。 春天晚風(fēng)還是帶點涼意。 ...
    馬可約伯閱讀 323評論 0 1
  • echartshighcharts 前臺后臺配合前端展示方式 技巧 注意事項后臺數(shù)據(jù)存儲方式(層級、詳情) 技巧 ...
    遇見番茄大人閱讀 196評論 0 0