/**
* 创建oracle用户
* */
public void createOrcleUser(String server, String userName, String password) {
String strsql = "create user " + userName + " identified by "
+ password + "";
DBoperator dboperator = new DBoperator(url, defaultloginname,
defaultpassword);
try {
dboperator.executeNonQuery(strsql);
dboperator.executeNonQuery("grant connect,resource,dba to "
+ userName + "");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
下面是DBoperator类:
package com.DB;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBoperator {
private ConnectionFactory conn;
private String username;
private String password;
public DBoperator(String url,String username, String password) {
this.username = username;
this.password = password;
conn = ConnectionFactory.instance();
conn.setUrl(url);
}
// 查询语句
public ResultSet executeQuery(String sql) throws SQLException {
Connection con;
try {
con = conn.createConnection(username, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
conn.releaseConnection();
return rs;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
// 验证是否存在查询数据
public boolean checkDataExis(String sql){
Connection con;
Statement stmt;
int count = 0;
try {
con = conn.createConnection(username, password);
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next())
count = rs.getInt(1);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
finally{
conn.releaseConnection();
}
boolean blrs = count == 0 ? false : true;
return blrs;
}
public ResultSet executeQuery(String sql, Object... obj)
throws SQLException {
Connection con;
ResultSet rs;
try {
con = conn.createConnection(username, password);
PreparedStatement pstmt = con.prepareStatement(sql);
for (int i = 0; i < obj.length; i++) {
pstmt.setObject(i + 1, obj[i]);
}
rs = pstmt.executeQuery();
conn.releaseConnection();
return rs;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
// 执行增删改
public int executeNonQuery(String sql) throws SQLException {
Connection con;
try {
con = conn.createConnection(username, password);
Statement stmt = con.createStatement();
int rs = stmt.executeUpdate(sql);
conn.releaseConnection();
return rs;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
}
public int executeNonQuery(String sql, Object... obj) throws SQLException {
Connection con;
try {
con = conn.createConnection(username, password);
PreparedStatement pstmt = con.prepareStatement(sql);
for (int i = 0; i < obj.length; i++) {
pstmt.setObject(i + 1, obj[i]);
}
int rs = pstmt.executeUpdate();
conn.releaseConnection();
return rs;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
}
}