jep benutze eclips, wie kann ich denn den datatabase explorer nutzen?
Seh grad dass das ein myeclipse feature ist..
Hast du den Oracle Driver im Classpath?
Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
jep benutze eclips, wie kann ich denn den datatabase explorer nutzen?
logger.info("Versuche, Verbindung aufzubauen...");
try
{
OracleDataSource ds;
ds = new OracleDataSource();
ds.setURL("jdbc:oracle:thin:@localhost");
Connection conn = ds.getConnection("hr", "hr");
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM Employees ORDER BY employee_id";
logger.info("\nExecuting query: " + sql);
ResultSet rSet = stmt.executeQuery(sql);
while (rSet.next()) {
logger.info(rSet.getInt(1) + " " +
rSet.getString(2) + " " +
rSet.getString(3) + " " +
rSet.getString(4));
}
rSet.close();
stmt.close();
conn.close();
}
catch(SQLException e)
{
logger.error("Konnte keine Verbindung herstellen.", e);
}
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import oracle.jdbc.driver.OracleTypes;
/****************************************************************************
* Database class
*
***************************************************************************/
public final class Database {
/************************************************************************
* Private Class. The only instance of Database is created by 'Holder'
* on loading.
***********************************************************************/
private static class Holder {
private static final Database INSTANCE = new Database();
}//HOLDER
//declarations
private String dbUser,
dbPassword,
dbUrl,
dbDriverClass;
private Connection con;
private boolean isConnected;
private Statement stmt;
/************************************************************************
* Constructor
*
***********************************************************************/
private Database() {
Settings conf = Settings.getInstance();
conf.readSettings();
//get DB-settings
Vector dbsettings = conf.getDBSettings();
//set db-values
this.dbUser = (String) dbsettings.elementAt(0);
this.dbPassword = (String) dbsettings.elementAt(1);
this.dbUrl = (String) dbsettings.elementAt(2);
this.dbDriverClass = (String) dbsettings.elementAt(3);
connect();
}//Database()
/************************************************************************
* Static methode 'getInstance()'. Return the only instance of 'Database'
* Implements the Singleton-Design-Pattern.
***********************************************************************/
public static Database getInstance() {
return Holder.INSTANCE;
}//getInstance()
/************************************************************************
* connect to database.
*
***********************************************************************/
private void connect(){
isConnected = true;
//load driver class into java runtime
try {
Class.forName(dbDriverClass);
} catch (ClassNotFoundException cnfe) {
System.err.println("Could not find the Oracle driver. " +
"Check your CLASSPATH. " +cnfe.getMessage());
System.exit(2);
}
//connect to database
try {
con = DriverManager.getConnection(dbUrl,dbUser,dbPassword);
stmt = con.createStatement();
} catch (SQLException sqle) {
System.err.println("Could not connect to database: " +
sqle.getMessage());
sqle.printStackTrace();
//System.exit(3);
}
}//connect()
/************************************************************************
* close database-connection
*
***********************************************************************/
public void close(){
try {
con.close();
isConnected = false;
} catch(SQLException sqle){};
}//close()
/************************************************************************
* returns the state of database-connection
*
***********************************************************************/
public boolean isConnected(){
return isConnected;
}//isConnected()
/************************************************************************
* method to execute SQL-statements
* @param String sqlStr
* @param boolean isUpdt
* @return Resultset rs
***********************************************************************/
private ResultSet execSQL(String sqlStr, boolean isUpdt) {
ResultSet rs = null;
//DB-connection established?
if (!isConnected()) {
System.err.println("Database connection is closed.\n");
return rs;
}
try {
//update- or other SQL-statement
if (isUpdt) {
//stmt.execute(sqlStr);
stmt.executeUpdate(sqlStr);
} else {
System.out.println("DB Line 170: "+sqlStr);
rs = stmt.executeQuery(sqlStr);
}
} catch (SQLException sqle) {
System.err.println("Check your SQL-Statement_DB174: ");
sqle.printStackTrace();
}
return rs;
}//execSQL()
/************************************************************************
* execute SELECT statement
* @param Strin sqlStr
***********************************************************************/
public ResultSet getResult(String sqlStr) {
return execSQL(sqlStr,false);
}//getResult()
/************************************************************************
* execute INSERT or UPDATE statement
* @param String sqlStr
***********************************************************************/
public void setCommand(String sqlStr) {
execSQL(sqlStr,true);
}//setCommand()
//=======================================================================
// help-methods
//=======================================================================
/************************************************************************
* help-method to test class and see if settings were imported correctly
*
***********************************************************************/
public void printInfos(){
System.out.println("Driverclass:\t" +dbDriverClass);
System.out.println("User:\t\t" +dbUser);
System.out.println("PWD:\t\t" +dbPassword);
System.out.println("URL:\t\t" +dbDriverClass);
}//printInfos()
}//class
wenn ich dein programm so mal ausfühe bekomme ich ein Fehler bei der Zeile mit dem
Settings
Code:Settings conf = Settings.getInstance();
import java.util.ArrayList;
import java.util.Vector;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
public final class Settings {
/************************************************************************
* Private Class. The only instance of Database is created by 'Holder'
* on loading.
***********************************************************************/
private static class Holder {
private static final Settings INSTANCE = new Settings();
}//HOLDER
//Declarations
private static final String CONF_FILE = "inc/settings";
//Database
private String dbHost,
dbPort,
dbUrl,
dbUser,
dbPassword,
dbDriverClass,
dbOraSid;
private boolean dbIsThinDriver;
//Settings read?
private boolean readStatus;
/************************************************************************
* Constructor.
*
***********************************************************************/
private Settings(){
this.readStatus = false;
}//Settings
/************************************************************************
* Static methode 'getInstance()'. Return the only instance of 'Settings'
* Implements the Singleton-Design-Pattern.
***********************************************************************/
public static Settings getInstance() {
return Holder.INSTANCE;
}//getInstance()
/************************************************************************
* Opens file and read settings. All informations are stored in local
* private values which can be read with getter - methods.
***********************************************************************/
public void readSettings(){
//settings already read ?
if(!readStatus){
PropertiesConfiguration cfg = null;
//read properties
try {
cfg = new PropertiesConfiguration(CONF_FILE);
} catch (ConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//set db-values
this.dbHost = (String) cfg.getProperty("DB.Host");
this.dbPort = (String) cfg.getProperty("DB.Port");
this.dbDriverClass = (String) cfg.getProperty("DB.DriverClass");
this.dbOraSid = (String) cfg.getProperty("DB.OraSID");
this.dbUser = (String) cfg.getProperty("DB.User");
this.dbPassword = (String) cfg.getProperty("DB.Password");
//build db-connection URL
dbIsThinDriver = cfg.getProperty("DB.DriverType").equals("thin");
dbUrl = "jdbc:oracle:" + (dbIsThinDriver ? "thin:@" +
dbHost + ":" + dbPort + ":" +
dbOraSid : "oci8:@" + dbOraSid);
readStatus = true;
}//if
}//readSettings()
/************************************************************************
* Changes settingsvalues in property-file.
* @param String name
* @param String value
* @return boolean changed
***********************************************************************/
public boolean changeSetting(String name, String value){
boolean changed = true;
PropertiesConfiguration cfg = null;
//read properties
try {
cfg = new PropertiesConfiguration(CONF_FILE);
} catch (ConfigurationException e) {
e.printStackTrace();
}
//change property
cfg.setProperty(name, value);
try {
//change property value in file
cfg.save();
} catch (ConfigurationException e) {
e.printStackTrace();
changed = false;
}
//initiate to read the settings again to change the values of the
//running application
readStatus = false;
readSettings();
return changed;
}//changeSetting()
/************************************************************************
* Returns the DB-Settings (user, password, url, driverclass) as vector.
* @return Vector dbsettings
***********************************************************************/
public Vector getDBSettings(){
Vector<String> dbSettings = new Vector<String>();
//Fill vector with elements
dbSettings.addElement(dbUser);
dbSettings.addElement(dbPassword);
dbSettings.addElement(dbUrl);
dbSettings.addElement(dbDriverClass);
return dbSettings;
}//getDBSettings()
//=======================================================================
// help-methods
//=======================================================================
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
/************************************************************************
* Help-Method to test if the settings are read correctly.
***********************************************************************/
public void printAll(){
System.out.println("Host: " + dbHost);
System.out.println("Port: " + dbPort);
System.out.println("DC: " + dbDriverClass);
System.out.println("User: " + dbUser);
System.out.println("URL: " + dbUrl);
System.out.println("PWD: " + dbPassword +"\n");
}//printAll()
}//CLASS
#############################################################################
#
# Configuration file for xyz.
#
# (c) Copyright 2007 by Ronin-Jay
#
#############################################################################
#________________________________Database____________________________________
# Enter the name of the machine hosting the database server
DB.Host = localhost
DB.Port = 1521
# Enter the driverclass for the databaseconnection
# e.g. oracle.jdbc.OracleDriver
DB.DriverClass = oracle.jdbc.OracleDriver
# Enter the name of the database instance you are running
DB.OraSID = xe
# DB.DriverType is < thin | oci8 >
DB.DriverType = thin
# DB.User
DB.User = system
# DB.Password
DB.Password = admin