The use of JDBC for web development

Source: Internet
Author: User

1.JDBC Technology Access database

in the Dynamic web development, the database must be used, because the dynamic Web implementation is mainly to achieve the interaction with the user, and the implementation of the interaction is mainly by the help of our number
Database connection is primarily a CRUD (create,read,update,delete) operation. CRUD operations are performed by
SQL statement is completed.

Review the basic SQL statement format:

Create:insert into table name (Field 1, Field 2,......, field N) VALUES (value 1, value 2,......, value N)
Example: INSERT INTO Firstleveltitle (titlename,creator,createtime) VALUES ("Learning", "GXS", Now ())

Read:select * from table name [Where condition] Select field name from table name [where condition]
Example: SELECT * from Firstleveltitle where creator= "GXs"

Update:update table Name Set field = value
Example: Update firstleveltitle set titlename= "Web Learning"

Delete:delete from table name where condition
Example: Delete from Firstleveltitle where id>5

This code is used when connecting to a database!

2.JDBC Working principle

So, let's talk about our database connection:

The Java language connects databases using JDBC (Java Data Base Connectivity) technology, and JDBC provides the ability to connect to a variety of databases.

Connecting to JDBC can be connected to a variety of data, such as mysql,oracle,sqlserver,db2 more commonly used, but I prefer to use MySQL, because not only small, but also practical.

JDBC Code Templates

try {
Class.forName (JDBC driver Class); Registering the JDBC driver, provided by the database vendor
} catch (ClassNotFoundException e) {
System.out.println ("Unable to find driver class");
}
try {
Gets the database connection that the JDBC URL uses to identify the database
Connection con=drivermanager.getconnection (JDBC URL, database user name, password);
Get Statement object, execute SQL statement
Statement stmt = Con.createstatement ();
ResultSet rs = stmt.executequery ("Select a, B, C from Table1");
Processing results
while (Rs.next ()) {
int x = Rs.getint ("a");
String s = rs.getstring ("B");
float f = rs.getfloat ("C");
}
Con.close ();//Release resources
} catch (SQLException e) {
E.printstacktrace ();
}

The use of JDBC for web development

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.