Guest User

Untitled

a guest
Nov 27th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. DBHelp.java:
  2.  
  3. public class DBHelp<T> {
  4.  
  5. private static BasicDataSource ds;
  6.  
  7. static{
  8.  
  9. InputStream stream = DBHelp.class.getClassLoader().getResourceAsStream("db.properties");
  10.  
  11. Properties p = new Properties();
  12. try {
  13. p.load(stream);
  14.  
  15. ds = new BasicDataSource();
  16. ds.setUrl(p.getProperty("url"));
  17. ds.setPassword(p.getProperty("pwd"));
  18. ds.setDriverClassName(p.getProperty("driver"));
  19. ds.setUsername(p.getProperty("name"));
  20. ds.setInitialSize(5);
  21. ds.setMaxActive(20);
  22. ds.setMaxWait(5000);
  23. ds.setMinIdle(8);
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27.  
  28. }
  29.  
  30. /**
  31. * 获取数据库连接对象
  32. * @return Connection类的对象
  33. */
  34. public Connection getConnection() {
  35. Connection conn =null;
  36. try {
  37. conn = ds.getConnection();
  38. } catch (SQLException e) {
  39. e.printStackTrace();
  40. }
  41. return conn;
  42. }
  43.  
  44.  
  45. db.properties:
  46.  
  47. driver=com.mysql.jdbc.Driver
  48. url=jdbc:mysql:///mydb
  49. name=root
  50. pwd=root
Add Comment
Please, Sign In to add comment