How Java connects to MySQL database

Source: Internet
Author: User

Connect to the database using JDBC mode.

1. Add MySQL driver jar package

I'm using this driver package Mysql-connector-java-5.1.26-bin.jar.

How to add:

2. Load MySQL driver class

1 Private voidLoaddriver () {2         Try {3String forname = "Com.mysql.jdbc.Driver";4             //load the MySQL driver class5 Class.forName (forname);6System. out. println ("MySQL driver loaded successfully");7}Catch(ClassNotFoundException e) {8System. out. println ("driver class not found, load driver failed! ");9             //e.printstacktrace ();Ten         } One}

3. Connect to the database

1  Public voidOpenlik () {2         Try {3String url = "Jdbc\:mysql\://localhost\:3306/test1";4String username = "root";5String password = "root";6System. out. println (URL + username +password);7Con =drivermanager.getconnection (URL, username, password);8System. out. println ("Database Connection Successful! ");9}Catch(SQLException se) {TenSystem. out. println ("Database connection Failed! "); One             //se.printstacktrace (); A         } -}

4. Create database execution commands and pass in SQL action statements

1  PublicStatement Getsqlstringcommand (String query) {2         //TODO auto-generated Method Stub3PreparedStatement pstmt =NULL;4         Try {5PSTMT =con.preparestatement (query);6System. out. println ("Get Command succeeded");7}Catch(Exception ex) {8System. out. println ("failed to get command");9             //ex.printstacktrace ();Ten         } One         returnpstmt; A}

5. Operation of SQL command statements according to database commands

1  PublicResultSet executedataset (Statement cmd) {2         //TODO auto-generated Method Stub3ResultSet RS =NULL;4PreparedStatement Cmd_ =(preparedstatement) cmd;5         Try {6RS =cmd_.executequery ();7System. out. println ("Get ResultSet Success");8}Catch(Exception ex) {9System. out. println ("Get resultset failed");Ten             //ex.printstacktrace (); One         } A         returnRS; -}

This allows the database to be connected!!!

How Java connects to MySQL database

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.