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