JDBC-Establish a database connection

Source: Internet
Author: User

 

 

 

The username, password, and connection URL of the database can be stored in an independent file when JDBC is used to establish a database connection. For example:

Put the property file in the directory/test. Its content is:

  1. Drivers = com. Microsoft. JDBC. sqlserver. sqlserverdriver
  2. Url = JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = studentmanager
  3. User = sa
  4. Password = wang411dong516

Dbconnection. Java:

 

  1. /**
  2. * Load the database driver through the attribute file to establish a database connection
  3. */
  4. Import java. SQL. sqlexception;
  5. Import java. SQL. connection;
  6. Import java. SQL. drivermanager;
  7. Import java. util. properties;
  8. Import java. Io. fileinputstream;
  9. Import java. Io. ioexception;
  10. Import java. Io. filenotfoundexception;
  11. Public class dbconnection
  12. {
  13. Private string URL; // Database URL
  14. Private string username; // The username used to log on to the database.
  15. Private string password; // User Password
  16. /**
  17. * Return a connection to the database. In a system or class, database operations are often performed.
  18. * The database connection is used as a separate method.
  19. */
  20. Public connection getconnection ()
  21. {
  22. Getproperty ();
  23. Connection con = NULL;
  24. Try
  25. {
  26. Con = drivermanager. getconnection (URL, username, password );
  27. }
  28. Catch (sqlexception E)
  29. {
  30. E. printstacktrace ();
  31. }
  32. Return con;
  33. }
  34. /**
  35. * Read the property configuration file
  36. */
  37. Private void getproperty ()
  38. {
  39. Properties prop = new properties ();
  40. Try
  41. {
  42. Fileinputstream in = new fileinputstream ("driver. properties ");
  43. Prop. Load (in );
  44. String driver = prop. getproperty ("drivers ");
  45. If (driver! = NULL)
  46. System. setproperty ("JDBC. Drivers", driver );
  47. Url = prop. getproperty ("url ");
  48. Username = prop. getproperty ("user ");
  49. Password = prop. getproperty ("password ");
  50. }
  51. Catch (filenotfoundexception E)
  52. {
  53. E. printstacktrace ();
  54. }
  55. Catch (ioexception E)
  56. {
  57. E. printstacktrace ();
  58. }
  59. }
  60. }

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.