Last active
December 14, 2015 00:19
-
-
Save haiderfaraz/4998547 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//STEP 1. Import required packages | |
import java.sql.*; | |
public class JDBCTest { | |
// JDBC driver name and database URL | |
static final String JDBC_DRIVER = "com.octetstring.jdbcLdap.sql.JdbcLdapDriver"; | |
static final String DB_URL = "jdbc:ldap://192.168.56.101:389/dc=dsu,dc=edu,dc=pk"; | |
// Database credentials | |
static final String USER = "cn=Admin,ou=people,dc=dsu,dc=edu,dc=pk"; | |
static final String PASS = "********"; | |
public static void main(String[] args) { | |
Connection conn = null; | |
Statement stmt = null; | |
try{ | |
//STEP 2: Register JDBC driver | |
Class.forName(JDBC_DRIVER); | |
//STEP 3: Open a connection | |
System.out.println("Connecting to database..."); | |
conn = DriverManager.getConnection(DB_URL,USER,PASS); | |
//STEP 4: Execute a query | |
System.out.println("Creating statement..."); | |
stmt = conn.createStatement(); | |
String sql; | |
sql = "SELECT mail,DN FROM objectScope;cn=Admin,ou=people,dc=dsu,dc=edu,dc=pk"; | |
ResultSet rs = stmt.executeQuery(sql); | |
//STEP 5: Extract data from result set | |
rs.next(); | |
String mail = rs.getString("mail"); | |
System.out.println(mail); | |
rs.close(); | |
stmt.close(); | |
conn.close(); | |
}catch(SQLException se){ | |
//Handle errors for JDBC | |
se.printStackTrace(); | |
}catch(Exception e){ | |
//Handle errors for Class.forName | |
e.printStackTrace(); | |
}finally{ | |
//finally block used to close resources | |
try{ | |
if(stmt!=null) | |
stmt.close(); | |
}catch(SQLException se2){ | |
}// nothing we can do | |
try{ | |
if(conn!=null) | |
conn.close(); | |
}catch(SQLException se){ | |
se.printStackTrace(); | |
}//end finally try | |
}//end try | |
System.out.println("Goodbye!"); | |
}//end main | |
}//end FirstExample |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment