javadb/org/homelinux/largo/sql/client/DBClient.java
changeset 135 f837cf975e95
parent 123 07b2c0b991af
equal deleted inserted replaced
134:8325a0fc22cd 135:f837cf975e95
       
     1 /**
       
     2  * Simple Database Client for Java
       
     3  *
       
     4  */
       
     5 
     1 package org.homelinux.largo.sql.client;
     6 package org.homelinux.largo.sql.client;
       
     7 
       
     8 import java.util.List;
       
     9 import java.util.ArrayList;
     2 
    10 
     3 import java.sql.Connection;
    11 import java.sql.Connection;
     4 import java.sql.DriverManager;
    12 import java.sql.DriverManager;
       
    13 import java.sql.DatabaseMetaData;
     5 import java.sql.ResultSet;
    14 import java.sql.ResultSet;
     6 import java.sql.Statement;
    15 import java.sql.Statement;
       
    16 import java.sql.SQLException;
     7 
    17 
     8 import javax.naming.Context;
    18 import javax.naming.Context;
       
    19 import javax.naming.NamingException;
     9 import javax.naming.InitialContext;
    20 import javax.naming.InitialContext;
    10 import javax.sql.DataSource;
    21 import javax.sql.DataSource;
    11 
    22 
    12 public class DBClient {
    23 public class DBClient {
    13 	private Connection connection;
    24 	private Connection connection;
       
    25 	private DatabaseMetaData metadata;
    14 	private Statement statement;
    26 	private Statement statement;
    15 	private ResultSet resultset;
    27 	private ResultSet resultset;
    16 
    28 
    17 	private String driver, username, password;
    29 	private String driver, username, password;
    18 	private String dbURL;
    30 	private String dbURL;
    19 
    31 
    20 	/**
    32 	/**
    21 	 * Default Konstruktor to connect to my database
    33 	 * Constructor to connect to a database with a specific driver
       
    34 	 * and connection string
    22 	 */
    35 	 */
    23 	public DBClient () throws Exception {
    36 	public DBClient (String driver, String dbURL, String username, String password) throws ClassNotFoundException, SQLException {
    24 		this.driver = "org.apache.derby.jdbc.EmbeddedDriver";
    37 		this.driver = driver;
    25 		this.dbURL = "jdbc:derby:clubstatistik";
    38 		this.dbURL = dbURL;
    26 
       
    27 		this.username = null;
       
    28 		this.password = null;
       
    29 
    39 
    30 		Class.forName (driver);
    40 		Class.forName (driver);
    31 		connection = DriverManager.getConnection (dbURL, username, password);
    41 		connect (username, password);
    32 		statement = connection.createStatement ();
       
    33 	}
    42 	}
    34 
    43 
    35 	/**
    44 	/**
    36 	 * Default Konstruktor to connect to my DataPool
    45 	 * Constructor to connect to a database with a specific driver
       
    46 	 * and connection string
    37 	 */
    47 	 */
    38 	public DBClient (String jndiResource) throws Exception {
    48 	public DBClient (String driver, String dbURL) throws ClassNotFoundException, SQLException {
       
    49 		this (driver, dbURL, null, null);
       
    50 	}
       
    51 
       
    52 	/**
       
    53 	 * Default Constructor to connect to my database
       
    54 	 */
       
    55 	public DBClient () throws ClassNotFoundException, SQLException {
       
    56 		this ("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:clubstatistik", null, null);
       
    57 	}
       
    58 
       
    59 	/**
       
    60 	 * Constructor to connect to a DataSource on an AppServer
       
    61 	 */
       
    62 	public DBClient (String jndiResource) throws NamingException, SQLException {
    39 		Context ctx = new InitialContext ();
    63 		Context ctx = new InitialContext ();
    40 		DataSource ds = (DataSource) ctx.lookup (jndiResource);
    64 		DataSource ds = (DataSource) ctx.lookup (jndiResource);
    41 
    65 
    42 		connection = ds.getConnection ();
    66 		connection = ds.getConnection ();
    43 		statement = connection.createStatement ();
    67 		statement = connection.createStatement ();
    44 	}
    68 	}
    45 
    69 
    46 	/**
    70         protected void connect(String user, String pass) throws SQLException {
    47 	 * Default Konstruktor to connect to my database with auth-data
    71 		assert (connection == null) : "CONNECT: Already connected";
    48 	 */
       
    49 	public DBClient (String driver, String dbURL, String username, String password) throws Exception {
       
    50 		this.driver = driver;
       
    51 		this.dbURL = dbURL;
       
    52 
    72 
    53 		this.username = username;
    73 		this.username = user;
    54 		this.password = password;
    74 		this.password = pass;
    55 
    75 
    56 		Class.forName (driver);
       
    57 		connection = DriverManager.getConnection (dbURL, username, password);
    76 		connection = DriverManager.getConnection (dbURL, username, password);
       
    77 		metadata = connection.getMetaData();
    58 		statement = connection.createStatement ();
    78 		statement = connection.createStatement ();
    59 	}
    79 	}
    60 
    80 
    61 	public void close () throws Exception {
    81 	public void disconnect () throws SQLException {
    62 		connection.close ();
    82 		if (connection == null)
       
    83 			throw new SQLException ("DISCONNECT: Not connected");
       
    84 		else
       
    85 			connection.close ();
    63 	}
    86 	}
    64 
    87 
    65 	public String getColumn (int pos) throws Exception {
    88 	public String getColumn (int pos) throws SQLException {
    66 		return resultset.getString (pos);
    89 		return resultset.getString (pos);
    67 	}
    90 	}
    68 
    91 
    69 	public String getColumn (String name) throws Exception {
    92 	public String getColumn (String name) throws SQLException {
    70 		return resultset.getString (name);
    93 		return resultset.getString (name);
       
    94 	}
       
    95 
       
    96 	public List<String> getTables() throws SQLException {
       
    97 		List<String> list = new ArrayList<String>();
       
    98 		resultset = metadata.getTables(null, null, "%", null);
       
    99 
       
   100 		while (resultset.next()) {
       
   101 			list.add(resultset.getString("TABLE_NAME"));
       
   102 		}
       
   103 
       
   104 		return list;
       
   105 	}
       
   106 
       
   107 	public List<String> getColumns(String table) throws SQLException {
       
   108 		List<String> list = new ArrayList<String>();
       
   109 		resultset = metadata.getColumns(null, null, table, null);
       
   110 
       
   111 		while (resultset.next()) {
       
   112 			list.add(resultset.getString("COLUMN_NAME"));
       
   113 		}
       
   114 
       
   115 		return list;
    71 	}
   116 	}
    72 
   117 
    73 	public Connection getConnection () {
   118 	public Connection getConnection () {
    74 		return connection;
   119 		return connection;
    75 	}
   120 	}
    88 
   133 
    89 	public String getUsername () {
   134 	public String getUsername () {
    90 		return username;
   135 		return username;
    91 	}
   136 	}
    92 
   137 
    93 	public boolean hasNext () throws Exception {
   138 	public boolean hasNext () throws SQLException {
    94 		return resultset.next ();
   139 		return resultset.next ();
    95 	}
   140 	}
    96 
   141 
    97 	public void query (String query) throws Exception {
   142 	public void query (String query) throws SQLException {
    98 		resultset = statement.executeQuery (query);
   143 		resultset = statement.executeQuery (query);
       
   144 	}
       
   145 
       
   146 	/**
       
   147          * returns the number of updated rows
       
   148          */
       
   149 	public int update (String update) throws SQLException {
       
   150 		return statement.executeUpdate (update);
    99 	}
   151 	}
   100 
   152 
   101 	public void setConnection (Connection connection) {
   153 	public void setConnection (Connection connection) {
   102 		this.connection = connection;
   154 		this.connection = connection;
   103 	}
   155 	}