java创建数据库

来源:互联网 发布:淘宝上哪家韩妆是正品 编辑:程序博客网 时间:2024/06/08 12:00

在这之前一直都是先在Oracle、MySql等数据库中将数据库、表等操作创建好,再开始编写代码,最近看到一个项目则是通过java代码来执行sql语句来创建数据库这一系列的操作,感觉还是蛮新鲜的,起码对我来说是这样的,接下来将部分的关键代码记录下来,方便后面忘了再回来看看

这里面我用到了c3p0包中的ComboPooledDataSource类和spring中的JdbcTemplate类,在之前对这两个类不了解,这里也借这个机会学习学习这两个类

如果要使用这两个类必须导入c3p0-x.x.xjar好spring.jar包

使用ComboPooledDataSource获取数据源

public DataSource createDataSource(String driver, String url,String userName, String password) {try {//创建ComboPooledDataSourceComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource();//设置相应参数comboPooledDataSource.setDriverClass(driver);comboPooledDataSource.setJdbcUrl(url);comboPooledDataSource.setUser(userName);comboPooledDataSource.setPassword(password);//设置最小连接个数comboPooledDataSource.setMinPoolSize(5);//设置最大连接个数comboPooledDataSource.setMaxPoolSize(50);//设置最大空闲时间comboPooledDataSource.setMaxIdleTime(5000);//返回数据源对象return comboPooledDataSource;} catch (PropertyVetoException e) {e.printStackTrace();}return null;}


 

 

测试MySql连接代码

/** * 测试Mysql连接 * @return */private boolean testMySqlConnection(){//数据库驱动String driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver";//数据库连接String conUrl="jdbc:mysql://"+this.url+"/test";try {//获取数据源对象DataSource newDataSource=dataScourceCreate.createDataSource(driverClass, conUrl, userName, password);//为jdbcTemplate设置数据源this.jdbcTemplate.setDataSource(newDataSource);//sql语句--创建数据库String sql="CREATE DATABASE IF NOT EXISTS "+this.name+" DEFAULT CHARACTER SET UTF8";//执行sql语句this.jdbcTemplate.execute(sql);//获取数据源--测试刚创建的数据库newDataSource=dataScourceCreate.createDataSource(driverClass, "jdbc:mysql://"+this.url+"/"+this.name+"", userName, password);this.jdbcTemplate.setDataSource(newDataSource);this.jdbcTemplate.execute("use "+this.name);this.jdbcTemplate.execute("create table if not exists student(id int not null)");this.jdbcTemplate.execute("drop table student");return true;} catch (DataAccessException e) {e.printStackTrace();return false;}}


测试SqlServer连接

/** * 测试SqlServer数据库连接 * @return */private boolean testSqlServerConnection(){//数据库驱动String driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver";//数据库连接String conUrl="jdbc:sqlserver://"+this.url+";databasename=master";try {//获取数据源对象DataSource newDataSource=dataScourceCreate.createDataSource(driverClass, conUrl, userName, password);//设置数据源this.jdbcTemplate.setDataSource(newDataSource);//创建是否存在数据库sql语句String sql="if exists(select 1 from sysdatabases where name='"+this.name+"') drop database "+this.name;//执行sql语句this.jdbcTemplate.execute(sql);//获取创建数据库sql语句并执行this.jdbcTemplate.execute(sqlUtil.createDataBaseSql(this.name));//从新获取数据源--测试刚创建的数据库newDataSource=dataScourceCreate.createDataSource(driverClass, "jdbc:sqlserver://"+this.url+";databasename="+this.name, userName, password);this.jdbcTemplate.setDataSource(newDataSource);this.jdbcTemplate.execute("use "+this.name);//执行判断表是否存在this.jdbcTemplate.execute("if exists(select 1 from sysobjects where name='test') drop table test");//创建表this.jdbcTemplate.execute("create table test(id int not null)");//删除表this.jdbcTemplate.execute("drop table test");return true;} catch (Exception e) {e.printStackTrace();return false;}}


测试Oracle连接

/** * 测试Oracle连接 * @return */private boolean testOracleConnection(){//数据库驱动String driverClass="oracle.jdbc.driver.OracleDriver";//数据库连接String conUrl="jdbc:oracle:thin:@"+this.url+":orcl";try {//获取数据源对象DataSource newDataSource=dataScourceCreate.createDataSource(driverClass, conUrl, "system", password);//设置数据源this.jdbcTemplate.setDataSource(newDataSource);//查询需要创建的用户是否存在int result=jdbcTemplate.queryForInt("select count(*) from all_users where USERNAME='"+this.userName.toUpperCase()+"'");if(result>0){Map map=jdbcTemplate.queryForMap("select sid,serial# from v$session where upper(username)=upper('"+this.userName.toUpperCase()+"')");//执行关闭jdbcTemplate.execute("alter system kill session '"+map.get("SID")+","+map.get("SERIAL#")+"'");//删除用户jdbcTemplate.execute("DROP USER "+this.userName.toUpperCase()+" CASCADE");}//创建用户this.jdbcTemplate.execute("CREATE USER "+this.userName+" IDENTIFIED BY "+this.password);//给新建用户绑定权限this.jdbcTemplate.execute("GRANT CONNECT,RESOURCE TO "+this.userName);//连接新创建的用户newDataSource=dataScourceCreate.createDataSource(driverClass, conUrl, this.userName, this.password);//设置新用户的数据源this.jdbcTemplate.setDataSource(newDataSource);//查询该用户是否存在表result=jdbcTemplate.queryForInt("SELECT count(*) FROM USER_TAB_COMMENTS WHERE TABLE_NAME='TEST'");if(result>0){//删除表this.jdbcTemplate.execute("DROP TABLE TEST");}//创建表this.jdbcTemplate.execute("CREATE TABLE TEST(ID NUMBER NOT NULL)");//删除表this.jdbcTemplate.execute("DROP TABLE TEST");return true;} catch (Exception e) {e.printStackTrace();return false;}}


关键代码都在这了,目前我只了解这3个数据库,所以也只有这3个了,代码没做优化,如果你们觉得有用,优化就自己弄吧

 

原创粉丝点击