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);
            }
        }

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

  • 相关阅读:
    linux使用windows中编辑的文件,格式问题
    模拟退火算法c++
    progress第三方框架和二维码第三方框架的选择
    iOS 初始化项目内容
    github上使用SSH和gitignore
    wordpress 如何设置自定义的首页
    wordpress 删除底部"自豪地采用 WordPress"
    masonry注意事项
    iOS修改工程名
    iOS版本更新在APP中直接访问AppStore
  • 原文地址:https://www.cnblogs.com/gode/p/5820145.html
Copyright © 2011-2022 走看看