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

JDBC Connectivity

The DriverManager class is used to connect to a database management system (DBMS) by calling the getConnection method, which establishes a connection using properties like username and password. The getConnection method checks the DBMS type and constructs the appropriate connection string for MySQL using hostname and port, or for Derby using just the database name and auto-creation flag.

Uploaded by

wallysh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
247 views1 page

JDBC Connectivity

The DriverManager class is used to connect to a database management system (DBMS) by calling the getConnection method, which establishes a connection using properties like username and password. The getConnection method checks the DBMS type and constructs the appropriate connection string for MySQL using hostname and port, or for Derby using just the database name and auto-creation flag.

Uploaded by

wallysh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

Using the DriverManager Class

Connecting to your DBMS with the DriverManager class involves calling the method DriverManager.getConnection. The following method, establishes a database connection:
public Connection getConnection() throws SQLException { Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", this.userName); connectionProps.put("password", this.password); if (this.dbms.equals("mysql")) { conn = DriverManager. getConnection("jdbc:" + this.dbms + "://" + this.serverName + ":" + this.portNumber + "/", connectionProps); } else if (this.dbms.equals("derby")) { conn = DriverManager. getConnection("jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps); } System.out.println("Connected to database"); return conn; }

You might also like