JDBC Pure Drive mode connection MySQL

Source: Internet
Author: User
Tags stmt

1 Create a new Java Project named Mysqldemo

2 Download the latest driver package from http://dev.mysql.com/downloads/connector/j/.

Here are the. tar.gz and. zip Two-format packages, because they can be decompressed under Windows, either way.

3 after extracting the downloaded drive package, copy the Mysql-connector-java-5.1.38-bin.jar to the project

4 Creating a Java class named Mysqldemo in the project

5 Writing code in Mysqldemo.java

[Java]View PlainCopy 
  1. Package com.abc;
  2. Importjava.sql.DriverManager;
  3. Importjava.sql.ResultSet;
  4. Importjava.sql.SQLException;
  5. Importjava.sql.Connection;
  6. Importjava.sql.Statement;
  7. Publicclass Mysqldemo {
  8. Publicstaticvoid Main (string[] args) throws Exception {
  9. Connection conn = null;
  10. String SQL;
  11. //MySQL JDBC URL writing method: jdbc:mysql://Host Name: Connection port/database name? parameter = value
  12. //Avoid Chinese garbled to specify Useunicode and characterencoding
  13. String url = "Jdbc:mysql://localhost:3306/test?"
  14. + "User=root&password=123456&useunicode=true&characterencoding=utf8";
  15. try {
  16. //The reason to use the following statement is to use the MySQL driver, so we have to drive it up,
  17. //Can be loaded through the class.forname, can also be driven by initialization, the following three forms can be
  18. Class.forName ("Com.mysql.jdbc.Driver"); Dynamic load MySQL driver
  19. //or:
  20. //Com.mysql.jdbc.Driver Driver = new Com.mysql.jdbc.Driver ();
  21. //or:
  22. //New Com.mysql.jdbc.Driver ();
  23. System.out.println ("load MySQL driver successfully");
  24. //A connection represents a database connection
  25. conn = drivermanager.getconnection (URL);
  26. //Statement with many methods, such as executeupdate can be inserted, update and delete, etc.
  27. Statement stmt = Conn.createstatement ();
  28. sql = "createtable student (no char (), name varchar (), primary key (No))";
  29. Intresult = stmt.executeupdate (sql); the///Executeupdate statement returns an affected number of rows if the return-1 does not succeed
  30. if (Result! =-1) {
  31. System.out.println ("Create data Table Success");
  32. sql = "INSERT into student (No,name) VALUES (' 2016001 ', ' Liu Big ')";
  33. result = Stmt.executeupdate (SQL);
  34. sql = "INSERT into student (No,name) VALUES (' 2016002 ', ' Chen er ')";
  35. result = Stmt.executeupdate (SQL);
  36. sql = "SELECT * from student";
  37. ResultSet rs = stmt.executequery (SQL); //ExecuteQuery Returns a collection of results, otherwise returns a null value
  38. System.out.println ("School number \ t name");
  39. While (Rs.next ()) {
  40. System.out.println (rs.getstring (1) + "\ T" + rs.getstring (2)); If the type int is returned can be used Getint ()
  41. }
  42. }
  43. } catch (SQLException e) {
  44. System.out.println ("MySQL operation Error");
  45. E.printstacktrace ();
  46. } catch (Exception e) {
  47. E.printstacktrace ();
  48. } finally {
  49. Conn.close ();
  50. }
  51. }
  52. }

Operation Result:

JDBC Pure Drive mode connection 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.