To get JDBC connection, normally there are two ways: 1. database driver manager 2. use data source
1. Using Database Driver Manager
The first approach includes the following steps
1. Register the database driver
2. Using DriverManager to get a connection to the database
3. query/update database
3. close other resources and close the connection
To get connection to different databases, the values will be different:
MySQL
1 2 3 4 |
Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://hostname:port/database_name","username", "password"); |
Oracle
1 2 3 4 |
Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@hostname:port:database_name","username","password"); |
PostgresSQL
1 2 3 4 |
Class.forName("org.postgresql.Driver"); Connection conn = DriverManager.getConnection("jdbc:postgresql://hostname:port/database_name","username", "password"); |
DB2
1 2 3 4 |
Class.forName("COM.ibm.db2.jdbc.net.DB2Driver"); Connection conn = DriverManager.getConnection("jdbc:db2:hostname:port/database_name","username", "password"); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package net.tecbar.examples; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class JdbcConnectionExample { public static void main(String args[]){ Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tecbar","tcdeploy", "12345678"); System.out.println("got the connection"); } catch (ClassNotFoundException e) { // process your exception here e.printStackTrace(); // use logging instead in real case } catch (SQLException e) { // process your exception here e.printStackTrace(); // use logging instead in real case } finally { if(conn != null) { try { conn.close(); } catch (SQLException e) { // process your exception here e.printStackTrace(); // use logging instead in real case } } } } } |
2. Using DataSource
If JNDI and the DataSource are available, DataSource interface shall be used to get a Connection. Normally, an application server or a servlet container can support JNDI, DataSource configuration and connection pool support.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
package net.tecbar.jdbc.examples; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public class JdbcConnectionExample2 { // used when JNDI supported environment public void getDataSourceConnection(String args[]) { Connection conn = null; try { // you can reorganize the code and reuse initialized datasource // object InitialContext ctx = new InitialContext(); DataSource datasource = (DataSource) ctx .lookup("java:comp/env/jdbc/tecbar"); conn = datasource.getConnection(); System.out.println("got the connection"); } catch (NamingException ne) { // process your exception here ne.printStackTrace(); // use logging instead in real case } catch (SQLException e) { // process your exception here e.printStackTrace(); // use logging instead in real case } finally { release(conn, null, null); } } public void release(Connection conn, Statement stmt, ResultSet rs) { try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException ex) { // process your exception here } } } |
Pingback: JDBC by Examples
Pingback: JDBC by Examples