Guest User

Untitled

a guest
Nov 26th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. /*database.properties*/
  2. jdbc.drivers=com.mysql.jdbc.Driver
  3. jdbc.url=jdbc:mysql://127.0.0.1:3306/corejava
  4. jdbc.username=root
  5. jdbc.password=159753
  6.  
  7. /*mysql连接代码*/
  8. public static Connection getConnection() throws SQLException, IOException
  9. {
  10. Properties props = new Properties();
  11. FileInputStream in = new FileInputStream("database.properties");
  12. props.load(in);
  13. in.close();
  14.  
  15. String drivers = props.getProperty("jdbc.drivers");
  16. if (drivers != null) System.setProperty("jdbc.drivers", drivers);
  17. String url = props.getProperty("jdbc.url");
  18. String username = props.getProperty("jdbc.username");
  19. String password = props.getProperty("jdbc.password");
  20.  
  21. return DriverManager.getConnection(url, username, password);
  22. }
Add Comment
Please, Sign In to add comment