首页 技术 正文
技术 2022年11月14日
0 收藏 916 点赞 2,480 浏览 2196 个字

我们通常只用java执行DML(即:insert, update, delete, select)操作,很少用来执行DDL(create, drop, alert)操作。今天试了下如何用java来创建oracle数据库表。

首先我们来看一下java帮助文档中关于java.sql.Statement类的executeUpdate(String sql)方法描述:
int executeUpdate(String sql)
Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.

完整代码如下:

package utility.mysql;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class DbUtility {

public static void main(String[] args) throws IOException {

String db_url = DbUtility.getDbInfo().get(“db_url”);
        String db_user = DbUtility.getDbInfo().get(“db_user”);
        String db_pwd = DbUtility.getDbInfo().get(“db_pwd”);

Connection conn = DbUtility.getConn(db_url, db_user, db_pwd);

String cmd = “create table test (id number(5), name varchar(10))”;

DbUtility.updateCMD(conn, cmd);
    }
    
    public static void updateCMD(Connection conn, String cmd) {
        try {
            Statement stmt = conn.createStatement();
            stmt.executeUpdate(cmd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

public static Connection getConn(String url, String user, String pwd) {

Connection conn = null;

try {
            Class.forName(“oracle.jdbc.driver.OracleDriver”).newInstance();
            conn = DriverManager.getConnection(url, user, pwd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

public static Map<String, String> getDbInfo() throws IOException {
        Map<String, String> map = new HashMap<String, String>();

Properties p = new Properties();
        InputStream in = Object.class.getResourceAsStream(“/config.properties”);

try {
            p.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }

map.put(“db_user”, p.getProperty(“db_user”));
        map.put(“db_pwd”, p.getProperty(“db_pwd”));
        map.put(“db_url”, p.getProperty(“db_url”));

return map;
    }
}

config.properties是一个配置文件,应该放到项目的bin目录下。代码中的Object.class.getResourceAsStream(“/config.properties”), “/”代码项目的根目录,即bin目录下。
因为好久不用Java了,弄了好一会才把config.properties放到了正确的位置。
config.properties内容如下:
db_user=scott
db_pwd=scott
db_url=jdbc:oracle:thin:@toughhou:1521:ORCL

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291