Java connects MySQL database with JDBC

Source: Internet
Author: User

First to download connector/j address: http://www.mysql.com/downloads/connector/j/

This is the official MySQL connection method:

After extracting the jar library file, you need to import the library file in the project

I'm using eclipse:







Java is a bit cumbersome to connect to MySQL, so write a class first to open or close the database:

Dbhelper.java

Java code
  1. Package Com.hu.demo;
  2. Import java.sql.Connection;
  3. Import Java.sql.DriverManager;
  4. Import java.sql.PreparedStatement;
  5. Import java.sql.SQLException;
  6. Public class DBHelper {
  7. public static final String url = "jdbc:mysql://127.0.0.1/student";
  8. public static final String name = "Com.mysql.jdbc.Driver";
  9. public static final String user = "root";
  10. public static final String password = "root";
  11. Public Connection conn = null;
  12. Public PreparedStatement PST = null;
  13. Public dbhelper (String sql) {
  14. try {
  15. Class.forName (name); //Specify connection type
  16. conn = drivermanager.getconnection (URL, user, password); //Get Connected
  17. PST = conn.preparestatement (SQL); //Prepare to execute statement
  18. } catch (Exception e) {
  19. E.printstacktrace ();
  20. }
  21. }
  22. public Void Close () {
  23. try {
  24. This.conn.close ();
  25. This.pst.close ();
  26. } catch (SQLException e) {
  27. E.printstacktrace ();
  28. }
  29. }
  30. }

Write a demo.java to perform the related query operation

Demo.java

Java code
    1. Package Com.hu.demo;
    2. Import Java.sql.ResultSet;
    3. Import java.sql.SQLException;
    4. Public class Demo {
    5. static String sql = null;
    6. static DBHelper db1 = null;
    7. static RESULTSET ret = null;
    8. public static void Main (string[] args) {
    9. sql = "select *from stuinfo"; SQL statements
    10. DB1 = new DBHelper (SQL); Create a DBHelper object
    11. try {
    12. ret = Db1.pst.executeQuery (); //EXECUTE statement to get result set
    13. While (Ret.next ()) {
    14. String uid = ret.getstring (1);
    15. String ufname = ret.getstring (2);
    16. String ulname = ret.getstring (3);
    17. String udate = ret.getstring (4);
    18. SYSTEM.OUT.PRINTLN (uid + "\ T" + ufname + "\ T" + ulname + "\ T" + udate);
    19. }//Display data
    20. Ret.close ();
    21. Db1.close (); //Close connection
    22. } catch (SQLException e) {
    23. E.printstacktrace ();
    24. }
    25. }
    26. }

Java connects MySQL database with JDBC

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.