Advertisement
Guest User

Untitled

a guest
Jun 12th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. package junbin.demo.tx;
  2.  
  3.  
  4. import org.apache.commons.dbcp2.BasicDataSource;
  5.  
  6. import javax.naming.NamingException;
  7. import javax.sql.DataSource;
  8. import java.sql.Connection;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12.  
  13. /**
  14. * Created by wujunbin on 6/12/17.
  15. */
  16. public class Main {
  17.  
  18. static String driver = "com.mysql.jdbc.Driver";
  19. static String url = "jdbc:mysql://localhost:3306/test";
  20. static String user = "root";
  21. static String password = "123456";
  22.  
  23. public static void main(String[] args) throws ClassNotFoundException, SQLException, NamingException {
  24. // Class.forName(driver);
  25. // Connection conn = DriverManager.getConnection(url, user, password);
  26. // Statement statement = conn.createStatement();
  27. // ResultSet rs = statement.executeQuery("select * from `user`");
  28. // while (rs.next()) {
  29. // System.out.println(rs.getString("id"));
  30. // }
  31. // conn.close();
  32. // Construct BasicDataSource
  33. BasicDataSource bds = new BasicDataSource();
  34. bds.setDriverClassName(driver);
  35. bds.setUrl(url);
  36. bds.setUsername(user);
  37. bds.setPassword(password);
  38. DataSource ds = bds;
  39. Connection conn = ds.getConnection();
  40. Statement statement = conn.createStatement();
  41. ResultSet rs = statement.executeQuery("select * from `user`");
  42. while (rs.next()) {
  43. System.out.println(rs.getString("id"));
  44. }
  45. conn.close();
  46.  
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement