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

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

  • 相关阅读:
    限制CPU个数
    delphi 不阻塞提示对话框
    快捷打开vnc 某个连接
    delphi ShellExecute 传递多个参数
    JS对JSON的操作总结 (转)
    Windows Server 2008 IIS下部署网站出现的问题
    SqlParameter序列化的问题
    求解,工作流点通过时,弹出窗口让用户录入审核意见
    oracle调度
    c# 多态
  • 原文地址:https://www.cnblogs.com/gode/p/5820145.html
Copyright © 2011-2022 走看看