Simple implementation of JDBC in mac or linux, graphic introduction (with test source code)

Source: Internet
Author: User
(For more information, see http: blogcsdnnetbuptgshengod) 1. Background 1. background because of the need to operate on big data, simple processing of txt files using python cannot meet the requirements. Therefore, after a whole day, the blogger finally completed the JDBC configuration.
A simple list of my environment: mac OS 10.8.5 (same to linux) eclipse 3.4 jdk 1.6.0 database mysql: Server version: 5.6.17 MySQL Community Server (GPL) database driver: mysql-connector-java-5.1.24-bin.jar2. for detailed procedures on installing and configuring mysql, you can check the installation of MYSQL. you need to register an ORACLE account and the like in the middle. it took about half a day. After installation is complete, enter the terminal input (we suggest adding the following sentence to path)
PATH="$PATH":/usr/local/mysql/bin
Then you can enter it. for example, it indicates that the installation is successful.
mysql -u root -p

Then there are some experimental properties. I created a database named test and a table named mytable, which contains two data types: name and sex. Assign values to "wangba" and "m" respectively. these basic SQL statements are relatively simple. for details, see create a database and a database table with MySQL.
Ps: If it is a mac computer, you can install MYSQLworkbench to view the database information conveniently. Previous figure


3. eclipse-java part (1) importing JDBC drivers can be downloaded to the official website based on your database version, or you can use the version packaged in my source code, I use 5.1.24 for mysql databases of more than 5.6. The method to import the jar package is. Right-click the project to be used, and choose -- Properties -- java build path -- Libraries -- add external jar (add jar) -- Order and Export to select the newly added jar package.
(2) code section
Package com. mysql; import java. SQL. *; import com. mysql. jdbc. statement; public class Driver {public static void main (String [] args) {try {Class. forName ("com. mysql. jdbc. driver "); System. out. println ("tested"); java. SQL. connection conn; conn = DriverManager. getConnection ("jdbc: mysql: // localhost/test", "root", "*****"); System. out. println ("conn -------------" + conn); Statement stmt = (Statement) conn. createStatement (); ResultSet rs1_stmt.exe cuteQuery ("select * from mytable"); while (rs. next () {String name = rs. getString ("name"); String sex = rs. getString ("sex"); System. out. println ("name ------" + name + "-------- sex-" + sex) ;}} catch (ClassNotFoundException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}

This sentence
 conn=DriverManager.getConnection("jdbc:mysql://localhost/test","root","*****");
The first double quotation mark in the brackets is the selected database name. In the second double quotation mark: user name (root by default) The third is password: The default is root.


The result is as follows:

4. download source code
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.