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

     

  • 相关阅读:
    bzoj 1208: [HNOI2004]宠物收养所 (Treap)
    Bzoj 2431: [HAOI2009]逆序对数列 (DP)
    Bzoj 1055: [HAOI2008]玩具取名 (区间DP)
    线段树入门详解
    Bzoj 1087: [SCOI2005]互不侵犯King
    Bzoj 2748: [HAOI2012]音量调节 (DP)
    Bzoj 2752 高速公路 (期望,线段树)
    惨淡的模拟赛
    GSS4
    Bzoj 近期题目一句话题解
  • 原文地址:https://www.cnblogs.com/toughhou/p/3778782.html
Copyright © 2011-2022 走看看