zoukankan      html  css  js  c++  java
  • 小小工具类

    package com.tools;

    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.SQLException;
    import java.util.Properties;

    import com.mysql.jdbc.Connection;
    import com.mysql.jdbc.Driver;
    import com.mysql.jdbc.PreparedStatement;
    import com.mysql.jdbc.ResultSet;
    import com.mysql.jdbc.Statement;

    public class Tools {

    public static Connection getconnetion() throws ClassNotFoundException{

    String dri=null;
    String jdbcurl=null;
    String userString=null;
    String passwordString=null;

    Connection connection=null;
    InputStream inputStream=Tools.class.getClassLoader().getResourceAsStream("jdbc.properties");
    System.out.println(inputStream);
    Properties properties=new Properties();
    try {
    properties.load(inputStream);
    } catch (IOException e) {
    e.printStackTrace();
    }
    dri=properties.getProperty("driver");
    jdbcurl=properties.getProperty("jdbcurl");
    userString=properties.getProperty("user");
    passwordString=properties.getProperty("password");

    Properties info=new Properties();
    info.put("user", userString);
    info.put("password", passwordString);

    try {
    Driver driver=(Driver) Class.forName(dri).newInstance();
    try {
    connection=(Connection) driver.connect(jdbcurl, info);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalAccessException e) {

    e.printStackTrace();
    }

    return connection;

    }
    public static void realese(ResultSet resultSet,Connection connection,PreparedStatement preparedStatement){
    if(resultSet!=null){
    try {
    resultSet.close();
    System.out.println("ok1");
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    if(preparedStatement!=null){
    try {
    preparedStatement.close();
    System.out.println("ok2");
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    if(connection!=null){
    try {
    connection.close();
    System.out.println("ok3");
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    }
    public static void updatele(String sql){
    try {
    Statement statement=(Statement)getconnetion().createStatement();
    statement.executeUpdate(sql);
    } catch (ClassNotFoundException | SQLException e) {

    e.printStackTrace();
    }
    }
    public static void updat(String sql,Object...args) throws Exception{
    Connection connection=null;
    PreparedStatement preparedStatement=null;
    try {
    connection=getconnetion();
    preparedStatement=(PreparedStatement) connection.prepareStatement(sql);
    for(int i=0;i<args.length;i++){
    preparedStatement.setObject(i+1, args[i]);
    }
    preparedStatement.executeUpdate();
    } catch (ClassNotFoundException e) {

    e.printStackTrace();
    }

    }
    }

  • 相关阅读:
    5.19 省选模拟赛 T1 小B的棋盘 双指针 性质
    5.15 省选模拟赛 容斥 生成函数 dp
    5.15 省选模拟赛 T1 点分治 FFT
    5.15 牛客挑战赛40 B 小V的序列 关于随机均摊分析 二进制
    luogu P4929 【模板】舞蹈链 DLX
    CF 878E Numbers on the blackboard 并查集 离线 贪心
    5.10 省选模拟赛 拍卖 博弈 dp
    5.12 省选模拟赛 T2 贪心 dp 搜索 差分
    5.10 省选模拟赛 tree 树形dp 逆元
    luogu P6088 [JSOI2015]字符串树 可持久化trie 线段树合并 树链剖分 trie树
  • 原文地址:https://www.cnblogs.com/afterhours/p/6229164.html
Copyright © 2011-2022 走看看