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 \"";
- PublicMysqlConnect (){
- 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. getConnection (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 ();
- }
- }
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: