JDBC Java Database connection 1) Getting Started with JDBC

Source: Internet
Author: User
Tags driver manager mysql client

  before manipulating Data

1) Log in to the database server (mysql-u root-p password) via MySQL client tool

2) Writing SQL statements

3) Send SQL statement to database server execution

What is jdbc?

the technique of sending SQL statements using Java Code (Programs) is jdbc technology!!!!

using jdbc to send sql Prerequisites
    1. Log on to the database server (connect to the database server)
    2. of the database IP Address
    3. Port
    4. Database user Name
    5. Password

Code:

  

1  Public classMysql_demo1 {2 3         //Create a URL4         Private StaticString url = "jdbc:mysql://localhost:3306";5     6         //CREATE DATABASE Information7         Private StaticString user = "root";8         Private StaticString Possword = "root";9     Ten         //The first way One         Private Static voidConnection1 ()throwsSQLException { A              -             //creating a Driver class object -Driver Driver =Newcom.mysql.jdbc.Driver (); the              -             //receive database information with a collection -Properties Pro =NewProperties (); -Pro.getproperty ("User", user); +Pro.getproperty ("Possword", Possword); -              +             //Connection AConnection con =driver.connect (URL, pro); at  -         } -          -         //The second way -         /** - * Connect with Driver Manager in          * @throwsSQLException -          */ to      +         Private Static voidConnection2 ()throwsSQLException { -              the             //Registering Drivers *             //through the source code, we know that this method is automatically registered, so there is no need to new a drive object $Driver Driver =Newcom.mysql.jdbc.Driver ();Panax Notoginseng              -             //connect to a specific database theConnection conn =drivermanager.getconnection (URL, user, possword); +                  A         } the          +          // third method (recommended)  -         /** $ * from See driver driver = new Com.mysql.jdbc.Driver (); Source Code $ * As you know, the method inside it is a static method that starts with the load of the class, so we can call it directly with the class, passing the URL field past -          * @paramargs -          * @throwsException the          */ -         Private Static voidConnection3 ()throwsException {Wuyi              the              // pass the URL field directly past, load the static code, and register the driver  -Class.forName ("Com.mysql.jdbc.Driver"); Wu              -             // Connect to a specific database  About             //drivermanager.getconnection (URL, user, Possword): An attempt was made to establish a connection to a given database URL.  $Connection con =drivermanager.getconnection (URL, user, possword); -              -         } -              A          Public Static voidMain (string[] args)throwsException { + Connection3 (); the         } -              $}

JDBC Java database connection 1) Getting Started with JDBC

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.