package graphics;
//import connection.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.sql.*;
public class Connn extends JFrame{
private JTextField tfuser = new JTextField();
private JPasswordField tfpass = new JPasswordField();
private JTextField tfport = new JTextField();
private JTextField tfhost = new JTextField();
private JTextArea meld = new JTextArea();
public Connn(){
super("MySQL - Connector");
JPanel pan = new JPanel();
pan.setLayout(new GridBagLayout());
pan.setBackground(Color.white);
JLabel lab = new JLabel();
//lab.setLayout(gb);
lab.setOpaque(true);
lab.setBackground(Color.blue);
lab.setForeground(Color.red);
lab.setFont(new Font("Helvetica", Font.BOLD, 24));
lab.setText("MySQL - Connector Version 0.1");
pan.add(lab, new GridBagConstraints(0,0,1,1,0.0,0.5,
GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0), 1, 1));
//pan.add(lab);
JLabel lab5 = new JLabel();
//lab.setLayout(gb);
lab5.setOpaque(true);
lab5.setBackground(Color.blue);
lab5.setForeground(Color.black);
lab5.setFont(new Font("Helvetica", Font.PLAIN, 21));
lab5.setText("Schnittstelle zu MySQl - Datenbanken");
pan.add(lab5, new GridBagConstraints(0,1,1,0,0.0,0.5,
GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0),1,1));
JPanel pan2 = new JPanel();
pan2.setLayout(new GridBagLayout());
//pan.setBackground(Color.green);
JLabel lab2 = new JLabel();
//lab2.setLayout(gb);
lab2.setOpaque(true);
lab2.setForeground(Color.black);
lab2.setFont(new Font("Helvetica", Font.PLAIN, 18));
lab2.setText("Username: ");
pan2.add(lab2, new GridBagConstraints(0,0,1,1,1.0,1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,0,0,0), 1, 1));
//pan2.add(lab2);
tfuser.setFont(new Font("Helvetica", Font.BOLD, 18));
//String user = tfuser.getText();
pan2.add(tfuser, new GridBagConstraints(1,0,1,1,1.0,1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,0,0,0),1,1));
JLabel lab3 = new JLabel();
//lab3.setLayout(gb);
lab3.setOpaque(true);
lab3.setForeground(Color.black);
lab3.setFont(new Font("Helvetica", Font.PLAIN, 18));
lab3.setText("Passwort: ");
pan2.add(lab3, new GridBagConstraints(0,1,1,1,1.0,1.0,
GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0,0,0,0),1,1));
//pan2.add(lab3);
tfpass.setFont(new Font("Helvetica", Font.BOLD, 18));
//String password = tfpass.getText();
pan2.add(tfpass, new GridBagConstraints(1,1,1,1,1.0,1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,0,0,0),1,1));
JLabel lab4 = new JLabel();
lab4.setOpaque(true);
lab4.setText("DB - Host: ");
lab4.setFont(new Font("Helvetica", Font.PLAIN, 18));
pan2.add(lab4, new GridBagConstraints(0,2,1,1,1.0,1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,0,0,0), 1,1));
tfhost.setFont(new Font("Helvetica", Font.BOLD, 18));
//String host = tfhost.getText();
pan2.add(tfhost, new GridBagConstraints(1,2,1,1,1.0,1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,0,0,0),1,1));
JLabel lab6 = new JLabel();
lab6.setOpaque(true);
lab6.setText("DB - Port: ");
lab6.setFont(new Font("Helvetica", Font.PLAIN, 18));
pan2.add(lab6, new GridBagConstraints(0,3,1,1,1.0,1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,0,0,0),1,1));
tfport.setFont(new Font("Helvetica", Font.BOLD, 18));
pan2.add(tfport, new GridBagConstraints(1,3,1,1,1.0,1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,0,0,0),1,1));
JButton but = new JButton();
but.setText("Verbinden");
but.setBackground(Color.green);
but.addActionListener(AL);
pan2.add(but, new GridBagConstraints(0,4,1,1,1.0,1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0),1,1));
JButton but1 = new JButton();
but1.setText("Verbindung beenden");
but1.setBackground(Color.red);
//ActionListener but = new AL();
pan2.add(but1, new GridBagConstraints(1,4,1,1,1.0,1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0),1,1));
JPanel pan3 = new JPanel();
pan3.setLayout(new GridBagLayout());
//JTextField meld = new JTextField();
meld.setFont(new Font("Helvetica", Font.BOLD, 12));
JScrollPane scroll = new JScrollPane(meld);
pan3.add(scroll, new GridBagConstraints(0,0,1,1,1.0,1.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,0,0,0),1,1));
//pan3.add(new JScrollPane(meld));
//pan3.add(meld );/*, new GridBagConstraints(0,0,1,1,1.0,1.0,
//GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,0,0,0),1,1));*/
this.getContentPane().setLayout(new GridBagLayout());
this.getContentPane().add(pan, new GridBagConstraints(0,0,1,1,1.0,1.0,
GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0,0,0,0),1,1));
this.getContentPane().add(pan2, new GridBagConstraints(0,1,1,1,1.0,1.0,
GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0,0,0,0),1,1));
this.getContentPane().add(pan3, new GridBagConstraints(0,2,1,1,1.0,1.0,
GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0,0,0,0),1,1));
}
ActionListener AL = new ActionListener(){
public void actionPerformed(ActionEvent e){
//Connection conn = null;
meld.append("\n"+"Button gedrückt");
/* try{
meld.append("\n"+"Treiber laden");
Class.forName("com.mysql.jdbc.Driver");//.newInstance();
//Class.forName("org.gjt.mm.mysql.Driver");
meld.append("Treiber wird geladen");
}
catch(ClassNotFoundException cnfs){
meld.append("\n" + "Class not Found");
}
try{
String connstr = "jdbc:mysql://localhost/mysql";
meld.append("\n" + connstr);
conn = DriverManager.getConnection(connstr, "akriege", "sierra");
//conn = DriverManager.getConnection("jdbc:mysql://"+ tfhost.getText() + ":"+ tfport.getText() +"/shanor", tfuser.getText(), tfpass.getText());
meld.append("\n" + "Verbindungsaufbau");}
catch(SQLException sqle){
meld.append("\n" + "Verbindung fehlgeschlagen: " + sqle.getMessage());}*/
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("OK");
}
catch (Exception ex){
System.out.println(ex);
};
}
Connection getconnection(){
Connection conn = null;
try{
String connstr = "jdbc:mysql://localhost/mysql";
conn = DriverManager.getConnection(connstr, "root", "");
}
catch (SQLException ex){
System.out.println("SQLException: "+ex.getMessage());
System.out.println("SQLState: "+ex.getSQLState());
System.out.println("VendorError: "+ex.getErrorCode());
System.exit(0);
};
return conn;
}
void execstmt(Connection conn, Statement stmt, String select){
ResultSet rs = null;
ResultSetMetaData rsmeta = null;
int i = 0;
try{
stmt = conn.createStatement();
System.out.println(stmt);
if (stmt.execute(select)){
rs = stmt.getResultSet();
};
try{
if (rs != null){
rsmeta = rs.getMetaData();
i = rsmeta.getColumnCount();
System.out.println("Anzahl Spalten: "+i);
for ( int ii=1; ii<=i; ii++){
System.out.println(
rsmeta.getColumnLabel(ii)+" "+rsmeta.getColumnName(ii)+" "+rsmeta.getColumnType(ii)+" "+rsmeta.getColumnTypeName(ii));
};
};
System.out.println("1");
System.out.println("2");
while (rs.next()){
String row = "";
for ( int ii=1; ii<=i; ii++){
row = row+ " "+rs.getString(ii);
};
System.out.println(row);
};
}
catch (SQLException ex){
System.out.println("SQLException: "+ex.getMessage());
System.out.println("SQLState: "+ex.getSQLState());
System.out.println("VendorError: "+ex.getErrorCode());
System.exit(0);
}
finally{
if (rs != null){
try{rs.close();}
catch (SQLException sqlEx){System.out.println(sqlEx);};
rs = null;
};
};
}
catch (SQLException ex){
System.out.println("SQLException: "+ex.getMessage());
System.out.println("SQLState: "+ex.getSQLState());
System.out.println("VendorError: "+ex.getErrorCode());
System.exit(0);
}
finally{
if (stmt != null){
try{stmt.close();}
catch (SQLException sqlEx){System.out.println(sqlEx);};
stmt = null;
};
};
};}; };