标签:java os io for cti 代码 ar new
package itcast;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Base {
/**
* @param args
*/
public static void main(String[] args) {
}
static void template() throws Exception {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
// 2建立链接
JdbcUtils.getConnection();
// 3创建语句
st = conn.createStatement();
// 4 执行语句
rs = st.executeQuery("select * from user");
// 5处理结果
while (rs.next()) {
System.out.println(rs.getObject(1) + "\t" + rs.getObject(2)
+ "\t" + rs.getObject(3) + "\t" + rs.getObject(4)
+ "\t" + rs.getObject(5) + "\t");
}
} finally {
JdbcUtils.free(rs, st, conn);
}
}
}
*************************************简单方法*********************************************************************
package itcast;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public final class JdbcUtils {
private static String url = "jdbc:mysql://localhost:3306/test";
private static String user = "root";
private static String password = "";
// 私有的构造方法
private JdbcUtils() {
}
// 把注册驱动的动作放在静态代码块内
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
// 建立链接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
// 释放资源
public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
*******************************单例方法 麻烦不推荐**************************************************
package cn.itcast.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public final class JdbcUtilsSing {
private String url = "jdbc:mysql://localhost:3306/test";
private String user = "root";
private String password = "";
// private static JdbcUtilsSing instance = new JdbcUtilsSing();
private static JdbcUtilsSing instance = null;
// 私有的构造方法
private JdbcUtilsSing() {
}
public static JdbcUtilsSing getInstance(){
if(instance == null){
synchronized(JdbcUtilsSing.class){
if(instance == null){
instance = new JdbcUtilsSing();
}
}
}
return instance;
}
// 把注册驱动的动作放在静态代码块内
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
// 建立链接
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
// 释放资源
public void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
标签:java os io for cti 代码 ar new
原文地址:http://www.cnblogs.com/siashan/p/3876570.html