一、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!!!!");
}
}