import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Date;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.json.JSONObject;
public class JdbcTest {
/**定义一个Connection 用来连接数据库*/
private Connection conn=null;
/**连接数据库的URL*/
private final String url="";
/**指定数据的用户名和密码*/
private final String username="";
private final String password="";
/**定义一个int记录更新的记录数量*/
int count=0;
/**定义一个结果集 用于放回查询结果*/
private ResultSet resultSet=null;
private PreparedStatement pstmt=null;
public JdbcTest(){
conn = connectionDB();
}
/**
* 建立数据的连接
* @exception SQLException, ClassNotFoundException
*/
@SuppressWarnings("finally")
public Connection connectionDB(){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection(url,username,password);
System.out.println("连接数据库成功");
}catch(Exception e){
e.printStackTrace();
System.out.println("建立数据库发生错误!");
}finally{
return conn;
}
}
/**
* 查询方法
* @param sql查询sql语句
* @return resultSet
*/
@SuppressWarnings("finally")
public ResultSet query(String sql){
try {
pstmt = conn.prepareStatement(sql);
/**查询*/
resultSet = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}finally{
return resultSet;
}
}
/* *//**
* 更新数据
* @param sql 更新sql语句
* @return
*//*
public int update(String sql){
try {
pstmt = conn.prepareStatement(sql);
count=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("执行更新出错了");
}
return count;
}
*/ /**关闭连接*/
public boolean coles(){
boolean isColes = false;
if(resultSet!=null){
try {
resultSet.close();
resultSet=null;
isColes=true;
} catch (SQLException e) {
isColes=false;
e.printStackTrace();
System.out.println("关闭结果集发生错误");
}
}
if(pstmt!=null){
try {
pstmt.close();
pstmt=null;
isColes=true;
} catch (SQLException e) {
isColes=false;
e.printStackTrace();
System.out.println("关闭pstmt发生异常");
}
}
if(conn!=null){
try{
conn.close();
conn=null;
isColes=true;
}catch (Exception e) {
isColes=false;
e.printStackTrace();
System.out.println("关闭conn发生异常");
}
}
return isColes;
}
/**
* 测试查询的方法
* @throws SQLException
*/
public String testQuery() throws SQLException{
resultSet =query("select id from t_Tsk_DayRpt");
// json数组
StringBuffer querybBuffer=new StringBuffer("[");
// 获取列数
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
JSONObject jsonObj=new JSONObject();
String columnName=null;
String value=null;
System.out.println("开始遍历数据……请耐心等候!");
// 遍历ResultSet中的每条数据
while (resultSet.next()) {
// 遍历每一列
for (int i = 1; i <= columnCount; i++) {
columnName =metaData.getColumnLabel(i);
value = resultSet.getString(columnName);
jsonObj.put(columnName, value);
}
querybBuffer.append(jsonObj.toString());
querybBuffer.append(",");
}
querybBuffer.delete(querybBuffer.length()-1, querybBuffer.length());
querybBuffer.append("]");
return querybBuffer.toString();
}
/**
*
* @param args
* @throws SQLException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws SQLException, ClassNotFoundException {
JdbcTest db = new JdbcTest();
System.out.println("查询开始时间--->"+DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
/**调用查询方法*/
String query = db.testQuery();
try {
FileOutputStream fos = new FileOutputStream("e:\\changgo1.txt");
fos.write(query.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("查询结束时间--->"+DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
/**调用更新方法*/
// db.testUpdate();
/**调用关闭连接方法*/
db.coles();
}
}
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Date;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.json.JSONObject;
public class JdbcTest {
/**定义一个Connection 用来连接数据库*/
private Connection conn=null;
/**连接数据库的URL*/
private final String url="";
/**指定数据的用户名和密码*/
private final String username="";
private final String password="";
/**定义一个int记录更新的记录数量*/
int count=0;
/**定义一个结果集 用于放回查询结果*/
private ResultSet resultSet=null;
private PreparedStatement pstmt=null;
public JdbcTest(){
conn = connectionDB();
}
/**
* 建立数据的连接
* @exception SQLException, ClassNotFoundException
*/
@SuppressWarnings("finally")
public Connection connectionDB(){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection(url,username,password);
System.out.println("连接数据库成功");
}catch(Exception e){
e.printStackTrace();
System.out.println("建立数据库发生错误!");
}finally{
return conn;
}
}
/**
* 查询方法
* @param sql查询sql语句
* @return resultSet
*/
@SuppressWarnings("finally")
public ResultSet query(String sql){
try {
pstmt = conn.prepareStatement(sql);
/**查询*/
resultSet = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}finally{
return resultSet;
}
}
/* *//**
* 更新数据
* @param sql 更新sql语句
* @return
*//*
public int update(String sql){
try {
pstmt = conn.prepareStatement(sql);
count=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("执行更新出错了");
}
return count;
}
*/ /**关闭连接*/
public boolean coles(){
boolean isColes = false;
if(resultSet!=null){
try {
resultSet.close();
resultSet=null;
isColes=true;
} catch (SQLException e) {
isColes=false;
e.printStackTrace();
System.out.println("关闭结果集发生错误");
}
}
if(pstmt!=null){
try {
pstmt.close();
pstmt=null;
isColes=true;
} catch (SQLException e) {
isColes=false;
e.printStackTrace();
System.out.println("关闭pstmt发生异常");
}
}
if(conn!=null){
try{
conn.close();
conn=null;
isColes=true;
}catch (Exception e) {
isColes=false;
e.printStackTrace();
System.out.println("关闭conn发生异常");
}
}
return isColes;
}
/**
* 测试查询的方法
* @throws SQLException
*/
public String testQuery() throws SQLException{
resultSet =query("select id from t_Tsk_DayRpt");
// json数组
StringBuffer querybBuffer=new StringBuffer("[");
// 获取列数
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
JSONObject jsonObj=new JSONObject();
String columnName=null;
String value=null;
System.out.println("开始遍历数据……请耐心等候!");
// 遍历ResultSet中的每条数据
while (resultSet.next()) {
// 遍历每一列
for (int i = 1; i <= columnCount; i++) {
columnName =metaData.getColumnLabel(i);
value = resultSet.getString(columnName);
jsonObj.put(columnName, value);
}
querybBuffer.append(jsonObj.toString());
querybBuffer.append(",");
}
querybBuffer.delete(querybBuffer.length()-1, querybBuffer.length());
querybBuffer.append("]");
return querybBuffer.toString();
}
/**
*
* @param args
* @throws SQLException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws SQLException, ClassNotFoundException {
JdbcTest db = new JdbcTest();
System.out.println("查询开始时间--->"+DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
/**调用查询方法*/
String query = db.testQuery();
try {
FileOutputStream fos = new FileOutputStream("e:\\changgo1.txt");
fos.write(query.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("查询结束时间--->"+DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
/**调用更新方法*/
// db.testUpdate();
/**调用关闭连接方法*/
db.coles();
}
}