| package com.lavasoft.dbtest;
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;
import java.sql.Connection; import java.sql.Statement; import java.sql.ResultSet; import java.sql.SQLException;
/** * Created by IntelliJ IDEA. * File: Test.java * User: leizhimin * Date: 2008-2-21 14:41:49 * Spring 数据源应用测试 */ public class Test { private static final Log log = LogFactory.getLog(Test.class);
public static void main(String args[]) { Test.test(); }
public static void test() { String testSql = "select * from t_user"; Connection conn = DBUtil.makeConnection(); Statement stmt = null; try { stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT); ResultSet rs = stmt.executeQuery(testSql); while (rs.next()) { String firstName = rs.getString("firstname"); String lastName = rs.getString("lastname"); System.out.println(firstName + " " + lastName); } } catch (SQLException e) { e.printStackTrace(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { log.info("关闭Statement对象出现异常!"); e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { log.error("关闭数据库连接失败!"); e.printStackTrace(); } } } } } |