Database connection Tool Class--contains access to connect and close resources Connutil.java

Source: Internet
Author: User

  1. Package com.util;
  2. Import java.sql.Connection;
  3. Import Java.sql.DriverManager;
  4. Import java.sql.PreparedStatement;
  5. Import Java.sql.ResultSet;
  6. Import java.sql.SQLException;
  7. /**
  8. * @className: Connutil.java
  9. * @classDescription: Database Connection Tool Class--includes getting connections and shutting down resources
  10. * @function:
  11. * @author: Wentasy
  12. * @createTime: 2012-9-24 11:51:15
  13. * @modifyTime:
  14. * @modifyReason:
  15. * @since: JDK 1.6
  16. */
  17. Public class Connutil {
  18. public static final String url = "Jdbc:mysql://xxx.xxx.xxx.xxx:3306/dbadapter";
  19. public static final String user = "root";
  20. public static final String password = "XXXXXX";
  21. /** 
  22. * Get Connected
  23. * @return
  24. * @throws SQLException
  25. * @throws ClassNotFoundException
  26. */
  27. public static Connection Establishconn () throws sqlexception,classnotfoundexception{
  28. Class.forName ("Com.mysql.jdbc.Driver");
  29. return drivermanager.getconnection (URL, user, password);
  30. }
  31. /** 
  32. * Close Connection
  33. * @param Conn
  34. * @throws SQLException
  35. */
  36. public static void Close (Connection conn) throws sqlexception{
  37. if (conn! = null) {
  38. Conn.close ();
  39. conn = null;
  40. }
  41. }
  42. /** 
  43. * Close PreparedStatement
  44. * @param pstmt
  45. * @throws SQLException
  46. */
  47. public static void Close (PreparedStatement pstmt) throws sqlexception{
  48. if (pstmt! = null) {
  49. Pstmt.close ();
  50. PSTMT = null;
  51. }
  52. }
  53. /** 
  54. * Close result set
  55. * @param RS
  56. * @throws SQLException
  57. */
  58. public static void Close (ResultSet rs) throws sqlexception{
  59. if (rs! = null) {
  60. Rs.close ();
  61. rs = null;
  62. }
  63. }
  64. }

Database connection Tool Class--contains access to connect and close resources Connutil.java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.