package common;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;java
/*
* 鏈接數據庫和經常使用數據庫操做的類
*/
import oracle.sql.CLOB;sql
/**
* 鏈接數據庫的類
* 最基礎的類
*
*/
public class text {數據庫
/**
* 鏈接數據庫經常使用的屬性
*
*
*/
public String sDBDriver = "oracle.jdbc.driver.OracleDriver";// 設置驅動
public String url = "jdbc:oracle:thin:@localhost :spe";// 設置數據庫信息
public String sHint = "";
public ResultSet rs = null;
public Connection conn = null;
public Statement stmt = null;
public String user = "study";
public String pwd = "ok";
/**
* 加載配置文件讀取信息
*
*
*/
public text() throws IOException {oracle
String userpath = System.getProperty("user.dir");
String filesparator = System.getProperty("file.separator");
String dbconfig = userpath + filesparator + "dbconfig.properties";
FileInputStream in;
in = new FileInputStream(dbconfig);
Properties dbp = new Properties();
dbp.load(in);
in.close();
this.url = dbp.getProperty("URL");
this.user = dbp.getProperty("USER");
this.pwd = dbp.getProperty("PASSWORD");
System.out.println("地址=" + dbconfig);
// System.out.println("真實="+this.url);app
}this
public String getSHint() {
return sHint;
}url
public void setSDBDriver(String dbDriver) {
sDBDriver = dbDriver;
}spa
public String getSDBDriver() {
return sDBDriver;
}.net
public String getUrl() {
return url;
}對象
public void setUrl(String s) {
url = s;
}
public ResultSet getResultSet() {
return rs;
}
public boolean initialize(String url, String user, String pwd) {
this.url = url;
this.user = user;
this.pwd = pwd;
return initialize();
}
public boolean initialize() {// 默認構造方法
try {
Class.forName(sDBDriver);
sHint = "Initialization sucessfully";
return true;
} catch (ClassNotFoundException e) {
sHint = "Initialization Exception:" + e.getMessage();
return false;
}
}
/**
* 關閉數據庫對象
*
*
*/
public boolean close() {// 關閉數據庫鏈接
try {
if (rs != null)
{ rs.close();}
if (stmt != null)
{stmt.close();}
if (conn != null)
{conn.close();}
return true;
} catch (SQLException ex) {
sHint = "Close Exception:" + ex.getMessage();
return false;
}
}
/**
* 插入大字段方法
*
*
*/
public String insertClob(int userid, int courseware_Id, int Progress,
String CourseClob) {// 插入數據庫clob字段
try {
// int testid = 77;
// System.out.println("113用戶ID" + userid + "課件ID" + courseware_Id+
// "進度" + Progress + "value=" + CourseClob);
Class.forName(this.sDBDriver);
Connection conn = DriverManager.getConnection(this.url, this.user,
this.pwd);
conn.setAutoCommit(false);
/* 如下表User_CourseWare中的Report字段時CLOB類型的 */
// 插入一條數據,注意CLOB字段,須要先插入一個空的clob類型 empty_clob(),而後再單獨更新clob字段
// String sql = "insert into
// User_CourseWare(User_Id,Courseware_Id,Progress,Report,Id)values(
// ?,?,?,empty_clob(),?)";
String sql = "insert into User_CourseWare(User_Id,Courseware_Id,Progress,Report ,id)values( ?,?,?,empty_clob(), user_courseware_sq.nextval )";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, userid);
pstmt.setInt(2, courseware_Id);
pstmt.setInt(3, Progress);
// System.out.println("sql insert=" + sql);
// pstmt.setInt(4, testid);
int i1 = pstmt.executeUpdate();
conn.commit();
pstmt = null;
if (i1 > 0) {
// System.out.println("用戶ID" + userid + "插入" + courseware_Id+
// "課件成功");
}
ResultSet rs = null;
CLOB clob = null;
String sql1 = "select Report from User_CourseWare where User_Id=? and Courseware_Id=? for update";
pstmt = conn.prepareStatement(sql1);
/*
* pstmt.setInt(1, testid); pstmt.setInt(2, userid); pstmt.setInt(3,
* courseware_Id);
*/
// System.out.println("sql1 select=" + sql1);
pstmt.setInt(1, userid);
pstmt.setInt(2, courseware_Id);
rs = pstmt.executeQuery();
if (rs.next()) {
clob = (CLOB) rs.getClob(1);
}
Writer writer = clob.getCharacterOutputStream();
writer.write(CourseClob);
writer.flush();
writer.close();
rs.close();
conn.commit();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
return "error";
}
return "success";
}
/**
* 得到大字段XML
* 得到大字符串格式
*
* @param user_id
* 用戶ID
* @param courseware_id
* 課件ID
* @return 大字符串
*
*/
public String getCourseClob(int user_id, int courseware_id) {// 根據課件ID和人ID查詢課程ID
String content = "null";
try {
Class.forName(this.sDBDriver);
Connection conn = DriverManager.getConnection(this.url, this.user,
this.pwd);
conn.setAutoCommit(false);
ResultSet rs = null;
CLOB clob = null;
String sql = "";
sql = "select Report from User_CourseWare where user_id=? and courseware_id=? ";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, user_id);
pstmt.setInt(2, courseware_id);
rs = pstmt.executeQuery();
if (rs.next()) {
clob = (CLOB) rs.getClob(1);
if (clob != null && clob.length() != 0) {
content = clob.getSubString((long) 1, (int) clob.length());
content = this.Clob2String(clob);
}
}
rs.close();
conn.commit();
pstmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
// return "null";
content = "error";
} catch (SQLException e) {
e.printStackTrace();
// return "null";
content = "error";
}
return content;
}
/**
* clob to string
* 大字符串格式轉換STRING
* @param clob
* @return 大字符串
*
*/
public String Clob2String(CLOB clob) {// Clob轉換成String 的方法
String content = null;
StringBuffer stringBuf = new StringBuffer();
try {
int length = 0;
Reader inStream = clob.getCharacterStream(); // 取得大字側段對象數據輸出流
char[] buffer = new char[10];
while ((length = inStream.read(buffer)) != -1) // 讀取數據庫 //每10個10個讀取
{
for (int i = 0; i < length; i++) {
stringBuf.append(buffer[i]);
}
}
inStream.close();
content = stringBuf.toString();
} catch (Exception ex) {
System.out.println("ClobUtil.Clob2String:" + ex.getMessage());
}
return content;
}
/**
*
* 更新Clob(大字符串格式)內容
* @param userid
* 用戶ID
* @param courseware_Id
* 課件ID
* @param Progress
* 課件進度
* @param CourseClob
* XML 字符串
* @return boolean
*
*/
public String updateClob(int userid, int courseware_Id, int Progress,
String CourseClob) {
this.updateUser_Course(userid, courseware_Id, Progress);// 調用更新進度
try {
Class.forName(this.sDBDriver);
Connection conn = DriverManager.getConnection(this.url, this.user,
this.pwd);
String sql = "update User_CourseWare set Report=empty_clob() , Progress=? where User_Id = ? and Courseware_Id =? ";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, Progress);
pstmt.setInt(2, userid);
pstmt.setInt(3, courseware_Id);
int i1 = pstmt.executeUpdate();
conn.commit();
// System.out.println("update sql="+sql);
pstmt = null;
if (i1 > 0) {
}
ResultSet rs = null;
CLOB clob = null;
String sql1 = "select Report from User_CourseWare where User_id=? and Courseware_id=? for update";
pstmt = conn.prepareStatement(sql1);
// System.out.println("select sql="+sql1);
pstmt.setInt(1, userid);
pstmt.setInt(2, courseware_Id);
rs = pstmt.executeQuery();
if (rs.next()) {
clob = (CLOB) rs.getClob(1);
}
Writer writer = clob.getCharacterOutputStream();
writer.write(CourseClob);
writer.flush();
writer.close();
rs.close();
conn.commit();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
return "error";
}
return "success";
}
/*
* 參數:userid 用戶ID courseware_Id 課件ID Progress 課件進度 返回值 課件進度不到100時不更新 到100時
* 更新 狀態爲2(已學)
*/
public String updateUser_Course(int userid, int courseware_Id, int Progress) {
String caseTest = "default";
if (Progress == 100) {
String sql = "update User_Course set Status =2 where USER_ID =? and COURSE_ID =? ";
try {
Class.forName(this.sDBDriver);
Connection conn = DriverManager.getConnection(this.url,
this.user, this.pwd);
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, userid);
pstmt.setInt(2, courseware_Id);
int i1 = pstmt.executeUpdate();
conn.commit();
pstmt = null;
if (i1 > 0) {
// System.out.println("更新成功!");
}
caseTest = "success";
} catch (Exception e) {
caseTest = "error";
e.printStackTrace();
}
}
return caseTest;
}
/**
*
* @param sql
* 要執行的查詢語句
* @return 結果集
*/
public ResultSet executeQuery(String sql) {
rs = null;
try {
Class.forName(this.sDBDriver);
conn = DriverManager.getConnection(this.url, this.user, this.pwd);
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (Exception ex) {
sHint = "Query Exception:" + ex.getMessage();
}
return rs;
}
/**
* 執行 更新刪除等語句
*
* @param sql
* @return 返回執行結果的boolean值
*/
public boolean executeUpdate(String sql) {
try {
Class.forName(this.sDBDriver);
conn = DriverManager.getConnection(this.url, this.user, this.pwd);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
conn.commit();
return true;
} catch (Exception ex) {
sHint = "Update Exception :" + ex.getMessage();
return false;
}
}
}