使用JDBC向資料庫中插入一條資料,jdbc資料庫

來源:互聯網
上載者:User

使用JDBC向資料庫中插入一條資料,jdbc資料庫

原諒我是初學者,這個方法寫的很爛,以後不會改進,謝謝

/**     * 通過JDBC向資料庫中插入一條資料 1.Statement 用於執行SQL語句的對象 1.1 通過Connection 的     * createStatement() 方法來擷取 1.2 通過executeUpdate(sql) 的方法來執行SQL 1.3     * 傳入的SQL可以是INSERT/UPDATE/DELETE,但不能是SELECT     *      * 2.Connection和Statement使用後一定要記得關閉 需要在finally裡關閉其對象     * 2.1 關閉的順序:先關閉後擷取的     */    @Test    public void testStatement() {        // 1.擷取資料庫連接        Connection connection = null;        // 4.執行插入        // 4.1 擷取操作SQL語句的Statement對象:        // 調用Connection的createStatement()方法來建立Statement的對象        Statement statement = null;        try {            connection = getConnection();            // 3.準備插入的SQL語句            String sql = "INSERT INTO customers (NAME,EMAIL,BIRTH) "                     +"VALUES ('李小龍','long@live.com','1940-11-27')";            statement = connection.createStatement();            // 4.2 調用Statement對象的executeUpdate(sql) 執行SQL 陳述式的插入            statement.executeUpdate(sql);        } catch (SQLException e) {            e.printStackTrace();        } catch (Exception e) {            e.printStackTrace();        } finally {            // 5.關閉Statement對象            if (statement != null) {                try {                    statement.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }            if (connection != null) {                // 2.關閉串連                try {                    connection.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }        }    }    public Connection getConnection() throws Exception {        // 準備串連資料庫的四個字串        // 驅動的全類名        String driverClass = null;        String jdbcUrl = null;        String user = null;        String password = null;        String jdbcName = null;        // 讀取jdbcName.properties檔案        InputStream inStream = getClass().getClassLoader().getResourceAsStream("properties/jdbcName.properties");        Properties propertiesOfName = new Properties();        propertiesOfName.load(inStream);        jdbcName = propertiesOfName.getProperty("jdbcName");        // 讀取需要的properties 檔案        InputStream in = getClass().getClassLoader().getResourceAsStream("properties/" + jdbcName + ".properties");        Properties properties = new Properties();        properties.load(in);        driverClass = properties.getProperty("driver");        jdbcUrl = properties.getProperty("jdbcUrl");        user = properties.getProperty("user");        password = properties.getProperty("password");        // 載入資料庫驅動程式(註冊驅動)        Class.forName(driverClass);        Connection connection = DriverManager.getConnection(jdbcUrl, user, password);        return connection;    }

資料庫配置如下

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.