zoukankan      html  css  js  c++  java
  • 连接数据库查询数据

    package dao;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    public class Test {
    private String className="A";
    private String url="B";
    private String user="C";
    private String password="D";
    private Connection connection;
    private Statement statement;
    private ResultSet resultSet;

    public Connection getConn() {
    try {
    Class.forName(className);
    connection = DriverManager.getConnection(url, user, password);
    } catch (ClassNotFoundException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }
    return connection;
    }

    public ResultSet executeQuery(String sql) {
    connection=getConn();
    try {
    statement = connection.createStatement();
    resultSet = statement.executeQuery(sql);
    } catch (SQLException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }
    return resultSet;
    }

    public static void main(String[] args) {
    Test test = new Test();
    ResultSet set = test.executeQuery("select * from T");
    }
    }
  • 相关阅读:
    集合框架整理及之间的区别
    ArrayList和LinkedList
    GC(Garbage Collection)
    Java常用工具类
    Java异常处理
    JDK环境配置
    内部类总结
    Java字符串定义及常用方法
    Java面向对象总结
    Java数组定义及方法
  • 原文地址:https://www.cnblogs.com/GodZhe/p/4842473.html
Copyright © 2011-2022 走看看