• Java创建Oracle数据库表


    我们通常只用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

     

  • 相关阅读:
    微信小程序分享及信息追踪
    vue刷新路由,不刷新页面
    vue中是使用富文本编辑器vue-quill-edit
    下载配置nodeJs,cnpm,webpack,vue-cli等,刚装的系统,所有东西重新配置
    promise学习总结
    【转】前端的BFC、IFC、GFC和FFC
    ES6中export与export default的区别
    前端常见跨域解决方案
    vue2.0s中eventBus实现兄弟组件通信
    RHEL5 yum更新源
  • 原文地址:https://www.cnblogs.com/toughhou/p/3778782.html
Copyright © 2020-2023  润新知