Java DataBase Connection
来源:互联网 发布:中易智联软件多少钱 编辑:程序博客网 时间:2024/04/30 15:34
// DB.java
import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;//封装DB,增强程序维护的维护性public class DB {private final static String DRIVER_CLASS="com.mysql.jdbc.Driver";//驱动类路径private final static String URL="jdbc:mysql://127.0.0.1:3306/test";//连接字符串private final static String USER="root";//数据库用户名private final static String PASSWORD="root";//数据库密码//获得连接Connectionpublic static Connection getConnection() {Connection conn = null;try {Class.forName(DRIVER_CLASS);//建立连接conn = DriverManager.getConnection(URL,USER,PASSWORD);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}return conn;}//获得语句对象Statementpublic static Statement getStatement(Connection conn) {Statement stmt = null; try {if(conn != null) {stmt = conn.createStatement();}} catch (SQLException e) {e.printStackTrace();}return stmt;}//获得语句对象PreparedStatementpublic static PreparedStatement getPreparedStatement(Connection conn,String sql) {PreparedStatement ps = null; try {if(conn != null) {ps = conn.prepareStatement(sql);}} catch (SQLException e) {e.printStackTrace();}return ps;}//获得结果集ResultSetpublic static ResultSet getResultSet(Statement stmt, String sql) {ResultSet rs = null;try {if(stmt != null) {rs = stmt.executeQuery(sql);}} catch (SQLException e) {e.printStackTrace();}return rs;}//获得结果集ResultSetpublic static int getResultInt(Statement stmt, String sql) {int cnt = 0;try {if(stmt != null) {cnt = stmt.executeUpdate(sql);}} catch (SQLException e) {e.printStackTrace();}return cnt;}//关闭连接public static void closeConnection(Connection conn) {try {if(conn != null) {conn.close();conn = null;}} catch (SQLException e) {e.printStackTrace();}}//关闭语句对象Statementpublic static void closeStatement(Statement stmt) {try {if(stmt != null) {stmt.close();stmt = null;}} catch (SQLException e) {e.printStackTrace();}}//关闭结果集ResultSetpublic static void closeResultSet(ResultSet rs) {try {if(rs != null) {rs.close();rs = null;}} catch (SQLException e) {e.printStackTrace();} }}
//Test.java
class Test{public static void main(String args []){DB.getConnection(); }}
- Java Database Connection
- Java DataBase Connection
- java database connection pool 备用
- JDBC(Java Database Connection) 教程
- DataBase Connection
- Connection to MySQL Database with Java
- SQL聚合函数--JDBC(Java DataBase Connection)
- aspdotnet connection database compare
- DataBase Connection in Struts
- Oracle Database Connection Error
- Oracle database connection problem
- DBCP (Database Connection Pool) Configurations
- Database Connection Pooling with Tomcat
- Database connection in SOAP UI
- database connection pool for mysql
- Error establishing a database connection!
- Setting up a Database Connection
- BlToolKit database connection timeout exception
- 使用Lua CJSON库进行encode与decode操作完成对Json数据转化
- HMM学习2之前向算法(转)
- poj 1144
- 受限的操作下迭代技巧之一道腾讯笔试题
- IOS 常用的宏定义(一)
- Java DataBase Connection
- 浏览器内核分析
- 日历函数
- C++中指针和引用的区别
- DB2 oralce mysql 取前十条记录对比
- 效率利器2-tmux
- Android Notes 00 - Services
- VC6.0用单文档调用对话框绘图
- Fedora 17删除旧内核的操作