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 人气:281
分享到:
blog comments powered by Disqus

相关文章

  • Mysql connection problems: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException 2011-05-16

    com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections" 1.Mysql database default number of connections is 100. 2 The default value of 100

  • java.sql.SQLException: Io exception: Connection refused 2010-11-03

    Specific exceptions are: java.sql.SQLException: Io exception: Connection refused (DESCRIPTION = (TMP =) (VSNNUM = 168821248) (ERR = 12505) (ERROR_STACK = (ERROR = (CODE = 12505) (EMFI = 4)))) at oracle.jdbc.dbaccess.DBError.throwSqlException (DBError

  • java.sql.SQLException: Io exception: Connection refused (DESCRIPTION = (TMP =) (VSNNUM = 18664 2011-02-11

    java.sql.SQLException: Io exception: Connection refused (DESCRIPTION = (TMP =) (VSNNUM = 169869568) (ERR = 12519) (ERROR_STACK = (ERROR = (CODE = 12519) (EMFI = 4)))) at oracle. jdbc.dbaccess.DBError.throwSqlException This problem, I let him check th

  • Java程序员可能犯的3个常见SQL错误 2014-06-14

    Java程序员不仅要具备扎实的Java编程能力,在日常的工作当中往往还要涉及到其他语言的基础知识,尤其是SQL.那么哪些常见的SQL错误是程序员们容易犯的呢?让我们一起来看看吧! 你可能看到Java程序员每周的工作是编码开发一个可伸缩的Web应用程序,或创建一个动态的网站,或者开发高效的电子商务产品页面,也可能是开发一个Android应用程序等等.但是,即使他们致力于不同的项目,却往往都有一个共同点,那就是编程! 他们的职业要求长时间的工作来积累更多的编程知识.Java程序员还需要了解项目的需求

  • PowerDesigner Database connection error: Non SQL Error: Could not load class 2011-03-30

    PowerDesigner Database connection error: Non SQL Error: Could not load class PowerDesigner is a bug. The solution: drive files, such as: ojdbc14.jar copied to the PowerDesigner installation directory, then edit the connection configuration, select th

  • Solution java.lang.classcastexception oracle.sql.blob cannot be cast to oracle.sql.blob 2010-06-03

    Save the file to the database at the time, reported java.lang.classcastexception oracle.sql.blob cannot be cast to oracle.sql.blob Solutions, see red OutputStream out = rs.getBlob (1). SetBinaryStream (1L);

  • String [] args = new String [] {sql.toString ()} 2011-06-29

    In this problem StringBuffer sql = new StringBuffer (); Which sql.toString results: 'Test User 1', 'male' String [] args = new String [] {sql.toString ()} args.length 1 results Obviously not what we need. Note: new String []{"","",&quo

  • Java socket - an example with a connection pool 2010-04-12

    An example with a connection pool 1. Introduction We now have the MultithreadedServer whenever a client to apply for a connection in a new Thread create a new ConnectionHandler. This means that there may be a bunch of Thread "lying" around us. A

  • JAVA Simple Tutorial: Database operations (2) 2008-09-02

    Today, the procedure to analyze the last of the deletions and the database search change operation. First of all, we use the Class.forName method to initialize the string to the corresponding categories, namely Class.forName("com.mysql.jdbc.Driver&qu

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

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

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