Basic steps for Java JDBC to connect to MySQL

Source: Internet
Author: User

Basic steps for Java and MySQL links:

First Step: Register driver

Method One:

[Java]View PlainCopy
    1. Drivermanager.registerdriver (new Com.mysql.jdbc.Driver ());

Method Two: Set properties

[Java]View PlainCopy
    1. System.setproperty ("Jdbc.drivers", "Com.mysql.jdbc.Driver");

Method Three:

[Java]View PlainCopy
    1. Class.forName ("Com.mysql.jdbc.Driver");

Step two: Establish a connection

[Java]View PlainCopy
    1. Connection conn = (Connection) drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test", "root", "  Root ");

Step three: Create a statement

[Java]View PlainCopy
    1. Statement st = (Statement) conn.createstatement ();

Fourth step: Execute the statement

Fifth step: Process the results

Sixth step: Freeing up resources

Test code:

[Java]View PlainCopy
    1. Import Java.sql.DriverManager;
    2. Import Java.sql.ResultSet;
    3. Import java.sql.SQLException;
    4. Import com.mysql.jdbc.Connection;
    5. Import com.mysql.jdbc.Statement;
    6. Public class Demo {
    7. public static void Main (string[] args) {
    8. Demo d = new demo ();
    9. try {
    10. D.test ();
    11. } catch (SQLException e) {
    12. //TODO auto-generated catch block
    13. E.printstacktrace ();
    14. }
    15. }
    16. static void Test () throws SQLException {
    17. //First step: Register driver
    18. Drivermanager.registerdriver (new Com.mysql.jdbc.Driver ());
    19. ///Step two: Establish a connection
    20. Connection conn = (Connection) drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test", "root", "  Root ");
    21. //Step three: Create a statement
    22. Statement st = (Statement) conn.createstatement ();
    23. //Fourth step: Execute the statement and remove the result
    24. ResultSet rs = st.executequery ("select * from MyTable");
    25. //Fifth step: Process results
    26. While (Rs.next ()) {
    27. System.out.println (Rs.getobject (1) + "\ T" + Rs.getobject (2) + "\ T"
    28. + Rs.getobject (3));
    29. }
    30. //Sixth step: Release Resources
    31. Rs.close ();
    32. St.close ();
    33. Conn.close ();
    34. }
    35. }

Basic steps for Java JDBC to connect to MySQL

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.