2016-12-09 3 views
-1

Ich möchte eine weitere Spalte in mysql entweder mit Java oder manuell in der mysql-Anwendung hinzufügen. Das ist mein Java-Code und ich brauche eine Spalte book_id namensIch muss eine weitere Spalte in meiner Datenbank hinzufügen, aber ich weiß nicht, wie

hinzufügen
package liblog; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.sql.*; 


public class AStudents extends JFrame { 
private JTextField admissionnumber; 
private JTextField firstname; 
private JTextField lastname; 
private JTextField surname; 
private JTextField house; 
private JTextField dome; 
private JButton add; 
private JButton back; 
private static Connection conn; 

JFrame frame = new JFrame(); 
ImageIcon icon = new ImageIcon("C:/Users/User/workspace/Login/src/liblog/parklands.png"); 
JPanel panel = new JPanel(new GridBagLayout()); 
JPanel panel2 = new JPanel(); 
JPanel panel3 = new JPanel(); 
JLabel admissionNumber = new JLabel(); 
JLabel firstName = new JLabel(); 
JLabel lastName = new JLabel(); 
JLabel surName = new JLabel(); 
JLabel mainHouse = new JLabel(); 
JLabel mainDome = new JLabel(); 

AStudents(){ 
    super("DR.RIBEIRO PARKLANDS SCHOOL"); 
    setLayout(new FlowLayout(FlowLayout.LEFT,3,100)); 
    setBounds(500,500,550,550); 
    setLocation(500,200); 
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    Container con = this.getContentPane(); 
    con.setBackground(Color.GRAY); 
    con.add(panel); 
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS)); 
    panel2.setLayout(new FlowLayout(FlowLayout.CENTER,3,10));/*X axis,Y axis*/ 
    panel3.setLayout(new BoxLayout(panel,BoxLayout.LINE_AXIS)); 
    panel.setBackground(Color.GRAY); 

    /* admissionNumber=new JLabel("Admission Number"); 
    admissionNumber.setAlignmentX(0.0f); 
    admissionnumber = new JTextField("No",30); 
    admissionnumber.setAlignmentX(0.0f);*/ 

    firstName=new JLabel("First Name"); 
    firstName.setAlignmentX(0.0f); 
    firstname = new JTextField("Name",20); 
    firstname.setAlignmentX(0.0f); 

    lastName=new JLabel("Last Name"); 
    lastName.setAlignmentX(0.0f); 
    lastname = new JTextField("Name",20); 
    lastname.setAlignmentX(0.0f); 

    surName=new JLabel("Surname"); 
    surName.setAlignmentX(0.0f); 
    surname = new JTextField("Name",20); 
    surname.setAlignmentX(0.0f); 

    mainHouse=new JLabel("House"); 
    mainHouse.setAlignmentX(0.0f); 
    house = new JTextField("House",20); 
    house.setAlignmentX(0.0f); 

    mainDome=new JLabel("Dome"); 
    mainDome.setAlignmentX(0.0f); 
    dome = new JTextField("Dome",20); 
    dome.setAlignmentX(0.0f); 


    add = new JButton("Add Student"); 
    back = new JButton("Back"); 

    add.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      // Insert into SQL: 
      try { 
       PreparedStatement insert = conn.prepareStatement("INSERT INTO students (first_name, last_name, surname, house, dome) VALUES (?,?,?,?,?)"); 
       insert.setString(1, firstname.getText()); 
       insert.setString(2, lastname.getText()); 
       insert.setString(3, surname.getText()); 
       insert.setString(4, house.getText()); 
       insert.setString(5, dome.getText()); 

       insert.execute(); 
       ABooks abooks = new ABooks(); 
       dispose(); 
      } 
      catch(Exception ex) { 
       System.out.println(ex); 
      } 

     } 
    }); 

    back.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      Admin admin = new Admin(); 
      dispose(); 
     } 
    }); 

    //panel.add(admissionNumber); 
    //panel.add(admissionnumber); 

    panel.add(firstName); 
    panel.add(firstname); 

    panel.add(lastName); 
    panel.add(lastname); 

    panel.add(surName); 
    panel.add(surname); 

    panel.add(mainHouse); 
    panel.add(house); 

    panel.add(mainDome); 
    panel.add(dome); 



    panel.add(add); 
    panel.add(back); 

try { 
     conn = getConnection(); 
    createTable(); 
} 
catch(Exception ex) { 
    System.out.println(ex); 

} 
setVisible(true); 
} 

/*public static void main(String[]args) throws Exception{new AStudents(); 
    createTable(); 
}*/ 

//Table 
public static void createTable() throws Exception{ 
     try{ 
      Connection conn = getConnection(); 
      PreparedStatement create = conn.prepareStatement("CREATE TABLE IF NOT EXISTS students(id int NOT NULL AUTO_INCREMENT,first_name varchar(45),last_name varchar(45),surname varchar(320),house varchar(50),dome varchar(50),book_id varchar(50),PRIMARY KEY(id))"); 
      create.executeUpdate(); 
     }catch(Exception e){System.out.println(e);} 
     finally { 
      System.out.println("System Updated");}; 
    } 
//EndofTable 

//DB 
public static Connection getConnection() throws Exception{ 

     try{ 
      String driver = "com.mysql.jdbc.Driver"; 
      String url = "jdbc:mysql://localhost:3306/students?useSSL=false"; 
      String username = "root"; 
      String password = "toor"; 
      Class.forName(driver); 

      Connection conn =  DriverManager.getConnection(url,username,password); 
      System.out.println("Connected"); 
      return conn; 
     }catch(Exception e){ 
      System.out.println(e); 
     } 
     return null; 
    } 
//DB 
    } 

Die Anwendung mir die folgende Fehlermeldung zu geben, die ich nicht von com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException loswerden kann: Unbekannte Spalte 'borrow_id' in 'field list'

das ist der andere Code http://www.rapidtables.com/tools/notepad.htm

+0

Ich sehe borrow_id in keiner der Code. Sind Sie sicher, dass dies der Code ist, der den Fehler verursacht? – xQbert

+0

ist unklar, welche Zeile diese Ausnahme auslöst. Es gibt keine 'book_id' in Ihrem Code. Zeigen Sie auf die Zeile, die die Ausnahme auslöst. – Yousaf

+0

http://www.rapidtables.com/tools/notepad.htm Ich habe den Code vor einem Monat geschrieben, und ich habe alles vergessen, seit ich kaum neu in Java bin. Dies ist der andere Code, den ich habe – Ben

Antwort

1

in der SQL-DB:

ALTER TABLE students ADD book_id varchar(50) 

Ich vermute den Datentyp der book_id.

Meinst du book_id oder borrow_id? Ich sehe borrow_id nicht in Ihrem Code, was etwas verwirrend ist und wahrscheinlich, warum Sie abgelehnt werden.

+0

Vielen Dank. Du bist der Einzige, der verstanden hat, wonach ich gefragt habe. – Ben

Verwandte Themen