📄 databaseoperation.java
字号:
package servercoedit;
import java.io.*;
import java.net.*;
import java.util.Vector;
import java.util.*;
import java.sql.*;
import javax.sql.*;
import java.io.*;
import oracle.jdbc.driver.*;
import javax.naming.*;
/**
* <p>Title: CoEditServer</p>
* <p>Description: 服务器</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: zhhz</p>
* @author 景栋盛
* @version 1.0
*/
public class DatabaseOperation {
public DatabaseOperation() {
}
public Connection getConnection() {
Connection con = null;
try {
Properties conProps = new Properties();
conProps.put("user", "sys");
conProps.put("password", "sys");
conProps.put("internal_logon", "sysdba");
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:thin:@210.29.175.35:1521:ZHHZ", conProps);
}
catch (Exception e) {}
return con;
}
public int getUserID(String userName) { //输入用户名,返回用户ID,测试通过
int userI = 0;
try {
Connection cn = getConnection();
Statement sql = cn.createStatement();
ResultSet rs = sql.executeQuery("select * from ZHHZ436.user_list");
while (rs.next()) {
if (rs.getString(2).compareTo(userName) == 0) {
userI = rs.getInt(1);
}
}
}
catch (Exception e) {}
return (userI);
}
public String getDocName(int numdoc) throws Exception { //输入文件ID,返回文件名
Connection cn = getConnection();
int numDoc = numdoc;
String strNameDoc = "";
String queryStr = "select * from ZHHZ436.DOCUMENT where DOC_ID='" + numDoc +
"'";
Statement sql = cn.createStatement();
ResultSet rs = sql.executeQuery(queryStr);
if (rs.next()) {
strNameDoc = rs.getString(2);
}
return strNameDoc;
}
public Vector getRoleObject(int userid) { //输入用户ID,返回文档ID和Block ID
//test success!
Vector vRole = new Vector();
Connection cn = getConnection();
String queryStr = "select * from ZHHZ436.role_object";
int userID = userid;
if (cn != null) {
try {
Statement sql = cn.createStatement();
ResultSet rs = sql.executeQuery(queryStr);
while (rs.next()) {
if (rs.getInt(4) == userID) {
//System.out.println("rs success!");//
Vector BR_ID = new Vector();
int numD = Integer.parseInt(rs.getObject(5).toString());
BR_ID.addElement(rs.getObject(5)); //Document ID
BR_ID.addElement(getDocName(numD)); //Doc Name
BR_ID.addElement(rs.getObject(6)); //Block ID
vRole.addElement(BR_ID);
}
}
}
catch (Exception e) {}
return vRole;
}
else {
return null;
}
}
public String getRoleObject(int userid, int docid) { //测试通过
int userID = userid;
int docID = docid;
String objectRole = "";
Connection cn = getConnection();
if (cn != null) {
try {
Statement sql = cn.createStatement();
ResultSet rs = sql.executeQuery("select * from ZHHZ436.role_object");
while (rs.next()) {
if (rs.getInt(4) == userID) {
if (rs.getInt(5) == docID) {
objectRole = rs.getString(2);
}
}
}
}
catch (Exception e) {}
}
return objectRole;
}
public String getBaseRole(String roleobject) { //输入角色名,返回基本权限名,测试通过
Connection cn = getConnection();
String roleObject = roleobject;
int baseRoleID = 0;
String baseRole = "";
//System.out.print("test1");
try {
Statement sql = cn.createStatement();
ResultSet rs = sql.executeQuery("select * from ZHHZ436.role_object");
//System.out.print("test2");
while (rs.next()) {
if (roleObject.equals(rs.getString(2))) { //java String equals
baseRoleID = rs.getInt(3);
//System.out.print("rs success");
}
}
}
catch (Exception e) {}
if (baseRoleID != 0) {
try {
Statement sql = cn.createStatement();
ResultSet rs = sql.executeQuery("select * from ZHHZ436.base_role");
while (rs.next()) {
if (rs.getInt(1) == baseRoleID) {
baseRole = rs.getString(2);
}
}
}
catch (Exception e) {}
return baseRole;
}
return "";
}
public int getBaseRoleID(String roleobject) { //输入角色名,返回权限ID
Connection cn = getConnection();
String roleObject = roleobject;
int baseRoleID = 0;
//String baseRole = "";
//System.out.print("test1");
try {
Statement sql = cn.createStatement();
ResultSet rs = sql.executeQuery("select * from ZHHZ436.role_object");
//System.out.print("test2");
while (rs.next()) {
if (roleObject.equals(rs.getString(2))) { //java String equals
baseRoleID = rs.getInt(3);
//System.out.print("rs success");
}
}
}
catch (Exception e) {}
return baseRoleID;
}
public void giveEditBlockPer(String userName, int docID, int blockID) {
//give someone the right to edit a block
int userID = 0, baseRoleID = 0, i = 0, id = 0, new_roleID = 0;
String roleObject = "ROLE_" + userName;
userID = getUserID(userName);
baseRoleID = getBaseRoleID(roleObject);
try {
Connection con = getConnection();
String query1 = "SELECT * FROM ZHHZ436.ROLE_OBJECT";
Statement stmt = con.createStatement();
ResultSet results = stmt.executeQuery(query1);
boolean more = results.next();
while (more) {
i = results.getInt("RO_ID");
if (i > id) {
id = i;
}
more = results.next();
}
}
catch (Exception e) {}
try {
Connection con = getConnection();
new_roleID = id + 1;
String query2 = "INSERT INTO ZHHZ436.ROLE_OBJECT VALUES(" + new_roleID +
",'" +
roleObject + "'," + baseRoleID + "," + userID + "," + docID + "," +
blockID + ")";
Statement stmt = con.createStatement();
stmt.executeQuery(query2);
}
catch (Exception e) {}
}
public void reclaimEditBlockRight(String userName, int docID, int blockID) {
//reclaim the right to edit a block
String roleObject = "ROLE_" + userName;
try {
Connection con = getConnection();
String query = "DELETE FROM ZHHZ436.ROLE_OBJECT WHERE RO_NAME='" +
roleObject + "' AND DOC_ID=" + docID + " AND BLOCK_ID=" + blockID +
"";
Statement stmt = con.createStatement();
stmt.executeQuery(query); //执行删除操作
}
catch (Exception e) {}
}
public static void main(String[] args) {
DatabaseOperation t = new DatabaseOperation(); //新建一个类
//System.out.print(t.getUserID("DGDU")); //测试方法
//String s = "";
//try {
//s = t.getDocName(436);
//}
//catch (Exception e) {
//}
//System.out.print(s);
//System.out.print(t.getRoleObject(46));
//System.out.print(t.getRoleObject(46, 436));
//System.out.print(t.getBaseRole("ROLE_SBWANG"));
//t.giveEditBlockPer("WFSHAO", 436, 3);
//t.reclaimEditBlockRight("WFSHAO", 436, 3);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -