javadb/org/homelinux/largo/sql/client/DBClient.java
changeset 100 a6d2add085d7
child 105 c19e37122deb
equal deleted inserted replaced
99:d99503984cd5 100:a6d2add085d7
       
     1 package org.homelinux.largo.sql.client;
       
     2 
       
     3 import java.sql.Connection;
       
     4 import java.sql.DriverManager;
       
     5 import java.sql.ResultSet;
       
     6 import java.sql.Statement;
       
     7 
       
     8 import javax.naming.Context;
       
     9 import javax.naming.InitialContext;
       
    10 import javax.sql.DataSource;
       
    11 
       
    12 public class DBClient {
       
    13 	private Connection connection;
       
    14 	private Statement statement;
       
    15 	private ResultSet resultset;
       
    16 
       
    17 	private String driver, username, password;
       
    18 	private String dbURL;
       
    19 
       
    20 	/**
       
    21 	 * Default Konstruktor to connect to my database
       
    22 	 */
       
    23 	public DBClient () throws Exception {
       
    24 		this.driver = "org.apache.derby.jdbc.EmbeddedDriver";
       
    25 		this.dbURL = "jdbc:derby://localhost/clubstatistik";
       
    26 
       
    27 		this.username = null;
       
    28 		this.password = null;
       
    29 
       
    30 		Class.forName (driver);
       
    31 		connection = DriverManager.getConnection (dbURL, username, password);
       
    32 		statement = connection.createStatement ();
       
    33 	}
       
    34 
       
    35 	/**
       
    36 	 * Default Konstruktor to connect to my DataPool
       
    37 	 */ 
       
    38 	public DBClient (String jndiResource) throws Exception {
       
    39 		Context ctx = new InitialContext ();
       
    40 		DataSource ds = (DataSource) ctx.lookup (jndiResource);
       
    41 
       
    42 		connection = ds.getConnection ();
       
    43 		statement = connection.createStatement ();
       
    44 	}
       
    45 
       
    46 	/**
       
    47 	 * Default Konstruktor to connect to my database with auth-data
       
    48 	 */ 	 
       
    49 	public DBClient (String driver, String dbURL, String username, String password) throws Exception {
       
    50 		this.driver = driver;
       
    51 		this.dbURL = dbURL;
       
    52 
       
    53 		this.username = username;
       
    54 		this.password = password;
       
    55 
       
    56 		Class.forName (driver);
       
    57 		connection = DriverManager.getConnection (dbURL, username, password);
       
    58 		statement = connection.createStatement ();
       
    59 	} 
       
    60 
       
    61 	public void close () throws Exception {
       
    62 		connection.close ();
       
    63 	} 
       
    64 
       
    65 	public String getColumn (int pos) throws Exception {
       
    66 		return resultset.getString (pos);
       
    67 	} 
       
    68 
       
    69 	public String getColumn (String name) throws Exception {
       
    70 		return resultset.getString (name);
       
    71 	} 
       
    72 
       
    73 	public Connection getConnection () {
       
    74 		return connection;
       
    75 	} 
       
    76 
       
    77 	public String getDbURL () {
       
    78 		return dbURL;
       
    79 	}
       
    80 
       
    81 	public String getDriver () {
       
    82 		return driver;
       
    83 	}
       
    84 
       
    85 	public String getPassword () {
       
    86 		return password;
       
    87 	}
       
    88 
       
    89 	public String getUsername () {
       
    90 		return username;
       
    91 	}
       
    92 
       
    93 	public boolean hasNext () throws Exception {
       
    94 		return resultset.next ();
       
    95 	}
       
    96 
       
    97 	public void query (String query) throws Exception {
       
    98 		resultset = statement.executeQuery (query);
       
    99 	} 
       
   100 
       
   101 	public void setConnection (Connection connection) {
       
   102 		this.connection = connection;
       
   103 	}
       
   104 
       
   105 	public void setDbURL (String dbURL) {
       
   106 		this.dbURL = dbURL;
       
   107 	}
       
   108 
       
   109 	public void setDriver (String driver) {
       
   110 		this.driver = driver;
       
   111 	}
       
   112 
       
   113 	public void setPassword (String password) {
       
   114 		this.password = password;
       
   115 	}
       
   116 
       
   117 	public void setUsername (String username) {
       
   118 		this.username = username;
       
   119 	}
       
   120 }