zoukankan      html  css  js  c++  java
  • 使用JDBC向数据库中插入一条数据(第一次修改版)

    增加了一个Tools类,放了一些常用的工具

    package com.JDBC.java;
    
    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.Properties;
    
    /**
     * JDBC 的工具类
     */
    public class JDBCTools {
    
        /**
         * 关闭Statement,Connection
         * 
         * @param statement
         * @param connection
         */
        public static void release(Statement statement, Connection connection) {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * 获取数据库连接的方法
         * 
         * @return
         * @throws Exception
         */
        public static Connection getConnection() {
            // 准备连接数据库的四个字符串
            // 驱动的全类名
            String driverClass = null;
            String jdbcUrl = null;
            String user = null;
            String password = null;
            String jdbcName = null;
            // 读取jdbcName.properties文件
            InputStream inStream = JDBCTools.class.getClassLoader().getResourceAsStream("properties/jdbcName.properties");
            Properties propertiesOfName = new Properties();
            try {
                propertiesOfName.load(inStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
            jdbcName = propertiesOfName.getProperty("jdbcName");
            // 读取需要的properties 文件
            InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream("properties/" + jdbcName + ".properties");
            Properties properties = new Properties();
            try {
                properties.load(in);
            } catch (IOException e) {
                e.printStackTrace();
            }
            driverClass = properties.getProperty("driver");
            jdbcUrl = properties.getProperty("jdbcUrl");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            // 加载数据库驱动程序(注册驱动)
            try {
                Class.forName(driverClass);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            Connection connection = null;
            try {
                connection = DriverManager.getConnection(jdbcUrl, user, password);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return connection;
        }
    }

    然后写了一个比较通用的update方法

    /**
         * 通用的更新方法:包括INSERT/UPDATE/DELETE
         * @param sql
         */
        public void update(String sql){
            Connection connection = null;
            Statement statement = null;
            try {
                connection = JDBCTools.getConnection();
                statement = connection.createStatement();
                statement.executeUpdate(sql);
            }catch (Exception e) {
                e.printStackTrace();
            }finally{
                JDBCTools.release(statement, connection);
            }
        }

    懒得写测试类,肯定好使,相信我~

  • 相关阅读:
    [CodeForces]Codeforces Round #429 (Div. 2) ABC(待补)
    About Me
    2018-06-14
    Codeforces Codeforces Round #484 (Div. 2) E. Billiard
    Codeforces Codeforces Round #484 (Div. 2) D. Shark
    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
    Codeforces Avito Code Challenge 2018 D. Bookshelves
    Codeforces Round #485 (Div. 2) D. Fair
    Codeforces Round #485 (Div. 2) F. AND Graph
  • 原文地址:https://www.cnblogs.com/gode/p/5820145.html
Copyright © 2011-2022 走看看