Pages

Best Database connector code for java and oracle.


package database;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Rajan Prasad Upadhyay
 */
public class OracleConnection {
    /* parameters */
    private String username=null;
    private String password=null;
    private String host=null;
    private String port=null;
    private String sid=null;
    
    private Connection con = null;
    
    
    
    /* Query execution */
    public void executeUpdate (String query) throws SQLException{
        if(con != null){
            PreparedStatement ps = con.prepareStatement(query);
            ps.executeUpdate();
        }
    }
    public ResultSet executeQuery (String query){
        ResultSet rs = null;
        if(con != null){
            try{
                PreparedStatement ps = con.prepareStatement(query);
                rs = ps.executeQuery();
            }catch(Exception e){
                
            }
        }
        return rs;
    }
    
    /* parameters part */
    public void setConnection (String conString){
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection(conString, username, password);
        } catch (Exception ex) {
            //return null;
            System.out.println("Connection could not be established");
            ex.printStackTrace();
        }
    }
    public Connection getConnection() {
        return con;
    }
    public void setDefaultParameters(){
        username = "USER1";
        password = "oracle";
        host    =   "localhost";
        port    =   "1521";
        sid     =   "XE";
    }
    public String getConnectionString(){
        return "jdbc:oracle:thin:@"+host+":"+port+":"+sid;
    }
 
 
 
 /* main method for unit testing */
    public static void main(String [] args) throws SQLException{
        OracleConnection con = new OracleConnection();
        //con.setDefaultParameters();
        con.setHost("localhost");
        con.setPort("1521");
        con.setSid("XE");
        con.setUsername("USER1");
        con.setPassword("oracle");
        
        //System.out.println(con.getConnectionString()); return ;
       
        con.setConnection(con.getConnectionString());
        
        ResultSet rs = con.executeQuery("select sysdate from dual");
        
        if(rs != null){
            while(rs.next()){
                System.out.println("The date is " + rs.getString(1));
            }
        }
    }

    /* Getters and Setters */
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
    }
    public String getPort() {
        return port;
    }
    public void setPort(String port) {
        this.port = port;
    }
    public String getSid() {
        return sid;
    }
    public void setSid(String sid) {
        this.sid = sid;
    }
}


No comments:

Post a Comment

If you like to say anything (good/bad), Please do not hesitate...