java學習筆記一 2019.6.27 週四

一、jdbc資源的關閉
package com.rupeng.jdbctest1;

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

public class Test1 {

public static void main(String[] args) {
    // TODO 自動生成的方法存根
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        // TODO 自動生成的 catch 塊
        //e.printStackTrace();
        System.out.println("加載jdbc驅動"+e.getMessage());
    }
    Connection conn=null;
    PreparedStatement stmt=null;
    try {
        conn=DriverManager.getConnection("jdbc:mysql://localhost/mystudy1?seUnicode=true&characterEncoding=UTF-8", "root", "root");
        System.out.println(conn.getClass());
        stmt=conn.prepareStatement("insert into t_persons(name,age,hobbies)values('趙寒',123456789,'三亞三亞')");
        System.out.println(stmt.getClass());
        int i=stmt.executeUpdate();
        System.out.println("成功執行"+i+"條");
    } catch (SQLException e) {
        // TODO 自動生成的 catch 塊
        //e.printStackTrace
        System.out.println("鏈接失敗"+e.getMessage());
    }finally{
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                // TODO 自動生成的 catch 塊
                e.printStackTrace();
            }
        }
        IOUtils.closeQuietly(conn);
    }
    System.out.println("YEAH!!!!");

}

}

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