Ich hab die 2 untenstehenden Klassen Programmiert. Die Kompilierung läuft ohne Fehlermeldung. Wenn ich jedoch auf den Button "Benutzer anlegen " drücke bekomm ich eine NullPointerException für den Aufruf von Grant. Wieso?
P.S.: Ich dachte erst es liegt am Statement und hab daran "rumprobiert", deshalb sieht der Teil mom so aus. / Die Liste hab ich erst mit JList machen wollen, dort ging jedoch li.addItem(dkdkd); nicht, mit List gings dann, deshalb diese Abkehr von Swing in dem Punkt.
P.S.: Ich dachte erst es liegt am Statement und hab daran "rumprobiert", deshalb sieht der Teil mom so aus. / Die Liste hab ich erst mit JList machen wollen, dort ging jedoch li.addItem(dkdkd); nicht, mit List gings dann, deshalb diese Abkehr von Swing in dem Punkt.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.sql.*;
//Klasse für den Menupunkt "Benutzer anlegen"
public class User extends JFrame implements ActionListener {
JButton bu10,bu20;
private static JTextArea ausg = new JTextArea(3,30);
private JTextField tf1 = new JTextField();
private JTextField tf2 = new JTextField();
private JTextField tf3 = new JTextField();
private JTextField tf4 = new JTextField();
private List li = new List();
public static void Texte(String text){
User.ausg.append("\n"+text);
}
public User(){
super("Benutzer anlegen");
JPanel pa = new JPanel();
pa.setLayout(new GridBagLayout());
JLabel la = new JLabel();
la.setOpaque(true);
la.setFont(new Font("Helvetica", Font.BOLD, 20));
la.setText("Benutzer anlegen");
pa.add(la, new GridBagConstraints(0,0,1,1,1.0,0.1,
GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0),1,1));
JLabel la1 = new JLabel();
la1.setOpaque(true);
la1.setFont(new Font("Helvetica", Font.BOLD, 14));
la1.setText("Benutzername: ");
pa.add(la1, new GridBagConstraints(0,1,1,1,1.0,0.1,
GridBagConstraints.CENTER,GridBagConstraints.NONE, new Insets(0,0,0,0),1,1));
JLabel la2 = new JLabel();
la2.setOpaque(true);
la2.setFont(new Font("Helvetica", Font.BOLD, 14));
la2.setText("Paßwort: ");
pa.add(la2, new GridBagConstraints(0,2,1,1,1.0,0.1,
GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0),1,1));
JLabel la3 = new JLabel();
la3.setOpaque(true);
la3.setFont(new Font("Helvetica", Font.BOLD, 14));
la3.setText("Datenbank: ");
pa.add(la3, new GridBagConstraints(0,3,1,1,1.0,0.1,
GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0),1,1));
JLabel la4 = new JLabel();
la4.setOpaque(true);
la4.setFont(new Font("Helvetica", Font.BOLD, 14));
la4.setText("Host: ");
pa.add(la4, new GridBagConstraints(0,4,1,1,1.0,0.1,
GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0),1,1));
tf1.setFont(new Font("Helvetica", Font.PLAIN, 14));
pa.add(tf1, new GridBagConstraints(1,1,1,1,1.0,0.1,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0),1,1));
tf2.setFont(new Font("Helvetica", Font.PLAIN, 14));
pa.add(tf2, new GridBagConstraints(1,2,1,1,1.0,0.1,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0),1,1));
tf3.setFont(new Font("Helvetica", Font.PLAIN, 14));
pa.add(tf3, new GridBagConstraints(1,3,1,1,1.0,0.1,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0),1,1));
tf4.setFont(new Font("Helvetica", Font.PLAIN, 14));
pa.add(tf4, new GridBagConstraints(1,4,1,1,1.0,0.1,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0),1,1));
//JList li = new JList();
li.addItem("all");
li.addItem("select");
li.addItem("insert");
li.addItem("delete");
li.addItem("drop");
pa.add(li, new GridBagConstraints(0,5,1,1,1.0,1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0),1,1));
JLabel l = new JLabel();
l.setOpaque(true);
l.setFont(new Font("Helvetica", Font.BOLD, 14));
l.setText("Bitte wählen Sie die entsprechende Option!");
pa.add(l, new GridBagConstraints(1,5,1,1,1.0,1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0),1,1));
ausg.setFont(new Font("Helvetica", Font.PLAIN, 14));
ausg.setEditable(false);
JScrollPane scro = new JScrollPane(ausg);
pa.add(scro, new GridBagConstraints(0,6,3,3,1.0,1.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0),1,1));
bu10= new JButton();
bu10.setText("Benutzer speichern");
bu10.setBackground(Color.green);
bu10.addActionListener(this);
pa.add(bu10,new GridBagConstraints(0,9,1,1,1.0,0.1,
GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0),1,1));
bu20= new JButton();
bu20.setText("Schließen");
bu20.setBackground(Color.red);
bu20.addActionListener(this);
pa.add(bu20,new GridBagConstraints(1,9,1,1,1.0,0.1,
GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0),1,1));
this.getContentPane().setLayout(new GridBagLayout());
this.getContentPane().add(pa, new GridBagConstraints(0,0,1,1,1.0,1.0,
GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0,0,0,0),1,1));
this.pack();
this.setVisible(true);
}
//ActionListener für die Buttons
public void actionPerformed(ActionEvent e){
Object obj = e.getSource();
if(obj == bu10){
User.Texte("Button gedrückt");
Grant G = new Grant(tf1.getText(), tf2.getText(), tf3.getText(), tf4.getText(), li.getSelectedItem());
}
if(obj == bu20){
this.dispose();
}
}
}
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.sql.*;
import java.io.*;
//Klasse für die Verarbeitung und Ausgabe von "Benutzer anlegen"
public class Grant{
public Grant(String user, String pwd, String db,String host, String li){
User.Texte("kfdasklfjfsdgklklsgk");
//Statement stmt=null;
PreparedStatement stmt = null;
ResultSet res = null;
User.Texte("Verarbeitung beginnt");
try{
stmt = conn.prepareStatement("grant all on shanor to krieger@localhost identified by ´krieger´");
/*stmt.setString(1,li);
stmt.setString(2,db);
stmt.setString(3,user);
stmt.setString(4,host);
stmt.setString(5,pwd);*/
}
catch(SQLException e){
User.Texte("Fehler beim Ausführen der Abfrage"+e);
};
try{
if ( stmt.execute()){
res = stmt.getResultSet();
show_result(res);
res.close();
}
else{
User.Texte("Abfrage ausgeführt");
User.Texte("\r\nAnzahl Datensätze: "+ stmt.getUpdateCount());
}
}
catch (SQLException e){
User.Texte("Fehler beim Ausführen der Abfrage: "+ e);
}
try{
stmt.cancel();
stmt.close();
}
catch (SQLException e){
User.Texte("Fehler beim Ausführen der Abfrage" +e);
}
try{
stmt.cancel();
stmt.close();
}
catch(SQLException e){
User.Texte("Fehler beim Ausführen der Abfrage2"+e);
}
}
private void show_result(ResultSet r){
String line = new String();
String blank = new String();
StringBuffer l = new StringBuffer(255);
StringBuffer b = new StringBuffer(255);
for (int i=0; i<255; i++){
l.insert(i,"-");
b.insert(i," ");
}
line = l.toString();
blank = b.toString();
int col_width[]=null;
boolean col_blob[]=null;
int bytes = 0;
int c;
int ct=0;
Object o = new Object();
String s = new String();
StringBuffer x= new StringBuffer();
InputStream is = null;
try{
ResultSetMetaData resm = r.getMetaData();
ct = resm.getColumnCount();
col_width = new int[ct+1];
col_blob = new boolean[ct+1];
for ( int i=1; i<=ct; i++){
col_width[1] = resm.getColumnDisplaySize(i);
if(resm.getColumnLabel(i).length() > col_width[i]){
col_width[i] = resm.getColumnLabel(i).length();
}
if(resm.getColumnTypeName(i)=="BLOB"){
col_blob[i]=true;
}
else{
col_blob[i]=false;
}
}
show_result_header(resm,ct,col_width,col_blob);
while(r.next()){
x.setLength(0);
x.append("|");
for(int i=1; i<=ct; i++){
if ( ! col_blob[i]){
o = r.getObject(i);
if(o==null){
s="";
}
else{
s = o.toString();
}
if (s.length() > col_width[i]){
s = s.substring(0,col_width[i]);
}
x.append(" " +s);
x.append(blank.substring(0, col_width[i] - s.length()));
x.append(" |");
}
}
x.append("\r\n");
Befehl.Tex(x.toString());
for(int i=1; i<=ct;i++){
if(col_blob[i]){
x.setLength(0);
is = r.getAsciiStream(i);
try{
while((c=is.read()) != -1){
bytes++;
x.append((char)c);
}
}
catch (Exception e){
}
if (x.length()>0){
x.insert(0, resm.getColumnLabel(i)+": ");
x.insert(0, "-Anfang Blob["+ resm.getColumnLabel(i) +"]----------->\r\n");
x.append("\r\n-Ende Blob ["+ resm.getColumnLabel(i) +"]----------->\r\n\r\n");
Komplett.Text(x.toString());
}
}
}
}
}
catch (SQLException e){
Befehl.Tex("Fehler beim Auswerten der Abfrage! " + e);
}
show_line(ct, col_width, col_blob);
}
private void show_result_header(ResultSetMetaData resm, int ct, int[] col_width, boolean[] col_blob){
String line = new String();
String blank = new String();
StringBuffer l = new StringBuffer(255);
StringBuffer b = new StringBuffer(255);
for (int i=0; i<255; i++){
l.insert(i,"-");
b.insert(i," ");
}
line = l.toString();
blank = b.toString();
StringBuffer x = new StringBuffer();
String s;
try{
show_line(ct, col_width, col_blob);
x.append("|");
for ( int j=1; j<=ct; j++){
if(! col_blob[j]){
s = resm.getColumnLabel(j);
x.append(" " +s );
x.append(blank.substring(0, col_width[j] - s.length()));
x.append(" |");
}
}
x.append("\r\n");
Befehl.Tex(x.toString());
show_line(ct, col_width, col_blob);
}
catch(SQLException e){
Befehl.Tex("Fehler beim Auswerten der Abfrage! " + e);
}
}
private void show_line(int ct, int[] col_width, boolean[] col_blob){
String line = new String();
String blank = new String();
StringBuffer l = new StringBuffer(255);
StringBuffer b = new StringBuffer(255);
for (int i=0; i<255; i++){
l.insert(i,"-");
b.insert(i," ");
}
line = l.toString();
blank = b.toString();
StringBuffer x = new StringBuffer();
x.append("+");
for (int j=1;j<=ct; j++){
if(! col_blob[j]){
x.append("-" + line.substring(0,col_width[j]) + "-+");
}
}
x.append("\r\n");
Befehl.Tex(x.toString());
};
}