Several methods and examples for connecting to databases in Java projects

Source: Internet
Author: User
Tags stmt
Several methods and examples for connecting to databases in Java projects
1. The most primitive writing method (also for Beginners)
First import the database to connect to the basic class, and then the other is simple. In actual operation, you only need to load the driver class and then call the SQL statement. The following is a simple program example.
// 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", "; password ");
// Create statement
Statement stmt = con. createStatement ();
// Execute the query and return the result set
ResultSet rs = stmt.exe cuteQuery ("SELECT a, B, c, d, key FROM Table1 ");
Stmt. close ();
Con. close ();
}
Catch (Exception ex ){
Ex. printStackTrace ();
}
}
}
2. Database connection pool:
As far as I know, most projects now use database connection pools. There is no other reason. In addition to improving network throughput and increasing system performance, there is also a so-called encapsulation of database operations, and most system operations on the database are limited to the centralized scope of CRUD, effective encapsulation. The subsequent programming is quite simple and easy. Other companies offer such dedicated database components for sale.
The connection pool mentioned above also has two types of branches. One is to directly use the connection pool provided by the container, and the other is to write the connection pool by yourself.

Related Article

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.