Updated March 29, 2023
Introduction to JDBC datasource
JDBC provides a different way for the user to connect the database, in which the DataSource is one of the ways used to establish the connection with the database. Basically, JDBC provides different options to identify the data source, how data is handled over the database connection, and how transactions are handled from the different data sources, either global or local. In other words, we can say that it provides the connection pooling and distributed transaction over the database connection. Basically, this functionality is useful for enterprise database computing.
Syntax
DriverManagerDataSource dataSource_obj = new_obj DriverManagerDataSource();
dataSource.specifedDriverClassName( "com.mysql.jdbc.Driver");
dataSource.specifiedUrl( " jdbc:mysql://localhost :");
dataSource.specifiedUsername( "specified username");
dataSource.specifiedPassword( "user defined password");
Explanation
In the above syntax, we try to implement the JDBC datasource; here, we must define the different parameters such as JDBC driver, url, specified username, and password as shown in the above syntax. The Username and Password depend on the user requirement. Similarly, we can write the syntax for oracle database and other as per user requirement.
How does datasource work in JDBC?
Now let’s see the datasource in JDBC as follows.
To work with information from a data set, we need to get an association with the data set. The manner in which Spring does this is through a DataSource. A DataSource is important for the JDBC determination and can be viewed as a summed-up association industrial facility. It permits a holder or a system to shroud association pooling and exchange the board issues from the application code. As a designer, you don’t have to know any insights regarding how to interface with the data set; that is the executive’s obligation to set up the datasource. You will undoubtedly need to satisfy the two jobs while you are creating and testing your code; however, you won’t really need to know how the creation information source is designed.
When utilizing Spring’s JDBC layer, you can either get an information source from JNDI or design your own, utilizing an execution given in the Spring conveyance. The last proves to be useful for unit testing outside of a web holder. We will utilize the DriverManagerDataSource execution for this part; however, a few extra executions will be covered later on. The DriverManagerDataSource works the same way you presumably are utilized when you acquire a JDBC association. First, you need to indicate the completely qualified class name of the JDBC driver that you are utilizing so the DriverManager can stack the driver class. Then, at that point, you need to give a URL that differs between JDBC drivers. Next, you need to counsel the documentation for your driver for the right worth to use here. At last, you should give a username and a secret phrase that will be utilized to associate with the data set.
Now let’s see how we can utilize the datasource in different ways as follows.
A layer of reflection: You can design JDBC DataSource in the application worker in an undertaking application and register it with JNDI. In those ways, clients simply need to know the heading’s consistent name for the DataSource, and the DB explicit subtleties are covered up.
Association pooling: If the pool has an association that can fulfill the solicitation, it returns the association with the application. On the off chance that all associations within the pool are being used, another association is made and gotten back to the application. The application utilizes the association to work on the data set and afterward gets the item again to the pool. The association is then accessible for the following association demand.
By pooling the association, you can reuse the association protests rather than making it each time that further develops execution for information base concentrated applications on the grounds that making association objects is expensive both as far as time and assets.
Conveyed exchange: DataSource executions can likewise give an association object that can be utilized in dispersed exchanges. A circulated exchange is an exchange that gets to at least two DBMS workers.
Now let’s see what the different DataSource interfaces available in Java are as follows.
Basically, JDBC provides the different interfaces that we call API users, such as getConnection(), getConnection(parameters), etc.
Normally both methods are used as a connection object. For implementation, we required the JDBC driver. The JDBC provides different drivers for different databases such as MySQL and Oracle.
Examples of JDBC datasource
Now let’s see different examples of JDBC DataSource for better understanding as follows.
First, we need to create a PreparedStatement that is D_source. Java file and write the following code as follows.
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public class D_source {
private static final String d_class = "com.mysql.jdbc.Driver";
private static final String c_url= "jdbc:mysql://localhost";
private static final String u_name = "root";
private static final String u_pass= "root";
private static D_source ds_obj;
private MysqlDataSource ds_obj = new MysqlDataSource();
private Da_Source(){
ds_obj.setDriverClassName(d_class);
ds_obj.setUrl(c_url);
ds_obj.setUser(u_name);
ds_obj.setPassword(u_pass);
}
public static Da_source getInstance(){
if(ds_obj == null){
ds_obj = new DataSource();
}
return ds_obj;
}
public MysqlDataSource getds_obj() {
return ds_obj;
}
public void setds_obj(MysqlDataSource ds_obj) {
this.ds_obj= ds_obj;
}
}
Now let’s create the connection file that ds_connection.java and write the following code as follows.
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public class ds_connection {
public static void main(String[] args) {
ds_connection ds_obj = new ds_connection();
try {
ds_obj.displaystud(3);
} catch (SQLException e_obj) {
e_obj.printStackTrace();
}
}
private void displaystud(string dept) throws SQLException{
Connection con_obj = null;
String sql_statement = "Select * from stud where dept = ?";
PreparedStatement p_stmt = null;
try {
MysqlDataSource mds_obj = DataSource.getInstance().getds_obj);
con_obj = mds.getcon_obj();
p_stmt = con_obj.prepareStatement(sql_statement);
p_stmt.setstring( dept);
ResultSet rs_obj = p_stmt.executeQuery();
while(rs_obj.next()){
System.out.println(
"Stud_Name: " + r_stmt.getString("name"));
System.out.println(
"Dept:" + r_stmt.getString("dept"));
}
}finally{
if(p_stmt!= null){
p_stmt.close();
}
if(con_obj != null){
con_obj.close();
}
}
}
}
Explanation
We illustrated the final output or result of the above program using the following screenshot as follows.
Conclusion
We hope from this article you learn the JDBC datasource. From the above article, we have learned the different types of datasource with their basic syntax, and we also see different examples of JDBC datasource. Furthermore, from this article, we learned how and when we use the JDBC datasource.
Recommended Articles
This is a guide to JDBC datasource. Here we discuss the different types of datasource with their basic syntax, and we also see different examples of JDBC datasource. You may also have a look at the following articles to learn more –