Java-mysql (1)

Source: Internet
Author: User

I have written many single-sided data in java, and the data storage used is also made of xml or direct files. However, the database is rarely used. I recently learned how to link java to the mysql database.

1. Create a database for testing

 is .--, , Oracle and/ or   help. Type  to > row affected ( sec)

2. Create a user for testing and grant all permissions to the user.

mysql> create user  identified by  rows affected (>> grant all on testdb.* rows affected ( sec)

Third, insert some data into the database:

        Authors(Id   ()) ENGINE     Books(Id   , Title ( (AuthorId)  Authors(Id)        Testing(Id ) ENGINE     Images(Id     Authors(Id, Name) (,   Authors(Id, Name) (,   Authors(Id, Name) (,   Authors(Id, Name) (,   Authors(Id, Name) (,   Books(Id, AuthorId, Title) (, ,   Books(Id, AuthorId, Title) (, ,   Books(Id, AuthorId, Title) (, ,   Books(Id, AuthorId, Title) (, ,   Books(Id, AuthorId, Title) (, ,   Books(Id, AuthorId, Title) (, ,   Books(Id, AuthorId, Title) (, , );

There are several ways to insert data into mysql:

For example:

hudson:hblu root p testdb preparesql.sql

OK, the preparation is complete, and then you start to use java to connect to the database:

Download a jdbc jar file for mysql and directly go to oracle.

        main(String[] args)         Connection connection = = = = "jdbc:mysql://172.20.23.75:3306/testdb"= "root"= "123456"= "show tables"===1 (resultSet !=  (statement !=  (connection != 

First, we need a mysql url:

String sqlurl = "jdbc:mysql://172.20.23.75:3306/testdb";

This address includes the database type, IP address, and port, and the database name.

Then create a connection to the database. The parameters include the url address, user name, and password.

connection =

Create a statement to send SQL statements to the database.

statement = connection.createStatement();

After creation, we use statement to execute the previously defined SQL statement and return a result set.

resultSet = statement.executeQuery(testsql);

Then we process the result set.

Because the result set stores a cursor pointing to the current data row, this cursor first points to the first column of the current data row.

1

Therefore, we first use next to determine whether there is a next column of data. If not, the returned data is empty. If yes, we will output the result, here, getString is used to obtain the data of the nth column.

Finally:

 (resultSet !=  (statement !=  (connection != 

Regardless of whether the database connection is successful or whether data exists, we must determine whether the data connection has been disconnected and whether the object is null before disconnecting the database connection. Otherwise, a null pointer exception will be thrown,

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.