2012-03-05 2 views
0

질의가 있습니다. 데이터베이스가 있고 코드를 작성하려고하므로 데이터베이스의 레코드를 Java 소프트웨어에서 만들 수 있습니다.java sql queries

데이터베이스에 연결하는 커넥터 클래스가 있고 registerStudent 클래스는 사용자가 2 개의 텍스트 필드에 값을 입력 할 수 있도록합니다. 값은 데이터베이스 테이블에 레코드를 작성하는 데 사용해야합니다.

나는 그것이 나에게이 오류 코드 제공 제출 버튼을 쳤을 때 : - :

import java.awt.Component; 
import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.Border; 
import javax.swing.border.LineBorder; 
import javax.swing.JLabel; 
import java.awt.Font; 
import javax.swing.JTextField; 
import javax.swing.JButton; 
import javax.swing.JOptionPane; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.sql.SQLException; 


public class RegisterStudent 
{ 

    public RegisterStudent() { 
     initialize(); 
    } 

    public JFrame frmRegisterStudent; 

    Connector con; 
    private JTextField textField_1; 
    private JTextField textField_2; 

    // initialise the frame 
    private void initialize() { 
     frmRegisterStudent = new JFrame(); 
     frmRegisterStudent.setTitle("LEC AdminPro: RegisterStudents"); 
     frmRegisterStudent.setBounds(100, 100, 413, 225); 
     frmRegisterStudent.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 
     frmRegisterStudent.setLocationRelativeTo(null); 


     Border border = LineBorder.createGrayLineBorder(); 
     frmRegisterStudent.getContentPane().setLayout(null); 
     con = new Connector(); 

     JPanel panel_1 = new JPanel(); 
     panel_1.setBounds(0, 0, 397, 189); 
     frmRegisterStudent.getContentPane().add(panel_1); 
     panel_1.setBorder(border); 
     panel_1.setLayout(null); 

     JLabel lblRegister = new JLabel("Register Student"); 
     lblRegister.setFont(new Font("Tahoma", Font.BOLD, 12)); 
     lblRegister.setBounds(10, 11, 124, 20); 
     panel_1.add(lblRegister); 

     JLabel lblSurname = new JLabel("Name"); 
     lblSurname.setFont(new Font("Arial", Font.PLAIN, 11)); 
     lblSurname.setBounds(10, 63, 69, 14); 
     panel_1.add(lblSurname); 

     JLabel lblDob = new JLabel("Profession"); 
     lblDob.setFont(new Font("Arial", Font.PLAIN, 11)); 
     lblDob.setBounds(10, 88, 69, 14); 
     panel_1.add(lblDob); 

     textField_1 = new JTextField(); 
     textField_1.setColumns(10); 
     textField_1.setBounds(104, 63, 266, 20); 
     panel_1.add(textField_1); 

     textField_2 = new JTextField(); 
     textField_2.setColumns(10); 
     textField_2.setBounds(104, 88, 266, 20); 
     panel_1.add(textField_2); 


     JButton btnNewButton = new JButton("Cancel"); 
     btnNewButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       Object[] options = {"Yes", "No"}; 
       Component form = null; 
       int n = JOptionPane.showOptionDialog(form, "Would you like to cancel the new Registration?", "Exit Confirmation", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options); 
       if(n == JOptionPane.YES_OPTION) { 
       frmRegisterStudent.setVisible(false); 
      } 

     } 
     }); 
     btnNewButton.setBounds(280, 119, 89, 23); 
     panel_1.add(btnNewButton); 

     JButton button = new JButton("Submit"); 
     button.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 


      String name = textField_1.getText();  
      String profession = textField_1.getText(); 


       try { 
      con.stmt.executeUpdate("INSERT INTO staff (Name, Profession)"+"VALUES"+"("+"'"+name+"',"+"'"+profession+"')"); 
      JOptionPane.showMessageDialog(frmRegisterStudent, "New Record has been added"); 

      } catch (SQLException e) { 
      System.out.println("Record couldn't be added!"); 
      e.printStackTrace(); 
      } 
      } 
      }); 



     button.setBounds(181, 119, 89, 23); 
     panel_1.add(button); 


    } 




    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      public void run() { 
        try { 
       RegisterStudent window = new RegisterStudent(); 
       window.frmRegisterStudent.setVisible(true); 
       } catch (Exception e) { 
       e.printStackTrace(); 
       } 
      } 
     }); 

     public void setVisible(boolean b) { 
     frmRegisterStudent.setVisible(true); 

}  
} 
참고

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
at RegisterStudent$2.actionPerformed(RegisterStudent.java:99) 

con.stmt.executeUpdate("INSERT INTO staff (Name, Profession)"+"VALUES"+"("+"'"+name+"',"+"'"+profession+"')"); 

이는 registerStudent 클래스 내 코드 = 라인 99 코드

+0

아마도 'con.stmt'가 null이기 때문일 수 있습니다. 'Connector' 클래스에는 무엇이 있습니까? –

+0

커넥터 구성은 어떻게 든 'stmt' 필드를 초기화합니까? –

+0

stmt = queryConnection.createStatement(); – Adrian

답변

0

Connector 클래스에 문제가있는 것 같습니다.이 클래스는 stmt입니다. 필드가 초기화되지 않습니다. NullPointerException을

con.stmt = con.conn.prepareStatement("insert into staff (name, profession) values (?, ?)"); 
con.stmt.setString(1, name); 
con.stmt.setString(2, profession); 
con.stmt.executeUpdate(); 

그러나이 대신 라인의

사용이 아주 나쁜 디자인이 있습니다.

+0

내 커넥터 클래스에서, 나는 PreparedStatement stmt있어; –

+0

@ 피타 : 필드를 선언했지만 초기화하지 않았습니다. 이것을 반영하기 위해 대답을 바꿀 것입니다. –

0

커넥터가 null입니까? stmt가 커넥터의 필드라는 것을 알았습니까? 초기화 했습니까? 그런데 Prepared 문을 사용하는 것이 문자열에서 이스케이프 (예 : " '"가있는 경우)되므로 이스케이프 처리가 훨씬 쉬워집니다.

PreparedStatement pstmt = connection.prepareStatement("insert into staff (name, profession) values (?, ?)"); 
pstmt.setString(1, name); 
pstmt.setString(2, profession); 
pstmt.executeUpdate(); 
+0

커넥터에서 null입니다. 어떻게 초기화할까요, 어떤 클래스에 있나요? 준비된 진술을 사용하면 무엇을 의미합니까? 준비된 진술로 내 코드의 예를 보여 줄 수 있습니까? 많은 감사 –

+0

이것은 준비된 문장 샘플입니다. –

+0

감사합니다. 커넥터를 초기화하는 방법을 다시 말해 줄 수 있습니까? –