连接数据库工具类调用程序
2015-09-28 12:17
483 查看
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Test.util;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author Administrator
*/
public class JDBCUtil {
//driverclass=com.mysql.jdbc.Driver
public static final String DRIVER="com.microsoftsqlserverjdbc.SQlServerDriver";
//jdbcurl=JDBC:mysql://127.0.0.1:3306/selectivesys?characterEncoding=UTF-8
public static final String URL="jdbc:sqlserver://localhost1433;databasename=selectivedb";
public static final String USER="root";
public static final String PWD="123456";
//构造方法调用驱动
public JDBCUtil(){
try {
Class.forName(DRIVER);
} catch (Exception e) {
e.printStackTrace();
}
}
//创建链接
public static Connection getConnection() throws SQLException{
new JDBCUtil();
Connection conn=null;
conn=DriverManager.getConnection(URL, USER,PWD);
return conn;
}
//关闭连接
public static void closeConn(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//关闭执行对象
public static void closeStatement(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//关闭结果集
public static void closeResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Test.util;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author Administrator
*/
public class JDBCUtil {
//driverclass=com.mysql.jdbc.Driver
public static final String DRIVER="com.microsoftsqlserverjdbc.SQlServerDriver";
//jdbcurl=JDBC:mysql://127.0.0.1:3306/selectivesys?characterEncoding=UTF-8
public static final String URL="jdbc:sqlserver://localhost1433;databasename=selectivedb";
public static final String USER="root";
public static final String PWD="123456";
//构造方法调用驱动
public JDBCUtil(){
try {
Class.forName(DRIVER);
} catch (Exception e) {
e.printStackTrace();
}
}
//创建链接
public static Connection getConnection() throws SQLException{
new JDBCUtil();
Connection conn=null;
conn=DriverManager.getConnection(URL, USER,PWD);
return conn;
}
//关闭连接
public static void closeConn(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//关闭执行对象
public static void closeStatement(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//关闭结果集
public static void closeResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
相关文章推荐
- MySQL InnoDB MVCC实现原理
- 允许远程用户登录访问mysql的方法
- SQL Server 游标运用:查看数据库所有表大小信息
- mongodb的简单使用二(结合springdata实现增删改查)
- oracle锁表
- MySQL重连,连接丢失问题解决
- MySQL表与数据向Oracle移植(使用Oracle SQL Developer工具)
- Mysql CPU占用高的问题解决方法小结
- oracle索引的理解
- 关于连接多个数据库的问题
- 记一次公司仓库数据库服务器死锁过程及解决办法
- 发现一个oracle11 g 新特性挺有意思
- 转 -- mariadb galera集群配置
- 【oracle ocp 知识点二】
- 渗透学习笔记--基础篇--sql注入(数字型)
- Ubuntu下Postgresql卸载出错的解决办法
- 数据库SQL优化总结 学习必看
- 渗透学习笔记--基础篇--sql注入(字符型)
- SQL语句优化 (二) (53)
- Mysql主从复制原理