Vous êtes sur la page 1sur 5

How to maintain the DBConnection separate and accessor fetch the data from the Tables

using JDBC Connection.

Maintain Dbconnection separate


/*
* Created on May 29, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package app;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* @author murali
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class DBConnection {

private static Connection c;

public static Connection connect() throws Exception {


Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@personal:1521:orcl";
c = DriverManager.getConnection(url, "system", "password");
return c;

public static int Update(String url)throws Exception{

if(c==null)
connect();
return c.createStatement().executeUpdate(url);
}
public static ResultSet Query(String url)throws Exception{
if(c==null)
connect();
return c.createStatement().executeQuery(url);

/**
* @param stmt
* @param rs
* @param connection
*/
public static void close(Statement stmt, ResultSet rs, Connection connection) {
// TODO Auto-generated method stub

}
Table creation :

create table ADDRESS(


ADDRESS_SEQ NUMBER(11) not null,
FIRST_NAME VARCHAR2(100) not null,
MI VARCHAR2(100),
LAST_NAME VARCHAR2(100) not null,
STREET VARCHAR2(100) not null,
CITY VARCHAR2(100) not null,
STATE VARCHAR2(100) not null)

Java class to fetch the data


/*
* Created on May 29, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package app;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

/**
* @author murali
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class GetNameDetails {

Connection connection = null;


Statement stmt = null;
ResultSet rs = null;
Statement stmt1 = null;
ResultSet rs1 = null;

/**
* To retrieve the func group,func name
*
* @return @throws
* Exception
*/
public HashMap getNameDetails() throws Exception {
HashMap data = new HashMap();
HashMap nameDet = new HashMap();
String query = null;
try {
connection = DBConnection.connect();
data = new HashMap();
query = "SELECT FIRST_NAME,LAST_NAME FROM
ADDRESS";
;
stmt = connection.createStatement();
rs = stmt.executeQuery(query);
while (rs.next()) {
nameDet.put(rs.getString("FIRST_NAME"), rs
.getString("LAST_NAME"));
}
data.put("NAME DETAILS", nameDet);
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.close(stmt, rs, connection);
}
return data;
}

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


HashMap data1 = new HashMap();
HashMap fetchDetails = new HashMap();
GetNameDetails getNameDetails = new GetNameDetails();
data1=getNameDetails.getNameDetails();
fetchDetails=getNameDetails.fetchDetails();

System.out.println(data1);
System.out.println("\n");
System.out.println(fetchDetails);
}

/**
* To retrieve the func group,func name from WF_FUNC_GROUP_MST
* @return @throws
* Exception
*/
public HashMap fetchDetails() throws Exception {
HashMap data = new HashMap();
HashMap details = new HashMap();
ArrayList arrayList = new ArrayList();
String query1 = null;

try {
connection = DBConnection.connect();
data = new HashMap();
query1 = "SELECT
FIRST_NAME,MI,LAST_NAME,STREET,CITY,STATE FROM ADDRESS";
stmt1 = connection.createStatement();
rs1 = stmt1.executeQuery(query1);
while (rs1.next()) {

details.put(rs1.getString("FIRST_NAME"),
rs1.getString("MI")
// rs1.getString("LAST_NAME"),
// rs1.getString("STREET"),
// rs1.getString("CITY"),
// rs1.getString("STATE")
);
}
data.put("DETAILS", details);
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.close(stmt1,rs1,connection);
}
return data;
}

Vous aimerez peut-être aussi