package dbconn;
import java.sql.*;
import java.lang.ClassNotFoundException;
public class BaseConn {
private Connection conn = null;
private Statement stmt = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
public BaseConn() throws SQLException,ClassNotFoundException{
try
{
String url = "jdbc:mysql://localhost:3306/classmanage";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,"root","root");
}catch(SQLException e)
{
System.out.println("Error occured when Connect DataBase:"+e);
throw e;
}
catch(ClassNotFoundException e)
{
System.out.println("Error occured when Connect DataBase:"+e);
throw e;
}
}
public PreparedStatement preparedStatement(String sql) throws SQLException
{
try
{
ps = conn.prepareStatement(sql);
return ps;
}catch(SQLException e)
{
System.out.println("Error occured when Connect DataBase:"+e);
throw e;
}
}
public static Connection getConnection() {
String driverClass ="com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/classmanage";
String userName = "root";
String password ="root";
System.out.println(driverClass + "\t" + url + "\t" + userName + "\t" + password);
Connection conn = null;
try {
Class.forName(driverClass);
conn = DriverManager.getConnection(url, userName, password);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
/**
* 执行静态 SQL 查询语句并返回它所生成结果的对象
* */
public ResultSet executeQuery(String sql) throws SQLException
{
rs = null;
try {
rs = stmt.executeQuery(sql);
}
catch (SQLException ex) {
System.out.println("Error occured when query database:" + ex);
throw ex;
}
return rs;
}
/**
* 执行静态SQL更新语句并返回影响数据条数
* */
public int executeUpdate(String sql) throws SQLException
{
try {
conn.setAutoCommit(false);
int re = stmt.executeUpdate(sql);
conn.commit();
return re;
}
catch (SQLException e) {
conn.rollback();
System.out.println("Error occured when update database:" + e);
throw e;
}
}
/**
* 执行预编译的SQL查询语句
*
* */
public ResultSet executeQuery() throws SQLException
{
try {
return ps.executeQuery();
}
catch (SQLException e) {
System.out.println("Error occured when query database:" + e);
throw e;
}
}
/**
* 执行预编译的SQL更新语句
*
* */
public int executeUpdate() throws SQLException
{
try {
conn.setAutoCommit(false);
int r = ps.executeUpdate();
conn.commit();
return r;
}
catch (SQLException e) {
conn.rollback();
System.out.println("Error occured when update database:" + e);
throw e;
}
}
/**
* 数据库关闭操作
* */
public boolean closeDB() throws SQLException
{
try {
if (this.rs != null)
rs.close();
if (this.stmt != null)
this.stmt.close();
if (this.ps != null)
this.ps.close();
if (this.conn != null)
conn.close();
return true;
}
catch (SQLException e) {
System.out.println("Error occured when close database:" + e);
throw e;
}
}
}
学生成绩管理系统
5星 · 超过95%的资源 需积分: 21 155 浏览量
2012-12-08
20:02:42
上传
评论 40
收藏 19KB ZIP 举报
hacke2
- 粉丝: 241
- 资源: 15
最新资源
- 基于matlab实现图像处理,本程序使用背景差分法对来往车辆进行检测和跟踪.rar
- 基于matlab实现视频监控中车型识别代码,自己写的,希望和大家多多交流.rar
- sdk.config
- 基于matlab实现配电网三相潮流计算方法,对几种常用的配电网潮流计算方法进行了对比分析.rar
- 基于matlab实现配电网潮流 经典33节点 前推回代法潮流计算 回代电流 前推电压 带注释.rar
- 基于matlab实现模拟退火遗传算法的车辆调度问题研究,用MATLAB语言加以实现.rar
- 基于matlab实现蒙特卡洛的的移动传感器节点定位算法仿真代码.rar
- 华中数控系统818用户说明书
- 基于matlab实现卡尔曼滤波器完成多传感器数据融合 对多个机器人的不同传感器数据进行融合估计足球精确位置.rar
- 基于matlab实现进行简单车辆识别-车辆检测.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页