I usually create a txt file like the following (I used Oracle but you can 
change to mysql stuffs):

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.driverType=jdbc:oracle:thin:@
jdbc.host=prokids
jdbc.port=1521
jdbc.sid=orcl
jdbc.username=scott
jdbc.password=tiger


Then I have a manager class (such as CustomerManager or ProductManager,  
whatever) establhishing connection like the following:


cont...
  /************************************************************
   * Establish a connection with the database
   ************************************************************/
  public void connectDB() {
    try {
      Properties props = new Properties();
      FileInputStream dbPropFile = new 
FileInputStream("DBHomeProperties.txt");
      props.load(dbPropFile);
      this.dbConnection = new DBConnection(props.getProperty("jdbc.driver"),
      props.getProperty("jdbc.driverType"), props.getProperty("jdbc.host"),
      props.getProperty("jdbc.port"), props.getProperty("jdbc.sid"),
      props.getProperty("jdbc.username"), 
props.getProperty("jdbc.password"));

    } catch (Exception e) {
        System.out.println(e);
                }
  }


OR you can set it directly like:


      this.dbConnection = new DBConnection 
("oracle.jdbc.driver.OracleDriver",
        "jdbc:oracle:thin:@","prokids","1521", "orcl",
        "scott", "tiger");


And then in my DBConnection class I got the following:

/**
* Copyright (c) 2000 Objectry.com
* File Name:   DBConnection.java
* @date        October 2001
* @author      Siomara Pantarotto
* Description: Definition of DBConnection class.This class is
*              design to access Oracle database via drivers and
*              to acces SQL Server database via ODBC bridge.
**/

import java.io.*;
import java.sql.*;
import java.util.*;

public class DBConnection implements Serializable
{
  private Connection conn;
  private Statement stmt;
  private String driver;
  private String driverType;
  private String host;
  private String port;
  private String sid;
  private String username;
  private String password;

  
/////////////////////////////////////////////////////////////////////////////
  // Constructors
  //

  // No argument constructor for Java Bean requirement
  public DBConnection() {
  }
  // For accessing Oracle via drivers
  public DBConnection(String driver, String driverType,
                      String host, String port, String sid,
                      String username, String password) {
    setDriver(driver);
    setDriverType(driverType);
    setHost(host);
    setPort(port);
    setSid(sid);
    setUsername(username);
    setPassword(password);
    connect();
  }
  // For accessing SQL Server via ODBC
  public DBConnection(String driver, String driverType) {
    setDriver(driver);
    setDriverType(driverType);
    setHost("");
    setPort("");
    setSid("");
    setUsername("");
    setPassword("");
    connect();
  }

  
/////////////////////////////////////////////////////////////////////////////
  // Establish connection to the database
  //
  public void connect() {
    String url;
    url = getDriverType();

    if (getHost().length() > 0)     { url = url + getHost(); }
    if (getPort().length() > 0)     { url = url + ":" + getPort(); }
    if (getSid().length() > 0)      { url = url + ":" + getSid();  }
System.out.println(url);
    try {
                        Class.forName(driver);
                }
                catch(Exception e) {
                        System.err.print("ClassNotFoundException: ");
                        System.err.println(e.getMessage());
                }
    try {
                        this.conn = DriverManager.getConnection(url,username, 
password);
                        this.stmt = conn.createStatement();
                }
                catch(SQLException ex) {
                        System.err.println("SQLException: " + ex.getMessage());
                }
  }

  
/////////////////////////////////////////////////////////////////////////////
  // Disconnect from the database
  //
  public void disconnect() {
    try {
      this.stmt.close();
                        this.conn.close();
                }
                catch(SQLException ex) {
                        System.err.println("SQLException: " + ex.getMessage());
                }
  }

  
/////////////////////////////////////////////////////////////////////////////
  // Get connection
  //
  public Connection getConnection() {
          return conn;
  }

  
/////////////////////////////////////////////////////////////////////////////
  // Get statement
  //
  public Statement getStatement() {
          return stmt;
  }

  
/////////////////////////////////////////////////////////////////////////////
  // Set and get DRIVER attribute
  //
  public void setDriver(String newDriver) {
    this.driver = newDriver;
  }
  public String getDriver() {
    return driver;
  }

  
/////////////////////////////////////////////////////////////////////////////
  // Set and get DRIVER TYPE attribute
  //
  public void setDriverType(String newDriverType) {
    this.driverType = newDriverType;
  }
  public String getDriverType() {
    return driverType;
  }

  
/////////////////////////////////////////////////////////////////////////////
  // Set and get HOST attribute
  //
  public void setHost(String newHost) {
    this.host = newHost;
  }
  public String getHost() {
    return host;
  }

  
/////////////////////////////////////////////////////////////////////////////
  // Set and get PORT attribute
  //
  public void setPort(String newPort) {
    this.port = newPort;
  }
  public String getPort() {
    return port;
  }

  
/////////////////////////////////////////////////////////////////////////////
  // Set and get SID attribute
  //
  public void setSid(String newSid) {
    this.sid = newSid;
  }
  public String getSid() {
    return sid;
  }

  
/////////////////////////////////////////////////////////////////////////////
  // Set and get USERNAME attribute
  //
  public void setUsername(String newUsername) {
    this.username = newUsername;
  }
  public String getUsername() {
    return username;
  }

  
/////////////////////////////////////////////////////////////////////////////
  // Set and get PASSWORD attribute
  //
  public void setPassword(String newPassword) {
    this.password = newPassword;
  }
  public String getPassword() {
    return password;
  }
}

*******************************************************************

>From: "Cal Evans" <[EMAIL PROTECTED]>
>To: <[EMAIL PROTECTED]>, <[EMAIL PROTECTED]>
>CC: <[EMAIL PROTECTED]>
>Subject: RE: The Syntax to Get JDBC Connection
>Date: Sun, 20 May 2001 20:09:43 -0500
>
>If that's an exact quote then you left off the opening quote marks.
>
>Cal
>http://www.calevans.com
>
>
>-----Original Message-----
>From: Li Bing [mailto:[EMAIL PROTECTED]]
>Sent: Saturday, May 19, 2001 4:14 PM
>To: [EMAIL PROTECTED]
>Cc: [EMAIL PROTECTED]
>Subject: The Syntax to Get JDBC Connection
>
>
>Dear All,
>
>I use mm.mysql.jdbc to access mysql in Java. To get the connection, the
>following sentence is writen in my program. But an error occurred.
>I guess that the syntax is wrong. Could you please give me the correct
>one.
>
>Connection conn =
>DriverManager.getConnection(jdbc:mysql://localhost/RegistrationDB?user=libin
>g;password=123456");
>
>My requirements are as follows,
>
>I need to create a database, RegistrationDB, after getting connected
>with MySql. The username is libing and password iis 123456.
>
>Thanks!
>Li Bing
>
>
>---------------------------------------------------------------------
>Before posting, please check:
>    http://www.mysql.com/manual.php   (the manual)
>    http://lists.mysql.com/           (the list archive)
>
>To request this thread, e-mail <[EMAIL PROTECTED]>
>To unsubscribe, e-mail <[EMAIL PROTECTED]>
>Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
>
>
>
>---------------------------------------------------------------------
>Before posting, please check:
>    http://www.mysql.com/manual.php   (the manual)
>    http://lists.mysql.com/           (the list archive)
>
>To request this thread, e-mail <[EMAIL PROTECTED]>
>To unsubscribe, e-mail 
><[EMAIL PROTECTED]>
>Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
>

_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to