星空网 > 软件开发 > 数据库

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

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




原标题:使用JDBC向数据库中插入一条数据(第一次修改版)

关键词:数据库

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

紧急通知:肯尼亚突然施行新规,已执行再通知让外贸货代企业措手不及!:https://www.ikjzd.com/articles/12705
亚马逊运营QA集锦(994-1003):https://www.ikjzd.com/articles/127050
如何快速申请亚马逊白名单,告别无品牌的尴尬?:https://www.ikjzd.com/articles/127052
活久见!大批美国二审账号自动复活:https://www.ikjzd.com/articles/127053
一个亚马逊老韭菜眼里的中东和印度站点!:https://www.ikjzd.com/articles/12706
亚马逊上线自动广告否定ASIN功能 允许非品牌卖家上传视频:https://www.ikjzd.com/articles/127064
瑞典VAT税号的有效期是多久?:https://www.kjdsnews.com/a/1842116.html
瑞典VAT税号的有效期是多久?:https://www.xlkjsw.com/news/91234.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流