Deploy the MySQL JDBC driver in Eclipse in Ubuntu

Source: Internet
Author: User

1. MySQL JDBC driver package: http://dev.mysql.com/downloads/connector/j/

Version: mysql-connector-java-5.1.17.zip

2. Download any directory decompress to/mysql-connector-java-5.1.17/mysql-connector-java-5.1.17-bin.jar;

/Mysql-connector-java-5.1.17/directory also has docs and src folder, src folder has lib directory;

3. Prerequisites: the mysql and eclipse environments have been installed. refer to the previous blog article on installing the j2ee environment and mysql In Ubuntu;

4. Open eclispe, go to the menu windows-> prefrences, go to Java-> Build Path-> user libraries, select new, name mysql, Path:/mysql-connector-java-5.1.17/mysql-connector-java-5.1.17-bin.jar;

5. Create a java project named mysqlcon, right-click it, select Build Path-> Add libraries, select user library from the list, and select the mysql installation package deployed above in the next interface;

6. for development, create a new class under the mysqlcon project, name it my, and set the package to cn, that is, the java file of cn. my:

Package cn;
import java.sql.*;
Public ClassMy {Public Static VoidMain (String args []) {Try{Class. forName ("Com. mysql. jdbc. Driver");// Load the mysql jdbc driverSystem. out. println ("Success loading Mysql Driver!");}Catch(Exception e) {System. out. print ("Error loading Mysql Driver!"); E. printStackTrace ();}Try{Connection connect=DriverManager. getConnection ("Jdbc: mysql: // localhost: 3306/dbname","Root","Password");System. out. println ("Success connect Mysql server!");
// Select code Statement stmt=Connect. createStatement (); ResultSet rs=Stmt.exe cuteQuery ("Select * from t_auth");//T_auth indicates the table name.  While(Rs. next () {System. out. println (rs. getString ("Auth_path"));//Auth_path is a table.T_auth Field
 }
 //insert into code
PreparedStatement=Connect. prepareStatement ("INSERTT_authVALUES (?,?)");Statement. setString (1,"2"); Statement. setString (2,"System Management"); Statement.exe cuteUpdate ();}Catch(Exception e) {System. out. print ("Get data error!"); E. printStackTrace ();}}}

Displayed and inserted correctly!

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.