Java -- JDBC example notes, javajdbc example

Source: Internet
Author: User

Java -- JDBC example notes, javajdbc example

First hanging out of the Big Brother's portal: http://www.cnblogs.com/erbing/p/5805727.html

  • Basic Steps

 

To do well, you must first sharpen your tools.

First, you need a jdbc jar package. I use mysql. This jar package can be downloaded directly from the official website.

Transport: https://dev.mysql.com/downloads/connector/j/3.1.html

Open the jar package.

After downloading the jar package, we can officially write the code.

Step 1: load the driver

1 Class.forName("com.mysql.jdbc.Driver");

Baidu is the driver of other databases.

 

Step 2: establish a connection

1 Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);

These three parameters are respectively the URL of the database, the user of the database, and the password.

Mysql URL is usually written as follows: jdbc: mysql: // hostname/dbname

In fact, drivers, URLs, users, and passwords can be written as constants.

    public static final String DRIVER = "com.mysql.jdbc.Driver";    public static final String URL = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8";    public static final String USER = "root";    public static final String PASSWORD = "12345";

As for why my URL is written so long, it prevents Chinese garbled characters. This is the case for Baidu.

 

Step 3: Create a statement

At first I was thinking that I had already connected to the database. Can't I write SQL statements directly? Later, I figured it out. The connection is like a network cable connecting your computer and database. However, communication between the computer and the database requires a protocol (language) that can be understood. (Just like communication between browsers and servers over http) This creation statement allows computers and databases to reach a consensus and explore their lives.

1 Statement statement = connection.createStatement();

 

Step 4: Execute the statement

Where are the statements executed? Result set

ResultSet resultSet = statement.executeQuery("SELECT * FROM student");

If you want to add, delete, modify, or use

statement.executeUpdate(sql);

 

Step 5: process the result set

The query result is placed in the result set.

There is a resultSet. next () method, which points to the first row of the first null data at the beginning. The next Pointer Points to the next data point.

ResultSet. get... there are many ways to get data. For example, getString (1), getString ("subject"), getShort (1), getInt (1 );

while (resultSet.next()) {            System.out.println(resultSet.getString("id") + "\t" + resultSet.getString("subject") + "\t"                    + resultSet.getString("score"));        }

 

Step 6: Close the connection

What should I do at last ???

I will not write the standard writing method. Just write a simple writing method.

            resultSet.close();            statement.close();            connection.close();    

The order of creation is reversed.

 

Paste the complete code below:

Public class SimpleJDBC {public static void main (String [] args) throws SQLException, ClassNotFoundException {// 1. load the driver Class. forName (JDBCUtil. DRIVER); System. out. println ("Driver loaded"); // 2. establish Connection connection = DriverManager. getConnection (JDBCUtil. URL, JDBCUtil. USER, JDBCUtil. PASSWORD); System. out. println ("Database connected"); // 3. create Statement statement Statement = connection. createStatement (); // 4. execute the statement ResultSet resultSet = statement.exe cuteQuery ("SELECT * FROM student"); // 5. process ResultSet while (resultSet. next () {System. out. println (resultSet. getString ("id") + "\ t" + resultSet. getString ("subject") + "\ t" + resultSet. getString ("score");} // 6. guess what the connection is. close ();}}

 

Running result:

 

 

 

Write it here first. After learning, continue writing.

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.