zoukankan      html  css  js  c++  java
  • 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

     

  • 相关阅读:
    怎样在caffe中添加layer以及caffe中triplet loss layer的实现
    【java】为数组全部元素赋同样的值 以及 数组之间的复制
    POJ 2386 Lake Counting 搜索题解
    android中实现内容搜索
    JNI之——在cmd命令行下编译执行C/C++源文件
    怎样编写高效android代码
    http headers
    matlab 工具函数、matlab toolbox(工具箱)
    数列收敛与数列极限
    数列收敛与数列极限
  • 原文地址:https://www.cnblogs.com/toughhou/p/3778782.html
Copyright © 2011-2022 走看看