连接数据库查询数据

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");
}
}
posted on 2015-09-27 17:49  XZhe  阅读(307)  评论(0编辑  收藏  举报