📄 sqlstatement.java
字号:
package com.cmmi2pms.common.comdb;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* The Statement Class which is execute a sql
*
* @author xunzy
* @version 0.01
*
*/
public class SqlStatement
{
//Internal JDBC objects
private Connection conn = null;
private PreparedStatement pstmt = null;
//Holds the SQL statement for execution
private String sql;
/**
* Default constructor
*/
public SqlStatement(){}
//Initialize the Statement object
private void initStatement() throws SQLException
{
//Only initialize PrepareStatement if member sql is not null.
if(sql==null)
{
throw new SqlStatementException("SQL string is null");
}
pstmt = conn.prepareStatement(sql);
}
/**
* Close PreparedStatement object
*
* @throws SQLException
*/
public void close() throws SQLException
{
if(pstmt!=null)
{
System.out.println("Closing SqlStatement!");
pstmt.close();
pstmt=null;
}
}
/**
* Used to set SQL statement and reinitialize PreparedStatement object
*
* @param sql
* @throws SQLException
*/
public void setSql(String sql) throws SQLException
{
this.sql=sql;
pstmt=null;
initStatement();
}
/**
* Returns the current SQL statement
*
* @return String
*/
public String getSql()
{
return sql;
}
/**
* Executes static SQL statement supplied as a parameter
*
* @param sql
* @return ResultSet
* @throws SQLException
*/
public ResultSet executeQuery(String sql) throws SQLException
{
setSql(sql);
return executeQuery();
}
/**
* Executes an SELECT statement
*
* @return ResultSet
* @throws SQLException
*/
public ResultSet executeQuery() throws SQLException
{
//Check to see if pstmt statement is null.
if(pstmt ==null)
{
throw new SqlStatementException("PreparedStatement not initialized");
}
return pstmt.executeQuery();
}
/**
* <p>Executes static UPDATE, INSERT, or DELETE statement
* supplied as a parameter
*
* @param sql
* @return int The counts which the sql updates , deletes or inserts
* @throws SQLException
*/
public int execute(String sql) throws SQLException
{
setSql(sql);
return execute();
}
/**
* Executes a SQL UPDATE, INSERT, or DELETE statement
*
* @return int The counts which the sql updates , deletes or inserts
* @throws SQLException
*/
public int execute() throws SQLException
{
if(pstmt ==null)
{
throw new SqlStatementException("PreparedStatement not initialized");
}
int count = pstmt.executeUpdate();
return count;
}
/**
* Sets the Connection object to valid database connection
*
* @param conn
*/
public void setConnection(Connection conn)
{
this.conn = conn;
}
//The following set methods set the appropriate value in a PreparedStatement.
/**
* The set methods set the string value in a PreparedStatement.
*
* @param index
* @param value
* @throws SQLException
*/
public void setString(int index, String value) throws SQLException
{
pstmt.setString(index,value);
}
/**
* The set methods set the int value in a PreparedStatement.
*
* @param index
* @param value
* @throws SQLException
*/
public void setInt(int index, int value) throws SQLException
{
pstmt.setInt(index,value);
}
/**
* The set methods set the double value in a PreparedStatement.
*
* @param index
* @param value
* @throws SQLException
*/
public void setDouble(int index, double value) throws SQLException
{
pstmt.setDouble(index,value);
}
/**
* The set methods set the date value in a PreparedStatement.
*
* @param index
* @param value
* @throws SQLException
*/
public void setDate(int index, Date value) throws SQLException
{
pstmt.setDate(index,value);
}
//End set methods
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -