DBUtil java 链接sqlserver 数据库
2015-11-25 10:41
453 查看
package com.util.base;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DBUtils {
private static final String DBDRIVER = "net.sourceforge.jtds.jdbc.Driver";// 驱动类类名
private static final String DBNAME = "database";// 数据库名
private static final String DBURL = "jdbc:jtds:sqlserver://localhost:1433/" + DBNAME;// 连接URL
private static final String DBUSER = "sa";// 数据库用户名
private static final String DBPASSWORD = "123456";// 数据库密码
private static Connection conn = null;
private static PreparedStatement ps = null;
private static ResultSet rs = null;
// 获取数据库连接
public static Connection getConnection() {
try {
Class.forName(DBDRIVER);// 注册驱动
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);// 获得连接对象
} catch (ClassNotFoundException e) {// 捕获驱动类无法找到异常
e.printStackTrace();
} catch (SQLException e) {// 捕获SQL异常
e.printStackTrace();
}
return conn;
}
// 查询数据
public ResultSet select(String sql) throws Exception {
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery(sql);
return rs;
} catch (SQLException sqle) {
throw new SQLException("select data Exception: " + sqle.getMessage());
} catch (Exception e) {
throw new Exception("System error: " + e.getMessage());
}
}
// 插入数据
public int insert(String sql) throws Exception {
int num = 0;// 计数
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
num = ps.executeUpdate();
} catch (SQLException sqle) {
throw new SQLException("insert data Exception: " + sqle.getMessage());
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {
throw new Exception("ps close exception: " + e.getMessage());
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("conn close exception: " + e.getMessage());
}
}
return num;
}
// 删除数据
public int delete(String sql) throws Exception {
int num = 0;// 计数
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
num = ps.executeUpdate();
} catch (SQLException sqle) {
throw new SQLException("delete data Exception: " + sqle.getMessage());
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {
throw new Exception("ps close Exception: " + e.getMessage());
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("conn close Exception: " + e.getMessage());
}
}
return num;
}
// 修改数据
public int update(String sql) throws Exception {
int num = 0;// 计数
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
num = ps.executeUpdate();
} catch (SQLException sqle) {
throw new SQLException("update data Exception: " + sqle.getMessage());
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {
throw new Exception("ps close Exception: " + e.getMessage());
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("conn close Excepiton: " + e.getMessage());
}
}
return num;
}
}
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DBUtils {
private static final String DBDRIVER = "net.sourceforge.jtds.jdbc.Driver";// 驱动类类名
private static final String DBNAME = "database";// 数据库名
private static final String DBURL = "jdbc:jtds:sqlserver://localhost:1433/" + DBNAME;// 连接URL
private static final String DBUSER = "sa";// 数据库用户名
private static final String DBPASSWORD = "123456";// 数据库密码
private static Connection conn = null;
private static PreparedStatement ps = null;
private static ResultSet rs = null;
// 获取数据库连接
public static Connection getConnection() {
try {
Class.forName(DBDRIVER);// 注册驱动
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);// 获得连接对象
} catch (ClassNotFoundException e) {// 捕获驱动类无法找到异常
e.printStackTrace();
} catch (SQLException e) {// 捕获SQL异常
e.printStackTrace();
}
return conn;
}
// 查询数据
public ResultSet select(String sql) throws Exception {
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery(sql);
return rs;
} catch (SQLException sqle) {
throw new SQLException("select data Exception: " + sqle.getMessage());
} catch (Exception e) {
throw new Exception("System error: " + e.getMessage());
}
}
// 插入数据
public int insert(String sql) throws Exception {
int num = 0;// 计数
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
num = ps.executeUpdate();
} catch (SQLException sqle) {
throw new SQLException("insert data Exception: " + sqle.getMessage());
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {
throw new Exception("ps close exception: " + e.getMessage());
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("conn close exception: " + e.getMessage());
}
}
return num;
}
// 删除数据
public int delete(String sql) throws Exception {
int num = 0;// 计数
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
num = ps.executeUpdate();
} catch (SQLException sqle) {
throw new SQLException("delete data Exception: " + sqle.getMessage());
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {
throw new Exception("ps close Exception: " + e.getMessage());
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("conn close Exception: " + e.getMessage());
}
}
return num;
}
// 修改数据
public int update(String sql) throws Exception {
int num = 0;// 计数
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
num = ps.executeUpdate();
} catch (SQLException sqle) {
throw new SQLException("update data Exception: " + sqle.getMessage());
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {
throw new Exception("ps close Exception: " + e.getMessage());
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new Exception("conn close Excepiton: " + e.getMessage());
}
}
return num;
}
}
相关文章推荐
- MySQL从安装到使用(运用JDBC技术访问)JAVA(Eclipse)
- 【转】最好的8个 Java RESTful 框架
- java 每日习题(十一)读取配置文件
- [Java]读取文件方法大全
- Spring 整体结构
- maven jdk 版本配置
- neo4j的examples之EmbeddedNeo4jWithCustomLogging.java
- 重温Java中的接口
- 源码分析:Java堆的创建
- spring中常用工具类介绍
- java设计模式之建造者模式
- neo4j的examples之EmbeddedNeo4j.java
- Java中二元运算符被简化后带来的变化
- Eclipse中使用SVN
- eclipse如何debug调试jdk源码
- 关于Eclipse 和 IDEA 导入library库文件 的步骤
- eclipse maven 导出项目依赖的jar包
- Java 中带参无返回值方法和带参带返回值方法的使用
- java项目怎么打成可运行的jar包
- maven mvn跳过生成javadoc