📄 ompreparedquery.java
字号:
/*
* 创建日期 2005-7-16
*
* 更改所生成文件模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
package servlets;
/**
* @author Administrator
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class OmPreparedQuery
{
public Connection connection = null; //获得数据库连接
private Statement stmt = null; //创建语句
ResultSet rsExcutQuery = null;
boolean isConnection = true;
String strDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String strUrl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=DA";
String strUserName = "sa";
String strUserPassWord = "sa";
//构造函数
public OmPreparedQuery()
{
}
/**
* 功能:
* 连接数据库
* @return boolean
*/
public boolean setConnectDataBase() throws Exception
{
try
{
Class.forName(strDriver).newInstance();
connection = DriverManager.getConnection(strUrl,strUserName,strUserPassWord);
stmt = connection.createStatement();
}
catch (Exception e)
{
isConnection = false;
System.out.println("数据库连接出现如下异常:" + e.getMessage());
}
return isConnection;
}
/**
* 功能:
* 设置是否自动提交 arg = true 自动提交, arg = false 不自动提交
* @return boolean
*/
public boolean setAutoCommit(boolean arg)
{
try
{
connection.setAutoCommit(arg);
}
catch (Exception e)
{
System.out.println("设置是否自动提交出现如下异常:" + e.getMessage());
}
return isConnection;
}
/**
* 功能:
* 添加
* @param strSql sql语句
* int intRow = 1; 正常 intRow = 0; 出错
* @return int
*/
public int executeInsert(String strSql)
{
int intRow = 1; //正常
try
{
intRow = stmt.executeUpdate(strSql);
}
catch (SQLException eExcutQuery)
{
intRow = 0; //出错
System.out.println("执行添加操作出现如下异常:" + eExcutQuery.getMessage());
}
return intRow;
}
/**
* 功能:
* 查询
* @param strSql sql语句
* @return ResultSet
*/
public ResultSet executeQuery(String strSql)
{
try
{
rsExcutQuery = stmt.executeQuery(strSql);
}
catch (SQLException eExcutQuery)
{
rsExcutQuery = null;
System.out.println("执行查询操作出现如下异常:" + eExcutQuery.getMessage());
}
return rsExcutQuery;
}
/**
* 功能:
* 更新
* @param strSql sql语句
* int intRow = 1; 正常 intRow = 0; 出错
* @return int
*/
public int executeUpdate(String strSql)
{
int intRow = 1; //正常
try
{
intRow = stmt.executeUpdate(strSql);
}
catch (SQLException eExecteUpdate)
{
System.out.println("执行更新操作出现如下异常:" + eExecteUpdate.getMessage());
intRow = 0; //出错
}
return intRow;
}
/**
* 功能:
* 删除
* @param strSql sql语句
* int intRow = 1; 正常 intRow = 0; 出错
* @return int
*/
public int executeDelete(String strSql)
{
int intRow = 1; //正常
try
{
intRow = stmt.executeUpdate(strSql);
}
catch (SQLException eExecteUpdate)
{
System.out.println("执行删除操作出现如下异常:" + eExecteUpdate.getMessage());
intRow = 0; //出错
}
return intRow;
}
/**
* 功能:
* 关闭所有连接
*/
public void closeAll()
{
try
{
if(rsExcutQuery != null)
{
rsExcutQuery.close();
rsExcutQuery = null;
}
if(stmt!=null)
{
stmt.close();
stmt = null;
}
if(connection!=null)
{
connection.close();
}
}
catch(Exception e)
{
System.out.println("关闭数据出现如下异常:" + e.getMessage());
}
}
/**
* 功能:
* 提交数据
*/
public void commit()
{
try
{
connection.commit();
}
catch(Exception e)
{
System.out.println("提交数据出现如下异常:" + e.getMessage());
}
}
/**
* 功能:
* 回滚数据
*/
public void rollback()
{
try
{
connection.rollback();
}
catch(Exception e)
{
System.out.println("回滚数据出现如下异常:" + e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -