JAVA Simple Tutorial: Database operations (1)
Advertisements
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.
First, configure a server link, and then you can start start of the
Create a test account:
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`));
Then insert the two test record try
The operation of the tutorial schema privileges granted to the tutorial
Then we implement our program, the results are as follows:
This is our first program reads the database, so we held as an application programmer has taken an important step in the.
Related Posts of JAVA Simple Tutorial: Database operations (1)
-
In the servlet use Bean
According to Sun's definition, JavaBean is a reusable software components. In fact JavaBean is a Java class, through the package into a property and methods of treatment of a function or a business object, referred to as bean. Because JavaBean is ...
-
hibernate generic generic DAO
package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *
-
Servlet brief introduction
Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...
-
can not be represented as java.sql.Timestamp
Development of procedures for the use of hibernate when, some time there is no need to fill in the fields, but after the hibernate query time reported "Java.sql.SQLException: Value'0000-00-00 'can not be represented as java.sql.Timestamp ...
-
First Hibernate Example
Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co
-
Hibernate annotation using notebook
These are the basic common @Entity --Declared an entity bean @Table(name="promotion_info") --For the entity bean mapping for the specified table (Table name ="promotion_info) @Id --Declare that the identifying attribute of the entity bean @GeneratedValue
-
Struts2 + hibernate + spring problem user log in
dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q












