Msyql:
驱动:com.mysql.jdbc.Driver 连接地址示例:jdbc:mysql://10.0.26.14:3306/zyzx?characterEncoding=utf-8 检测数据库是否存在: SELECT information_schema.SCHEMATA.SCHEMA_NAME FROM information_schema.SCHEMATA where SCHEMA_NAME="database_name" 创建数据库:create database database_name 创建用户:CREATE USER 'mysql_user'@'%' IDENTIFIED BY 'password' # %代码所有ip地址 授权:grant all privileges on database_name.* to 'mysql_user'@'%' |
Oracle:
驱动:oracle.jdbc.driver.OracleDriver 连接地址:jdbc:oracle:thin:@localhost:1521/orcl 检测用户是否存在:(oracle只有一个数据库,在12c里面才会有多个数据库的概念,12c就有点类似于mysql只是名字起的很专业很高深,可以自行百度下,也可以看官方文档) select username from SYS.dba_users where username='"+StringUtils.upperCase(user)+"'"); #username需要用大写的 创建表空间:create tablespace tableSpace_name datafile 'tableSpace_name.dbf' size 100m AUTOEXTEND ON next 100m 创建临时表空间:create temporary tablespace tableSpace_tempName datafile 'tableSpace_tempName .dbf' size 100m AUTOEXTEND ON next 100m 创建用户: //11g创建用户带数字需加上,不带可以不加,create "user" 创建完毕后保存的是大写 //12c创建数字用户不需要加引号 创建完毕也是大写 若加上引号则用户为"user" 登入的时候需要输入"user" pwd Create user userName identified by "pwd" default tablespace tableSpace_name temporary tablespace tableSpace_tempName quota unlimited on tableSpace_name 授权:grant connect,resource to username #connect和resource是俩种角色 connect登入权限 resource就是一些增删改查 创建各种表单了 视图的权限 ,但是同时也会授予unlimited tablespace权限,意思就是可以使用所有的表空 //unlimited tablespace会把所有表空间的权限撤销 需要在执行一次表 空间授权 查看用户的系统权限: select privilege from dba_sys_privs where grantee='USERNAME' 撤销unlimited权限 revoke unlimited tablespace from USERNAME 表空间授权: alter user USERNAME quota unlimited on tableSpace_name |
postgresql:
驱动:org.postgresql.Driver 连接地址:jdbc:postgresql://localhost:5432/testdb 检测数据库是否存在:SELECT u.datname FROM pg_catalog.pg_database u where u.datname='"+db+"' //创建用户 用户或者数据库纯数字需要加上双引号,因这不会有纯数字,暂且不加了 st.execute("create database "+db+" owner "+user); postgresql中有模式的概念:创建的时候默认创建在public模式下,任何一个用户可以在任意的数据库中的public模式下创建表单,但是仅仅该用户可以访问其表单,该数据库的所属用户不能; 例如:用户seliya1 数据库seliya1 用户seliya2 数据库seliya2 seliya1即可以在seliya1的public中创建表单,也可以在seliya2数据库的public模式下创建表单,但是该表单只有seliya1可以访问 postgresql中角色和用户用户组是一个概念即角色既是用户也可以看做用户组 |
KingbaseES:
kingbaseES和postgresql差不多 驱动:com.kingbase.Driver 连接地址:jdbc:kingbase://host:port/database 校验数据库是否存在: String getDB = "SELECT u.datname FROM sys_catalog.sys_database u where u.datname='"+db+"'"; //创建用户 用户或者数据库纯数字需要加上双引号,因这不会有纯数字,暂且不加了 create database "+db+" owner "+user kingbase中也有模式的概念和es差不错自己测试下吧 |
上面数据库创建的代码:
package cn.com.taiji.management.service.impl; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Locale; import java.util.ResourceBundle; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.sun.xfile.XFile; import cn.com.taiji.commons.utils.DicTools; import cn.com.taiji.commons.utils.JdbcTools; import cn.com.taiji.management.dao.AdminResourceDao; import cn.com.taiji.management.entity.AdminResource; import cn.com.taiji.management.service.DbRsManageService; import net.sf.json.JSONObject; @Service(value ="DbRsManageService") public class DbRsManageServiceImpl implements DbRsManageService{ @Autowired private AdminResourceDao adminResourceDao; private static final Logger log = LoggerFactory.getLogger(DbRsManageServiceImpl.class); private static ResourceBundle rb = ResourceBundle.getBundle("application", Locale.getDefault()); // private static Connection conn = null; // private static Statement st = null; // private static ResultSet rs = null; @Value("${oracle.tablespace.size}") private String tablespaceSize; @Value("${oracle.tablespace.next.size}") private String tablespaceNextSize; @Override public String createMysqlDB(String db,String adminResourceId,String user,String pwd) throws Exception { Connection conn = null; Statement st = null; ResultSet rs = null; String result = "FAIL"; Boolean flag = false; try { //获取dba用户 AdminResource admin = adminResourceDao.findOne(adminResourceId); //注册驱动 JdbcTools.registerDriver(rb.getString("mysql.driver")); //获取连接 url jdbc:mysql://10.0.26.14:3306/zyzx?characterEncoding=utf-8 String url = "jdbc:mysql://"+admin.getIp()+":"+admin.getPort()+"/"+admin.getInstance()+"?characterEncoding=utf-8"; conn = JdbcTools.getConnection(url,admin.getUserName(),admin.getPwd()); st = conn.createStatement(); //校验数据库是否存在 String getDB = "SELECT information_schema.SCHEMATA.SCHEMA_NAME FROM information_schema.SCHEMATA where SCHEMA_NAME='"+db+"'"; rs =st.executeQuery(getDB); if(rs.next()) { flag = true; throw new Exception("数据库已经存在!"); }else { //创建数据库 st.execute("create database "+db); //创建用户 String createUserSql = "CREATE USER '"+user+"'@'%' IDENTIFIED BY '"+pwd+"'"; st.execute(createUserSql); //授权 String grantSql = "grant all privileges on "+db+".* to '"+user+"'@'%'"; st.execute(grantSql); result = "SUCCESS"; } } catch (Exception e) { log.error("创建数据库或授权失败"+e); if(flag) { throw new Exception("数据库已经存在!"); }else { throw new Exception("数据库创建失败!"); } }finally { JdbcTools.release(conn, st, rs); } return result; } @SuppressWarnings("resource") @Override public String createOracleDB(String adminResourceId,String user,String pwd) throws Exception { Connection conn = null; Statement st = null; ResultSet rs = null; String result = "FAIL"; Boolean flag = false; try { //获取dba用户 AdminResource admin = adminResourceDao.findOne(adminResourceId); //注册驱动 JdbcTools.registerDriver(rb.getString("oracle.driver")); //获取连接 jdbc:oracle:thin:@localhost:1521/orcl String url = "jdbc:oracle:thin:@"+admin.getIp()+":"+admin.getPort()+"/"+admin.getInstance(); conn = JdbcTools.getConnection(url,admin.getUserName(),admin.getPwd()); //创建报表 st = conn.createStatement(); //判断用户是否存在 //11g创建用户带数字需加上,不带可以不加,create "user" 创建完毕后保存的是大写 //12c创建数字用户不需要加引号 创建完毕也是大写 若加上引号则用户为"user" 登入的时候需要输入"user" pwd rs = st.executeQuery("select username from SYS.dba_users where username='"+StringUtils.upperCase(user)+"'"); if(rs.next()) { flag = true; throw new Exception("用户已经存在!"); }else { //创建表空间 表空间以user名称命名 String tableSpace = "space"+user; st.execute("create tablespace "+tableSpace+" datafile '"+tableSpace+".dbf' size "+tablespaceSize+" AUTOEXTEND ON next "+tablespaceNextSize); st.execute("create temporary tablespace "+tableSpace+"temp tempfile '"+tableSpace+"temp.dbf' size "+tablespaceSize+" AUTOEXTEND ON next "+tablespaceNextSize); //创建用户 log.info("Create user \""+user+"\" identified by \""+pwd+"\" default tablespace "+tableSpace+" temporary tablespace "+tableSpace+"temp quota unlimited on "+tableSpace); st.execute("Create user "+user+" identified by \""+pwd+"\" default " + "tablespace "+tableSpace+" temporary tablespace "+tableSpace+"temp quota unlimited on "+tableSpace); //用户授权 st.execute("grant connect,resource to "+user); //unlimited tablespace会把所有表空间的权限撤销 需要在执行一次表 空间授权 //11g需要撤销 12c不需要撤销 查询其权限的时候用单引号 String tmpUser ="'"+StringUtils.upperCase(user)+"'"; //撤销授权 rs = st.executeQuery("select privilege from dba_sys_privs where grantee="+tmpUser); if(rs.next()) { st.execute("revoke unlimited tablespace from "+user); } //修改表空间授权 st.execute(" alter user "+user+" quota unlimited on "+tableSpace); result = "SUCCESS"; } } catch (Exception e) { log.error("创建数据库或授权失败"+e); if(flag) { throw new Exception("用户已经存在!"); }else { throw new Exception("oracle创建资源失败"); } }finally { JdbcTools.release(conn, st, rs); } return result; } @Override public JSONObject checkResource(String adminResourceId, String name) throws Exception { String res = "{'isfalg':'1'}"; AdminResource admin = adminResourceDao.findOne(adminResourceId); String rs_type = admin.getTypeIsType(); Boolean flag = true; if(DicTools.RS_STYLE_DATABASE.equals(rs_type)) { flag = checkDBResource(admin, name); }else if(DicTools.RS_STYLE_FILE.equals(rs_type)) { flag = checkFileResource(admin, name); } if(!flag) { res = "{'isfalg':'0'}"; } return JSONObject.fromObject(res); } private Boolean checkFileResource(AdminResource admin, String name) throws Exception { Boolean flag = true;//存在 String softType = admin.getType().trim(); if(DicTools.SOFT_STYLE_NFS.equals(softType)) { //检测目录是否存在 String url = "nfs://"+admin.getIp()+admin.getInstance()+"/"+name; XFile xfile = new XFile(url); if (!xfile.exists()) { flag = false; } } return flag; } private Boolean checkDBResource(AdminResource admin, String name) throws Exception { Connection conn = null; Statement st = null; ResultSet rs = null; Boolean flag = true;//存在 try { String softType = admin.getType().trim(); String driverName = rb.getString(softType+".driver"); String url = ""; String sql = ""; if(DicTools.SOFT_STYLE_MYSQL.equals(softType)) { url = "jdbc:mysql://"+admin.getIp()+":"+admin.getPort()+"/"+admin.getInstance()+"?characterEncoding=utf-8"; sql = "SELECT information_schema.SCHEMATA.SCHEMA_NAME FROM information_schema.SCHEMATA where SCHEMA_NAME='"+name+"'"; }else if(DicTools.SOFT_STYLE_ORACLE.equals(softType)){ url = "jdbc:oracle:thin:@"+admin.getIp()+":"+admin.getPort()+"/"+admin.getInstance(); sql = "select username from dba_users where username='"+StringUtils.upperCase(name)+"'"; }else if(DicTools.SOFT_STYLE_POSTGRESQL.equals(softType)) { url = "jdbc:postgresql://"+admin.getIp()+":"+admin.getPort()+"/"+admin.getInstance(); sql = "SELECT u.datname FROM pg_catalog.pg_database u where u.datname='"+name+"'"; }else if(DicTools.SOFT_STYLE_KINGBASE.equals(softType)) { url = "jdbc:kingbase://"+admin.getIp()+":"+admin.getPort()+"/"+admin.getInstance(); sql = "SELECT u.datname FROM sys_catalog.sys_database u where u.datname='"+name+"'"; } //注册驱动 JdbcTools.registerDriver(driverName); //获取连接 conn = JdbcTools.getConnection(url,admin.getUserName(),admin.getPwd()); //创建包边 st = conn.createStatement(); //执行查询 rs = st.executeQuery(sql); if(!rs.next()) { flag = false; } } catch (SQLException e) { log.error("数据库校验资源是否存在报错"+e); throw new Exception("数据库校验资源是否存在报错"); }finally { JdbcTools.release(conn, st, rs); } return flag; } @Override public String createNFSPath(String adminResourceId,String path) throws Exception { String rel = "FAIL"; AdminResource admin = adminResourceDao.findOne(adminResourceId); String url = "nfs://"+admin.getIp()+admin.getInstance()+"/"+path; XFile xfile = new XFile(url); if(xfile.exists()) { throw new Exception("路径已存在!"); }else { if(xfile.mkdir()) { rel = "SUCCESS"; } } return rel; } @Override public String createPostgreSQLDB(String db,String adminResourceId,String user,String pwd) throws Exception { Connection conn = null; Statement st = null; ResultSet rs = null; String result = "FAIL"; try { //获取dba用户 AdminResource admin = adminResourceDao.findOne(adminResourceId); //注册驱动 JdbcTools.registerDriver(rb.getString("postgresql.driver")); //获取连接 url jdbc:postgresql://localhost:5432/testdb String url = "jdbc:postgresql://"+admin.getIp()+":"+admin.getPort()+"/"+admin.getInstance(); conn = JdbcTools.getConnection(url,admin.getUserName(),admin.getPwd()); st = conn.createStatement(); //校验数据库是否存在 String getDB = "SELECT u.datname FROM pg_catalog.pg_database u where u.datname='"+db+"'"; rs =st.executeQuery(getDB); if(rs.next()) { throw new Exception("数据库已经存在!"); }else { //创建用户 用户或者数据库纯数字需要加上双引号,因这不会有纯数字,暂且不加了 String createUserSql = "CREATE USER "+user+" WITH PASSWORD '"+pwd+"'"; st.execute(createUserSql); //创建数据库并授权 st.execute("create database "+db+" owner "+user); result = "SUCCESS"; } } catch (Exception e) { log.error("创建数据库或授权失败"+e); throw new Exception("数据库创建失败!"); }finally { JdbcTools.release(conn, st, rs); } return result; } @Override public String createKingbaseESDB(String db,String adminResourceId,String user,String pwd) throws Exception { Connection conn = null; Statement st = null; ResultSet rs = null; String result = "FAIL"; try { //获取dba用户 AdminResource admin = adminResourceDao.findOne(adminResourceId); //注册驱动 JdbcTools.registerDriver(rb.getString("kingbase.driver")); //获取连接 url jdbc:kingbase://host:port/database String url = "jdbc:kingbase://"+admin.getIp()+":"+admin.getPort()+"/"+admin.getInstance(); conn = JdbcTools.getConnection(url,admin.getUserName(),admin.getPwd()); st = conn.createStatement(); //校验数据库是否存在 String getDB = "SELECT u.datname FROM sys_catalog.sys_database u where u.datname='"+db+"'"; rs =st.executeQuery(getDB); if(rs.next()) { throw new Exception("数据库已经存在!"); }else { //创建用户 用户或者数据库纯数字需要加上双引号,因这不会有纯数字,暂且不加了 String createUserSql = "CREATE USER "+user+" WITH PASSWORD '"+pwd+"'"; st.execute(createUserSql); //创建数据库并授权 st.execute("create database "+db+" owner "+user); result = "SUCCESS"; } } catch (Exception e) { log.error("创建数据库或授权失败"+e); throw new Exception("数据库创建失败!"); }finally { JdbcTools.release(conn, st, rs); } return result; } } |
jdbc工具类:
package cn.com.taiji.commons.utils; import java.sql.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class JdbcTools { // 注册驱动 public static void registerDriver(String driver) throws Exception { try { Class.forName(driver); } catch (ClassNotFoundException e) { throw new ExceptionInInitializerError(e); } } // 获得连接 public static Connection getConnection(String url, String userName, String passWord) throws Exception { return DriverManager.getConnection(url, userName, passWord); } // 关闭资源 public static void release(Connection conn, Statement statement, ResultSet resultSet) { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } finally { conn = null; } } if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } finally { statement = null; } } if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } finally { resultSet = null; } } } /** * 查询SQL * @param sql * @return 数据集合 * @throws Exception */ public static List |