Connect to the MySQL database using LinuxJava

Source: Internet
Author: User
Linux (Ubuntu platform) Java through JDBC to connect to MySQL database, similar to Windows platform, steps are as follows: Download jdbc: mysql-connector-java-5.1.18.tar.gz unzip jdbc: tar-zxvfmysql-connector-java-5.1.18.tar.gz configuration jdbc: cpmysql-connector-java-5

Linux (Ubuntu platform)JavaConnect to the MySQL database through JDBC, which is similar to that on the Windows platform. The procedure is as follows:

DownloadJdbc: mysql-connector-java-5.1.18.tar.gz

ExtractJdbc: tar-ZxvfMysql-connector-java-5.1.18.tar.gz

ConfigurationJdbc:CpMysql-connector-java-5.1.18-bin.jar/usr/local/jdk1.6.0 _ 22/jre/lib/ext/

Complete source code:

Free onHttp://linux.linuxidc.com/

Both the user name and password areWww.linuxidc.com

The specific download directory is in/Pub/2011/10/12/Linux Java connection to MySQL database/


Java example:

Private static String SQL = "select top, id, name, country, dtime from gametop800 where id = \" com. rovio. angrybirds \"";

  1. PublicMysqlConnect (){
  2. String dbDriver ="Com. mysql. jdbc. Driver";
  3. String url ="Jdbc: mysql: // localhost/top800";
  4. String username ="Root";
  5. String password ="";
  6. Statement mStatement =Null;
  7. ResultSet mResultSet =Null;
  8. Connection mConnection =Null;
  9. String SQL ="Select top, id, name, country, dtime from gametop800 where top <= 20";
  10. Try{
  11. Class. forName (dbDriver). newInstance ();
  12. MConnection = DriverManager. getConnection (url, username, password );
  13. MStatement = mConnection. createStatement ();
  14. MResultSet = mStatement.exe cuteQuery (SQL );
  15. Try{
  16. While(MResultSet. next ()){
  17. System. out. print (mResultSet. getInt (1) +"\ T");
  18. System. out. print (mResultSet. getString (2) +"\ T");
  19. System. out. print (mResultSet. getString (3) +"\ T");
  20. System. out. println (mResultSet. getString (4));
  21. }
  22. }Catch(Exception e ){
  23. System. out. println ("Database read error! \ N"+ E. getMessage ());
  24. }
  25. }Catch(SQLException e ){
  26. System. out. println ("Database connection error: \ n"+ Url +"\ N"+ E. getMessage ());
  27. }Catch(Exception e ){
  28. E. printStackTrace ();
  29. }Finally{
  30. // MStatement. close ();
  31. // MConnection. close ();
  32. }
  33. }
Public mysqlConnect () {String dbDriver = "com. mysql. jdbc. driver "; String url =" jdbc: mysql: // localhost/top800 "; String username =" root "; String password =" "; Statement mStatement = null; resultSet mResultSet = null; Connection mConnection = null; String SQL = "select top, id, name, country, dtime from gametop800 where top <= 20"; try {Class. forName (dbDriver ). newInstance (); mConnection = DriverManager. getConne Ction (url, username, password); mStatement = mConnection. createStatement (); mResultSet = mStatement.exe cuteQuery (SQL); try {while (mResultSet. next () {System. out. print (mResultSet. getInt (1) + "\ t"); System. out. print (mResultSet. getString (2) + "\ t"); System. out. print (mResultSet. getString (3) + "\ t"); System. out. println (mResultSet. getString (4) ;}} catch (Exception e) {System. out. println ("database read error! \ N "+ e. getMessage () ;}} catch (SQLException e) {System. out. println ("database connection error: \ n" + url + "\ n" + e. getMessage ();} catch (Exception e) {e. printStackTrace ();} finally {// mStatement. close (); // mConnection. close ();}}

Execution result:

 

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.