Hallo
Ein Freund hat folgendes Problem:
Tabelle:
CREATE TABLE buch (
nr INTEGER PRIMARY KEY,
titel VARCHAR(255),
seiten INTEGER);
Code:
Bei meinen Button Insert funktioniert das Einfügen von neuen Daten nicht, er schreibt mir das der Mysql-Befehl nicht stimmt,
Alle andren Buttons funktionieren;
Bitte um Hilfe
MfG
Ein Freund hat folgendes Problem:
Tabelle:
CREATE TABLE buch (
nr INTEGER PRIMARY KEY,
titel VARCHAR(255),
seiten INTEGER);
Code:
Code:
import java.awt.event.*;
import java.applet.Applet;
import java.awt.*;
import javax.swing.JOptionPane;
import java.sql.*;
public class Datenbank extends Applet
{
TextField nr, titel;
Label lbnr, lbtitel;
Button bErste,bZurueck,bNext,bLetzte,bDelete,bAendern,bInsert;
ResultSet r;
public void init()
{
try {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection("jdbc:mysql://localhost/bibliothek?user=root");
Statement s = c.createStatement();
s.execute("SELECT Buch.Nr,Buch.Titel FROM Buch");
r = s.getResultSet();
//c.close();
}
catch (SQLException e1) {
JOptionPane.showMessageDialog(null, "Exception aufgetreten: " + e1);
}
catch (Exception e2) {
JOptionPane.showMessageDialog(null, "Exception aufgetreten: " + e2);
}
nr = new TextField ("");
titel = new TextField ("");
bErste = new Button (" 1. ");
bZurueck = new Button ("Zurück");
bNext = new Button ("Next");
bLetzte = new Button ("letzter");
lbnr = new Label ("BuchNr: ");
lbtitel = new Label ("BuchTitel: ");
bDelete = new Button ("Delete");
bAendern = new Button ("Ändern");
bInsert = new Button ("Insert");
this.add(bErste);
this.add(bZurueck);
this.add(bNext);
this.add(bLetzte);
this.add(bDelete);
this.add(bAendern);
this.add(bInsert);
this.add(lbnr);
this.add(nr);
this.add(lbtitel);
this.add(titel);
Erste z1 = new Erste();
Zurueck z2 = new Zurueck();
Next z3 = new Next();
Letzte z4 = new Letzte();
Delete z5 = new Delete();
Aendern z6 = new Aendern();
Insert z7 = new Insert();
bErste.addActionListener (z1);
bZurueck.addActionListener (z2);
bNext.addActionListener (z3);
bLetzte.addActionListener (z4);
bDelete.addActionListener (z5);
bAendern.addActionListener (z6);
bInsert.addActionListener (z7);
}
private class Erste implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
r.first();
nr.setText("" + r.getInt("Nr"));
titel.setText(r.getString("Titel"));
}
catch
(SQLException e1)
{
JOptionPane.showMessageDialog(null, "Exception aufgetreten: " + e1);
}
JOptionPane.showMessageDialog(null, "Button First gedrückt");
}
}
private class Zurueck implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
r.previous();
nr.setText("" + r.getInt("Nr"));
titel.setText(r.getString("Titel"));
}
catch
(SQLException e1)
{
JOptionPane.showMessageDialog(null, "Exception aufgetreten: " + e1);
}
JOptionPane.showMessageDialog(null, "Button Zurück gedrückt");
}
}
private class Next implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
r.next();
nr.setText("" + r.getInt("Nr"));
titel.setText(r.getString("Titel"));
}
catch
(SQLException e1)
{
JOptionPane.showMessageDialog(null, "Exception aufgetreten: " + e1);
}
JOptionPane.showMessageDialog(null, "Button Nächster gedrückt");
}
}
private class Letzte implements ActionListener{
public void actionPerformed(ActionEvent e){
try {
r.last();
nr.setText("" + r.getInt("Nr"));
titel.setText(r.getString("Titel"));
}
catch (SQLException e1) {
JOptionPane.showMessageDialog(null, "Exception aufgetreten: " + e1);
}
JOptionPane.showMessageDialog(null, "Button Letzter gedrückt");
}
}
private class Delete implements ActionListener{
public void actionPerformed(ActionEvent e){
try {
String Nr = nr.getText();
Connection c = DriverManager.getConnection("jdbc:mysql://localhost/bibliothek?user=root");
Statement s = c.createStatement();
String sqlstatement = "Delete from Buch WHERE Buch.Nr =" + Nr;
s.executeUpdate(sqlstatement);
}
catch (SQLException e1) {
JOptionPane.showMessageDialog(null, "Exception aufgetreten: " + e1);
}
JOptionPane.showMessageDialog(null, "Button Delete gedrückt");
}
}
private class Aendern implements ActionListener{
public void actionPerformed(ActionEvent e){
try {
String Nr = nr.getText();
String Titel = titel.getText();
Connection c = DriverManager.getConnection("jdbc:mysql://localhost/bibliothek?user=root");
Statement s = c.createStatement();
String sqlstatement = "UPDATE Buch SET Titel ='" + Titel + "' WHERE Nr = " + Nr;
s.executeUpdate(sqlstatement);
c.close();
}
catch (SQLException e1) {
JOptionPane.showMessageDialog(null, "Exception aufgetreten: " + e1);
}
JOptionPane.showMessageDialog(null, "Button Aendern gedrückt");
}
}
private class Insert implements ActionListener{
public void actionPerformed(ActionEvent e){
try {
String Nr = nr.getText();
String Titel = titel.getText();
Connection c = DriverManager.getConnection("jdbc:mysql://localhost/bibliothek?user=root");
Statement s = c.createStatement();
String sqlstatement = "INSERT INTO Buch (nr,titel,seiten) VALUES (" + Nr + "," + Titel + "," + "0" + ")";
s.executeUpdate(sqlstatement);
r = s.getResultSet();
c.close();
}
catch (SQLException e1) {
JOptionPane.showMessageDialog(null, "Exception aufgetreten: " + e1);
}
JOptionPane.showMessageDialog(null, "Button Insert gedrückt");
}
}
}
Alle andren Buttons funktionieren;
Bitte um Hilfe
MfG
Zuletzt bearbeitet von einem Moderator: