Hallo, ich hab hier mit etwas angefangen, komm aber nicht weiter... und zwar hab ich eine DBConnect Klasse mit der ich die Verbindung zu einer Datenbank herstelle. Nun möchte ich, das diese Klasse aufgerufen wird wenn ich auf einen Button der in einer anderen Klasse liegt klicke. Mir wurde gesagt das funktioniert mit einem Actionlistener, hab aber keine ahnung wie das Funktionieren soll.
Hier die 2 meiner meinung nach benötigten klassen, bin anfänger, und für jeden tipp dankbar.
Vielen Dank
Hier die 2 meiner meinung nach benötigten klassen, bin anfänger, und für jeden tipp dankbar.
Code:
import java.sql.*;
import java.util.*;
public class DBConnect {
DBServer dbServer = null;
Connection con = null;
String user = null;
String password = null;
String odbc = null;
public DBConnect(String user, String password, String odbc)
{
this.user = user;
this.password = password;
this.odbc = odbc;
open(user, password, odbc);
}
private void open(String user, String password, String odbc)
{
dbServer = new DBServer(this);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:" + odbc, user, password);
}
catch (SQLException ex)
{
con = null;
}
catch (ClassNotFoundException ex)
{
con = null;
}
}
public void open()
{
open(user, password, odbc);
}
public void close()
{
try
{
if (con != null && con.isClosed() == false)
{
con.close();
}
}
catch (SQLException ex)
{
}
}
public Hashtable getServer()
{
return dbServer.getServer(con);
}
/* try
{
if (con.isClosed())
{
open(user, password, odbc);
}
if (con != null && con.isClosed() == false)
{
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, name FROM server");
server se;
int id;
String name;
while (rs.next())
{
id = rs.getInt(1);
name = rs.getString(2);
//System.out.println(" id=" + id + " name=" + name );
se = new server(name, id);
serverHash.put(se.name, se);
}
}
}
catch (SQLException ex)
{
}
return serverHash;
}
public server getServer(int i)
{
server se = null;
try
{
if (con.isClosed())
{
open(user, password, odbc);
}
if (con != null && con.isClosed() == false)
{
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, name FROM server where id=" + i);
int id;
String name;
if (rs.next())
{
id = rs.getInt(1);
name = rs.getString(2);
se = new server(name, id);
}
}
}
catch (SQLException ex)
{
}
return se;
}
public Hashtable getServer(String n)
{
Hashtable serverHash = new Hashtable();
try
{
if (con.isClosed())
{
open(user, password, odbc);
}
if (con != null && con.isClosed() == false)
{
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, name FROM server WHERE name like '" + n + "'");
server se;
int id;
String name;
while (rs.next())
{
id = rs.getInt(1);
name = rs.getString(2);
//System.out.println(" id=" + id + " name=" + name );
se = new server(name, id);
serverHash.put(se.name, se);
}
}
}
catch (SQLException ex)
{
}
return serverHash;
}
*/
}
Code:
import java.awt.*;
import java.awt.event.*;
public class LogInGUI extends Frame {
TextField TPassword = new TextField (15);
TextField TUserID = new TextField();
Button BExit = new Button();
Button BConnect = new Button();
Choice ServerList;
Label LB;
class SymWindow extends java.awt.event.WindowAdapter {
public void windowClosing(java.awt.event.WindowEvent event) {
Object object = event.getSource();
if (object == LogInGUI.this)
LogInGUI_WindowClosing(event);
}
}
void LogInGUI_WindowClosing(java.awt.event.WindowEvent event)
{
}
public static void main (String [] args) {
LogInGUI ui = new LogInGUI();
ui.LogInGUI();}
public void LogInGUI()
{
// Layouteigenschaften des Fensters
this.setLayout(null);
this.setSize(300,200);
this.setVisible(true);
this.setTitle("LogIn");
/* ServerList = new Choice();
ServerList.setSize(260, 30);
ServerList.setLocation(130,80);
ServerList.setVisible(true);
this.add(ServerList); */
BConnect= new Button("Connect");
BConnect.setSize(60,30);
BConnect.setLocation(200,60);
this.add(BConnect);
BExit= new Button("Exit");
BExit.setSize(60,30);
BExit.setLocation(200,100);
this.add (BExit);
TUserID = new TextField("UserID");
TUserID.setSize(60, 20);
TUserID.setLocation(50, 80);
TUserID.setEditable(true);
TUserID.setBackground(Color.white);
TUserID.setForeground(Color.black);
this.add(TUserID);
TPassword = new TextField("Passwort");
TPassword.setSize(60, 20);
TPassword.setLocation(50, 100);
TPassword.setEditable(true);
TPassword.setBackground(Color.white);
TPassword.setForeground(Color.black);
TPassword.setEchoChar ('*');
this.add(TPassword);
SymWindow aSymWindow = new SymWindow();
this.addWindowListener(aSymWindow);
}
}