JAVA Simple Tutorial: Database operations (1)

Is still too many things recently, has been unable to adhere to written copies of daily blog, I'm sorry everyone.

I have decided that this year's teaching to the system, so that everyone can become a real programmer from scratch.

So, first of all we have to learn JDBC calls.

Great majority of our applications, would require the database support. Such as membership information, publish content, and even we write articles, most of which will be saved to the database. How to access and invoke database resources, we wrote most of the programs need to be considered one such example.

Now let's take a look at how we are going to use JAVA to provide the interface to access the database:

package tutorial.sql;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBQueryDemo
{
    public static void main(String[] args) throws SQLException
    {
        // Initialize the database operation class pointer
        Connection conn  = null;
        Statement stmt  = null;
        ResultSet rs  = null;
        // Initialize the database driver class
        try
        {
            // Mount mysql JDBC driver
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch(ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        try
        {
            // Gets the database link
            conn = DriverManager.getConnection("jdbc:mysql://localhost/tutorial","tutorial","tutorial");
            // Gets the  Statement
            stmt = conn.createStatement();
            // Gets a pointer to the records read
            rs = stmt.executeQuery("select * from users");
            // Open the pointer to the next record
            while(rs.next())
                System.out.println(rs.getString("user_name"));
        }
        catch(SQLException ex)
        {
           ex.printStackTrace();
        }
        finally
        {
             // Free link resource
             if(rs !=null)
                 rs.close();
             if(stmt !=null)
                 stmt.close();
             if(conn !=null)
                 conn.close();
        }
    }
}

In the implementation of this code, we need to prepare test database environment. First download a MySQL database, download the following address:
http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-essential-5.1.44-win32.msi/from/http://mysql.ntu.edu.tw/
In addition, we also need to use mysql graphical tools:
http://dev.mysql.com/get/Downloads/MySQLGUITools/mysql-workbench-oss-5.2.16-beta-win32.msi/from/http://mysql.cs.pu.edu.tw/
There need to download it jdbc links package:
http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.12.zip/from/http://mysql.ntu.edu.tw/

Good all Dongdong installed later, after the local to create a database, open the Workbench URI tool.
JAVA Simple Tutorial: Database operations (1)

First, configure a server link, and then you can start start of the

Create a test account:
JAVA Simple Tutorial: Database operations (1)

Execute the following SQL:
/ * Create Schema * /
CREATE SCHEMA IF NOT EXISTS `tutorial` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
/ * Create `users` Table * /
CREATE TABLE IF NOT EXISTS `tutorial`. `Users` (
`user_id` INT NOT NULL,
`user_name` VARCHAR (100) NULL,
`sex` INT NULL,
`birthday` DATE NULL,
PRIMARY KEY ( `user_id`));
JAVA Simple Tutorial: Database operations (1)

Then insert the two test record try
JAVA Simple Tutorial: Database operations (1)

The operation of the tutorial schema privileges granted to the tutorial
JAVA Simple Tutorial: Database operations (1)

JAVA Simple Tutorial: Database operations (1)

Then we implement our program, the results are as follows:
JAVA Simple Tutorial: Database operations (1)

This is our first program reads the database, so we held as an application programmer has taken an important step in the.

分类:Java 时间:2009-05-29 人气:277
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.678 (s). 12 q(s)