写在前面
本文涉及的工具类部分是自己编写,另一部分是在项目里收集的。工具类涉及数据库连接、格式转换、文件操作、发送邮件等等。提高开发效率,欢迎收藏与转载。
数据库连接工具类
数据库连接工具类——仅仅获得连接对象 ConnDB.java
[java]
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
/**
* 数据库连接工具类——仅仅获得连接对象
*
*/
public class ConnDB {
private static Connection conn = null;
private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/axt?useUnicode=true&characterEncoding=UTF-8";
private static final String USER_NAME = "root";
private static final String PASSWORD = "root";
public static Connection getConn(){
try {
Class.forName(DRIVER_NAME);
conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
/**
* 数据库连接工具类——仅仅获得连接对象
*
*/
public class ConnDB {
private static Connection conn = null;
private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/axt?useUnicode=true&characterEncoding=UTF-8";
private static final String USER_NAME = "root";
private static final String PASSWORD = "root";
public static Connection getConn(){
try {
Class.forName(DRIVER_NAME);
conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
数据库连接工具类——包含取得连接和关闭资源 ConnUtil.java
[java]
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @className: ConnUtil.java
* @classDescription: 数据库连接工具类——包含取得连接和关闭资源
* @function:
* @author: Wentasy
* @createTime: 2012-9-24 上午11:51:15
* @modifyTime:
* @modifyReason:
* @since: JDK 1.6
*/
public class ConnUtil {
public static final String url = "jdbc:mysql://XXX.XXX.XXX.XXX:3306/dbadapter";
public static final String user = "root";
public static final String password = "XXXXXX";
/**
* 得到连接
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public static Connection establishConn() throws SQLException,ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection(url, user, password);
}
/**
* 关闭连接
* @param conn
* @throws SQLException
*/
public static void close(Connection conn) throws SQLException{
if(conn != null){
conn.close();
conn = null;
}
}
/**
* 关闭PreparedStatement
* @param pstmt
* @throws SQLException
*/
public static void close(PreparedStatement pstmt) throws SQLException{
if(pstmt != null){
pstmt.close();
pstmt = null;
}
}
/**
* 关闭结果集
* @param rs
* @throws SQLException
*/
public static void close(ResultSet rs) throws SQLException{
if(rs != null){
rs.close();
rs = null;
}
}
}
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @className: ConnUtil.java
* @classDescription: 数据库连接工具类——包含取得连接和关闭资源
* @function:
* @author: Wentasy
* @createTime: 2012-9-24 上午11:51:15
* @modifyTime:
* @modifyReason:
* @since: JDK 1.6
*/
public class Conn
补充:软件开发 , Java ,