Ich hab das jetzt eingegeben, aber leider kommt immer noch der Nullpointer in der Zeile --> stmt = conn.prepareStatement(SQL);
Ich poste jetzt hier mal den Aufruf mit, vielleicht hab ich ja auch da einen Fehler drinn. Ich weiß nicht mehr weiter, warum ich immer diese nullpointer krieg????
Eingabemaske = User1.java
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 User1 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 JTextField tf5 = new JTextField();
public static void Texte(String text){
User1.ausg.append("\n"+text);
}
public User1(){
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));
JLabel l2 = new JLabel();
l2.setOpaque(true);
l2.setFont(new Font("Helvetica", Font.BOLD, 14));
l2.setText("Option: ");
pa.add(l2, new GridBagConstraints(0,5,1,1,1.0,0.1,
GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0),1,1));
tf5.setFont(new Font("Helvetica", Font.PLAIN,14));
pa.add(tf5, new GridBagConstraints(1,5,1,1,1.0,0.1,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 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 ob = e.getSource();
if(ob == bu10){
User1.Texte("Button gedrückt");
Grant G = new Grant();
}
if(ob == bu20){
this.dispose();
}
}
}
Verarbeitung = Grant.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.sql.*;
import java.io.*;
import java.util.Enumeration;
//Klasse für die Verarbeitung und Ausgabe von "Benutzer anlegen"
public class Grant{
public Grant(){
Connection conn = null;
PreparedStatement stmt = null;
ResultSet res = null;
User1.Texte("Aktuell geladene MySQL - Treiber ...");
for ( Enumeration enum = DriverManager.getDrivers(); enum.hasMoreElements()
{
User1.Texte(enum.nextElement().getClass().getName());
}
User1.Texte("Verarbeitung beginnt");
String sql = "grant all on shanor.* to test@localhost identified by 'test'";
try{
stmt = conn.prepareStatement(sql);
}
catch(SQLException e){
User1.Texte("Fehler beim Ausführen der Abfrage: "+e);
};
try{
stmt.executeUpdate(sql);
User1.Texte("\r\nAnzahl Datensätze: "+stmt.getUpdateCount());
}
catch(SQLException e){
User1.Texte("Fehler beim Ausführen " + e);
}
try{
stmt.cancel();
stmt.close();
}
catch(SQLException e){
User1.Texte("Fehler beim Ausführen " +e);
}
try{
stmt.cancel();
stmt.close();
}
catch(SQLException e){
User1.Texte("Fehler beim Ausführen " +e);
}
}
}