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