Sxt: JDBC-jdbc programming procedure

Source: Internet
Author: User

JDBC programming steps:
1. Load the driver to load the driver (register the driver)

(1) class. forname ("com. MySQL. JDBC. Driver"); | class. forname (). newinstance () | new drivername ();
// New drivername () is connected by new COM. MySQL. JDBC. Driver ();
(2) It automatically registers with drivermanager during instantiation and does not need to explicitly call the drivermanager. registerdriver method.

2. Connect to database

(1) drivermanager. getconnection ()

3. Execute the SQL

(1) connection. createstatement ()
(2) statement.exe cutequery ()
(3) statement.exe cuteupdate ()

4. retireve the result data

(1) loop to get the result while (Rs. Next ())

5. Show the result data

(1) convert various data types in data to the getxxx method in Java

6. Close

1. Close the resultset/Close the statement/Close the connection

 

Demo

  1. /**
  2. * The code is an example of getting started. It is mainly used to familiarize yourself with the basic process of JDBC programming. Data is not displayed on the page, and exceptions are not considered in detail.
  3. */
  4. Import java. SQL .*;
  5. Public class testjdbc {
  6. /**
  7. * @ Param ARGs
  8. */
  9. Public static void main (string [] ARGs) throws exception
  10. {
  11. // 1. Load the driver to load the driver (register the driver)
  12. New COM. MySQL. JDBC. Driver ();
  13. // Or use this connection method // class. forname ("com. MySQL. JDBC. Driver ");
  14. // 2. Connect to database to connect to the database. Take mysql5.0 as an example. The database name is college, the user name is root, and the password is 123456.
  15. Connection conn = drivermanager. getconnection ("JDBC: mysql: // localhost: 3306/College", "root", "123456 ");
  16. // 3. Execute the SQL
  17. Statement stmt = conn. createstatement ();
  18. Resultset rs = stmt.exe cutequery ("select * from user ");
  19. While (Rs. Next () // 4. retireve the result data traverses the result set
  20. {
  21. // 5. Show the result data to display the result. convert various data types in the data to the getxxx method in Java.
  22. System. Out. println ("the first Colum :");
  23. System. Out. println (Rs. getstring ("username "));
  24. System. Out. println ("the second Colum :");
  25. System. Out. println (Rs. getstring ("userflag "));
  26. }
  27. // 6. Close the connection and close it in reverse order from the start.
  28. Rs. Close ();
  29. Stmt. Close ();
  30. Conn. Close ();
  31. }
  32. }

 

Make the demo a little better, as shown below:

  1. Import java. SQL .*;
  2. Public class testjdbc
  3. {
  4. /**
  5. * @ Param ARGs
  6. */
  7. Public static void main (string [] ARGs)
  8. {
  9. Connection conn = NULL;
  10. Statement stmt = NULL;
  11. Resultset rs = NULL;
  12. Try
  13. {
  14. // 1. Load the driver to load the driver (register the driver)
  15. Class. forname ("com. MySQL. JDBC. Driver ");
  16. // Or use this connection method // new COM. MySQL. JDBC. Driver ();
  17. // 2. Connect to database
  18. // Connect to the database. Take mysql5.0 as an example. The database name is college, the username is root, and the password is 123456.
  19. Conn = drivermanager. getconnection (
  20. "JDBC: mysql: // localhost: 3306/College", "root", "123456 ");
  21. // 3. Execute the SQL
  22. Stmt = conn. createstatement ();
  23. Rs = stmt.exe cutequery ("select * from user ");
  24. While (Rs. Next () // 4. retireve the result data traverses the result set
  25. {
  26. // 5. Show the result data to display the result. convert various data types in the data to the getxxx method in Java.
  27. System. Out. println ("the first Colum :");
  28. System. Out. println (Rs. getstring ("username "));
  29. System. Out. println ("the second Colum :");
  30. System. Out. println (Rs. getstring ("userflag "));
  31. }
  32. } Catch (classnotfoundexception E)
  33. {
  34. E. printstacktrace ();
  35. } Catch (sqlexception E)
  36. {
  37. E. printstacktrace ();
  38. } Finally
  39. {
  40. // 6. Close the connection and close it in reverse order from the start.
  41. Try
  42. {
  43. If (RS! = NULL)
  44. {
  45. Rs. Close ();
  46. Rs = NULL;
  47. }
  48. If (stmt! = NULL)
  49. {
  50. Stmt. Close ();
  51. Stmt = NULL;
  52. }
  53. If (Conn! = NULL)
  54. {
  55. Conn. Close ();
  56. Conn = NULL;
  57. }
  58. } Catch (exception E)
  59. {
  60. E. printstacktrace ();
  61. }
  62. }
  63. }
  64. }

 

 

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.