Connect to the MySQL database using Linux Java

Source: Internet
Author: User

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/


Java example:

Private Static string SQL = "select top, ID, name, country, dtime from gametop800 where id = \" com. Rovio. angrybirds \"";

Public mysqlconnect () {<br/> string dbdriver = "com. mySQL. JDBC. driver "; <br/> string url =" JDBC: mysql: // localhost/top800 "; <br/> string username =" root "; <br/> string Password = ""; </P> <p> statement mstatement = NULL; <br/> resultset mresultset = NULL; <br/> connection mconnection = NULL; </P> <p> string SQL = "select top, ID, name, country, dtime from gametop800 where top <= 20 "; </P> <p> try {<br/> class. forna Me (dbdriver ). newinstance (); <br/> mconnection = drivermanager. getconnection (URL, username, password); <br/> mstatement = mconnection. createstatement (); <br/> mresultset = mstatement.exe cutequery (SQL); </P> <p> try {<br/> while (mresultset. next () {<br/> system. out. print (mresultset. getint (1) + "\ t"); <br/> system. out. print (mresultset. getstring (2) + "\ t"); <br/> system. out. print (mresultset. getstring (3) + "\ t "); <Br/> system. out. println (mresultset. getstring (4); <br/>}< br/>} catch (exception e) {<br/> system. out. println ("database read error! \ N "+ E. getmessage (); <br/>}< br/>} catch (sqlexception e) {<br/> system. out. println ("database connection error: \ n" + URL + "\ n" + E. getmessage (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}finally {<br/> // mstatement. close (); <br/> // mconnection. close (); <br/>}< br/>}

Execution result:

Complete source code

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.