zoukankan      html  css  js  c++  java
  • 在静态方法中不能调用非静态变量,但getclass()可以换个形式来调用

    数据库连接的工具类

    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.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,Statement statement){
    if(resultSet!=null){
    try {
    resultSet.close();
    System.out.println("ok1");
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    if(statement!=null){
    try {
    statement.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();
    }
    }
    }

  • 相关阅读:
    python 监听文件夹下的文件,将文本内容写入kafka,支持断电续传 (docker 发布)
    python监控目录和文件变化
    Python 单例
    Python 面向对象 继承
    Python 面向对象 多态
    Python 面向对象 类属性和类方法
    Python 基础2
    Python 面向对象
    【bird-front】全自动数据表格组件bird-grid
    【bird-java】bird-java系列文章汇总
  • 原文地址:https://www.cnblogs.com/afterhours/p/6097852.html
Copyright © 2011-2022 走看看