package com.wgh.DB;
import java.sql.*;
import java.io.*;
import java.util.*;
public class connDB {
public Connection conn = null;
public Statement stmt = null;
public ResultSet rs = null;
private static String propFileName = "C:/connDB.properties";
private static Properties prop = new Properties();
private static String dbClassName =
"com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static String dbUrl =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_shop";
private static String dbUser = "sa";
private static String dbPwd = "";
private static void loadProperty() {
try {
prop.load(new FileInputStream(propFileName)); //通过文件输入流对象加载Properties文件
dbClassName = prop.getProperty("DB_CLASS_NAME");
dbUrl = prop.getProperty("DB_URL",
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_shop");
dbUser = prop.getProperty("DB_USER", "sa");
dbPwd = prop.getProperty("DB_PWD", "");
}
catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn = null;
loadProperty();
try {
Class.forName(dbClassName).newInstance();
conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
}
catch (Exception ee) {
ee.printStackTrace();
}
if (conn == null) {
System.err.println(
"警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:" +
dbClassName + "\r\n链接位置:" + dbUrl + "\r\n用户/密码" + dbUser + "/" +
dbPwd);
}
return conn;
}
/*
*功能:执行查询语句
*/
public ResultSet executeQuery(String sql) {
try {
conn = getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch (SQLException ex) {
System.err.println(ex.getMessage());
}
return rs;
}
/*
*功能:执行更新操作
*/
public int executeUpdate(String sql) {
int result = 0;
try {
conn = getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
result = stmt.executeUpdate(sql);
}
catch (SQLException ex) {
result = 0;
}
try {
stmt.close();
}
catch (SQLException ex1) {
}
return result;
}
public int executeUpdate_id(String sql) {
int result = 0;
try {
conn = getConnection();
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
result = stmt.executeUpdate(sql);
String ID = "select @@IDENTITY as id";
rs = stmt.executeQuery(ID);
if (rs.next()) {
int autoID = rs.getInt("id");
result = autoID;
}
}
catch (SQLException ex) {
result = 0;
}
return result;
}
/*
*功能:关闭数据库的连接
*/
public void close() {
try {
if (rs != null) {
rs.close();
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}
try {
if (stmt != null) {
stmt.close();
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}
try {
if (conn != null) {
conn.close();
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
网上购书系统 电子商务网站
4星 · 超过85%的资源 需积分: 9 30 浏览量
2009-06-03
12:22:54
上传
评论 1
收藏 1.78MB RAR 举报
guanmu09
- 粉丝: 1
- 资源: 2
最新资源
- maven下载、安装、配置与使用教程(windows、Linux)
- html css网页制作成品案例.zip
- 神经网络介绍及教程&案例.docx
- 重庆市首席信息官(CIO)协会 《软件及信息化工程造价规范V5.0》T/CQCIO 001-2019
- 联想Y471老A卡笔记本开机黑屏 需等待黑屏结束才能进入桌面,解决方法
- elasticsearch-7.14.0和ik分词器
- 利用MPI计算任意范围内的质数
- EnviroSkyandWeather v2.1.1(u2017.1.2)真实动态天气系统包
- React框架介绍及相关教程、案例.docx
- 基于Springboot和Vue教务评教系统(PC端+server端源码+数据库MySQL脚本+环境部署步骤讲解+运行步骤讲解)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈