Java JDBC connection to the database

Source: Internet
Author: User

This article is just a summary of my younger brother's experience.

 

Although Java JDBC is used to connect to the database, it is difficult to have a certain MySQL Foundation. Here, I suggest using command line input,

Familiar with the syntax, the graphical interface is not very suitable for beginners. After all, familiar statements are king!

Here, I will only introduce the commonly used MySQL statements. You can skip this step. What about connecting Java JDBC to the database? 2

If you have time, you can review and correct your mistakes. I am very grateful to you!

 

 

I. [basic operations]

1. Modify the database encoding: Find the my. ini file in the installation directory and modify all [default-character-set] content:
[Default-character-set = gbk] After modification, restart the server.

2. Connect to the mysql database
Mysql-u username-p Password

 

 

Ii. [Create a database]

1. create a database: [create database name ;]

2. delete a database: [drop database name ;]

 

Iii. database usage]

1. [use Database Name ;]

 

4. [Create a database table]

Create table mytab (
Id int auto_increment primary key,
Name varchar (30) not null,
Password varchar (32) not null,
Age int not null
);

[Auto_increment primary] automatic Growth

 

V. delete database tables]

1. drop table name;

 

6. [View table structure]

1. desc table name;

 

7. [view database information]

1. view all databases: show databases;

2. view all tables in a database: show tables;

 

8. [add data]

1. insert into Table Name (Field 1, Field 2, Field 3) values
(Value 1, value 2, value 3 );

Note: when inserting a string, use ['] To enclose it.

2. the date in the MySQL database is in the yyyy-mm-dd format.
When the input time is:
]

 

Note:

The number of fields before and after data insertion must be the same as the number of values assigned!

If there is an automatic value assignment, you do not need to declare it. You do not need to assign a value later ]!

If there is an Automatically increasing field before and the field is assigned a value later, it will change!

If a value is assigned to a string, it is converted to a number. This does not understand what it is .... Is it a Bug?

[So] to avoid unnecessary errors, follow the rules. 0.0 --!

 

9. [delete data]

1. delete from table name [delete condition];

NOTE: If no deletion condition exists, the data of the entire table will be deleted.
Generally, [id] is used as the condition.

[Example:] delete from user where id = 1;

 

10. [update data]

1. update table name set field 1 = value 1,... field n = value 1 [where
Update condition];

NOTE: If no conditions exist, all data is changed.
[Example:] update user set
Name = 'mldn', age = 5, birthday = '2017-03-23 'where id = 2;

 

11. [query data]

1. Simple query statements

A. query all databases
Select * from user; [user is a table]

B. query specific fields
Select name, age from user;

C, like statement, used for fuzzy query of data, usually used together with [%]
'%' Indicates matching any data
Select * from user where name like '% m %' or
Password like '% m % ';

Note: [or], [and]
[And]: Find data with both conditions
[Or]: A condition is valid.

 

D. [limit] -- limit the number of lines displayed. Too much data is helpful for observation!

[It is widely used in paging development !]

Select * from class limit 2, 3;
Note: lines from 3rd to 4th are displayed, because the first line is 0, you know!

 

 

At this point, the basic syntax of MySQL is provided,

In the next chapter, we will officially connect to JDBC.

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.