Several ways and examples of connecting databases in Java projects
Source: Internet
Author: User
Connecting databases | Several ways and examples of connecting databases in Project Java projects
1, the most original writing (also the general beginners to the wording)
First import the database connection base class, then the others are then simple. Just load the driver class during the actual operation, and then call the SQL statement. Here is a simple example of a program.
Select.java
Import Java.net.URL;
Import java.sql.*;
Class select{
public static void Main (string[] args) {
try{
Create a Connection URL
String url = "Jdbc:odbc:wombat";
Establish a connection
Connection con = drivermanager.getconnection (URL, "Usre", ";p assword");
Creating statements
Statement stmt = Con.createstatement ();
Execute query, return result set
ResultSet rs = stmt.executequery ("Select A,b,c,d,key from Table1");
Stmt.close ();
Con.close ();
}
catch (Exception ex) {
Ex.printstacktrace ();
}
}
}
2, the use of database connection pool:
As far as I know, most projects now use database connection pools. The reason is not other, in addition to improve network throughput, increase system performance; There is another, it is the so-called, encapsulation database operations, most of the system to the database operation is limited to the concentration of crud within the scope of effective encapsulation. The process behind it is quite simple and easy to write. There are also companies that offer this proprietary database component for sale.
As mentioned in the connection pool, there are two branches, one is the direct use of the container provided by the connection pool, the first is to write their own connection pool.
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