TestDm.java 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package testDM;
  2. import java.sql.Connection;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5. import javax.sql.DataSource;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;
  8. public class TestDm{
  9. public static void main(String[] args) {
  10. // TODO Auto-generated method stub
  11. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  12. DataSource dataSource = ctx.getBean("dataSource", DataSource.class);
  13. String sql = "select * from sys_user_info";
  14. Connection conn = null;
  15. Statement stm = null;
  16. ResultSet rs = null;
  17. try {
  18. conn = dataSource.getConnection();
  19. stm = conn.createStatement();
  20. rs = stm.executeQuery(sql);
  21. while (rs.next()) {
  22. System.out.println("用户名为:");
  23. System.out.println(rs.getString("LOGIN_NAME"));
  24. }
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. } finally {
  28. try {
  29. rs.close();
  30. stm.close();
  31. conn.close();
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. }
  37. }