jdbc 操作數據庫,數據庫連接池

一,jdbc 操作數據庫
1,主要步驟:
加載驅動類 -> 獲取數據庫連接 -> 獲取PreparedStatement ->執行數據庫操作
2,實例

package jdbctest;

import java.sql.*;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2016/9/11.
 * jdbc 操作數據庫
 */
public class JdbcTest {

    public static void main(String[] args) {
        JdbcTest jdbcTest = new JdbcTest();
        jdbcTest.updateTest();
       // jdbcTest.queryTest();
    }

    /**
     * jdbc添加測試
     */
    public void updateTest(){
        Connection con = null;
        PreparedStatement pst = null;
        String sql = "update t_student set name = ? where id = '43105'";
        try {
            con = JdbcUtil.getConnection();
            con.setAutoCommit(false); //設置自動提交爲false
            pst = con.prepareStatement(sql); //獲取
            pst.setString(1, "name_6_2");
            int i = pst.executeUpdate(); //執行查詢
            System.out.println("更新結果: " + i);
            con.commit(); //提交
        } catch (SQLException e){
            try {
                con.rollback(); //回滾
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally { //關閉連接
            try {
                con.setAutoCommit(true);
                pst.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * jdbc查詢測試
    */
    public void queryTest(){
        Connection con = null;
        PreparedStatement pst = null;
        String sql = "select * from t_student limit 10";
        ResultSet rs = null;
        try {
            con = JdbcUtil.getConnection();
            pst = con.prepareStatement(sql); //獲取
            rs = pst.executeQuery(); //執行查詢
            while(rs.next()){ //遍歷查詢結果
                System.out.println("1: " + rs.getInt("id") + ", 2: " + rs.getInt("age"));
            }
        } catch (SQLException e){
            e.printStackTrace();
        } finally { //關閉連接
            try {
                rs.close();
                pst.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}


-----------------------------------

package jdbctest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Created by Administrator on 2016/9/11.
 */
public class JdbcUtil {

    /*加載驅動類*/
    static{
        try {
            Class.forName("com.mysql.jdbc.Driver"); //加載驅動類
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /*獲取數據庫連接*/
    public static Connection getConnection(){
        try {
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ssh", "root", ""); //獲取連接
            return con;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}

二,數據庫連接池
1,作用
。。。。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章