Connect to the MySQL database using JDBC

Source: Internet
Author: User

It is convenient to connect to the MySql server using Jdbc.

First, import jdbc to the project or put jdbc into ClassPath. Here, I use Eclipse to directly import the jdbc jar file.

Then, the DriverManager is developed, And the froName of the Class is directly completed using the simplest method. The Code is as follows:

Class. forName ("com. mysql. jdbc. Driver"). newInstance ();

Then, instantiate a Connection. Pay attention to the user name and password. There are several methods to choose from. Here I use the getConnection (String url, String user, String password) method of the DirverManager class. Specific Use: DriverManager

For example: Connection conn = DriverManager. getConnection ("jdbc: mysql: // localhost/mydatabase", "root", "1234 ");

Next, create a Statement for executing the SQL Statement. This is easy to do with one line of code:

Statement stat = conn. createStatement ();

Finally, you can use the stat instance to execute SQL statements. For details, refer to Statement.

Sample Code:

The created mydatabase contains a mytable table that contains an integer id and a text content.

Use the following code to view the content of the first 20 rows in mytable.

  1. PackageCom. tobacco. mysqltest;
  2. ImportJava. SQL. Connection;
  3. ImportJava. SQL. DriverManager;
  4. ImportJava. SQL. ResultSet;
  5. ImportJava. SQL. SQLException;
  6. ImportJava. SQL. Statement;
  7. Public ClassMain {
  8. Private StaticConnection conn;
  9. Private StaticStatement stat;
  10. Private StaticResultSet rs;
  11. Public Static VoidMain (String [] args ){
  12. Try{
  13. Class. forName ("Com. mysql. jdbc. Driver"). NewInstance ();
  14. System. out. println ("Load jdbc successfully");
  15. }Catch(InstantiationException e ){
  16. // TODO Auto-generated catch block
  17. E. printStackTrace ();
  18. }Catch(IllegalAccessException e ){
  19. // TODO Auto-generated catch block
  20. E. printStackTrace ();
  21. }Catch(ClassNotFoundException e ){
  22. // TODO Auto-generated catch block
  23. E. printStackTrace ();
  24. }
  25. Try{
  26. Conn = DriverManager. getConnection ("Jdbc: mysql: // localhost/mydatabase","Root","1234");
  27. Stat = conn. createStatement ();
  28. IntN =20;
  29. IntI =1;
  30. While(I <n ){
  31. Rs = stat.exe cuteQuery ("SELECT * FROM mytable WHERE id ="+ I );
  32. If(Rs! =Null){
  33. Rs. first ();
  34. String content = rs. getString (rs. findColumn ("Content"));
  35. System. out. println (content );
  36. }
  37. I ++;
  38. }
  39. }Catch(SQLException e ){
  40. // TODO Auto-generated catch block
  41. E. printStackTrace ();
  42. }
  43. }
  44. }

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.