SQL---->MYSQL Database 1------JDBC Quick Start

Source: Internet
Author: User

JDBC (Java Data Base Connectivity,java database connection) is a Java API for executing SQL statements that provides unified access to a variety of relational databases.

Objective: To learn the JDBC interface without having to learn the driver of each database.

Here I would like to illustrate a small demo, connect MySQL database, query to the table data:

First of all, the data in my MySQL database:

With JDBC, we're going to introduce Mysql-connector-java-5.1.41-bin.jar

/** * */package cn.snowing;import java.sql.connection;import java.sql.drivermanager;import java.sql.ResultSet;import Java.sql.sqlexception;import java.sql.statement;/** * @author: Snowing * @date: April 27, 2017 * */public class JdbcDemo1 {p Ublic static void Main (string[] args) throws SQLException, classnotfoundexception {String url = "Jdbc:mysql://localhost:3 306/MYDB1 ";//This is OK, the default port//string url =" JDBC:MYSQL:///MYDB1 "; String user = "root"; String Password = "1";//1. The load driver registered two drive//drivermanager.registerdriver (new Com.mysql.jdbc.Driver ());//2. Load driver Common mode, Only one driver Class.forName ("Com.mysql.jdbc.Driver") was registered;//2. Get the connection connection Conne = drivermanager.getconnection (URL, user, password);//3. Gets the Statament object that sends the SQL statement like the database statement st = Conne.createstatement ();//4. Send SQL to the database to get the result set returned by the database resultset rs = St.executequery ("select * from User;"); /5. Get data from an interface set while (Rs.next ()) {System.out.println ("" + Rs.getobject ("id")); System.out.println (Rs.getobject ("username")); System.out.println (Rs.getobject ("Birthday")); System.Out.println (Rs.getobject ("entry_date")); System.out.println (Rs.getobject ("job")); System.out.println (Rs.getobject ("salary")); System.out.println (Rs.getobject ("Resume")); System.out.println (Rs.getobject ("image"));} 6. Release the connection, it is important ah, do not forget!! Rs.close (); St.close (); Conne.close ();}}

Results:

SQL---->MYSQL Database 1------JDBC Quick Start

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.