0% found this document useful (0 votes)
52 views1 page

Java Database Connection

This code connects to an H2 database using JDBC. It creates a BasicDataSource object to manage the connection pool, configures the driver class, URL, username and password. It then gets a connection from the data source, prints "Connected" if successful, and closes the data source to release resources.

Uploaded by

PasanNanayakkara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views1 page

Java Database Connection

This code connects to an H2 database using JDBC. It creates a BasicDataSource object to manage the connection pool, configures the driver class, URL, username and password. It then gets a connection from the data source, prints "Connected" if successful, and closes the data source to release resources.

Uploaded by

PasanNanayakkara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

public class Main {

public static void main(String[] args) throws SQLException {


BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("org.h2.Driver");
ds.setUrl("jdbc:h2:C:/Users/Pathum/Documents/My Java
Projects/JavaDB/target/db");
ds.setUsername("sa");
ds.setPassword("");
try (Connection connection = ds.getConnection()) {
System.out.println("Connected");
} finally {
ds.close();
}
/*try (Connection connection = DriverManager
.getConnection(
"jdbc:h2:C:/Users/Pathum/Documents/My Java
Projects/JavaDB/target/db",
"sa", "")) {
System.out.println("Connected");*/

You might also like