2014-01-13 4 views
0

Sir/Madam Нужна ваша помощькак отображать записи из базы данных в textfied в swing?

я хочу Disply записи из базы данных по текстовому полю. Я не получаю результат этого даже код отладки успешно

Я новичок в языке программирования

* Это мой код *

import java.awt.Container; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.sql.*; 
import javax.swing.*; 
public abstract class customer_details extends JFrame implements ActionListener 
{ 
JTextField cust_id,cust_nm,cont; 
JLabel l1,l2,l4,l5; 
JButton b1,b2; 
private PreparedStatement ps; 
Container c = getContentPane();  
customer_details() 
{ 
    super("ancfsdfasdfasfas"); 
    setBounds(140,250,777,555); 
    c.setLayout(null); 
    cust_id = new JTextField(); 
    l1 = new JLabel("Update Customer Details"); 
    l2 = new JLabel("Customer Id"); 
    l1.setBounds(10,10,340,20);   
    l2.setBounds(10,20,140,70); 
    l4 = new JLabel("Customer Name"); 
    l4.setBounds(90,140,100,20); 
    cust_nm = new JTextField(); 
    cust_nm.setBounds(90,160,160,20); 
    l5 = new JLabel("Contact No"); 
    l5.setBounds(270,140,100,20); 
    cont = new JTextField(); 
    cont.setBounds(270, 160, 100, 20); 
    cust_id.setBounds(10,70,70,20); 
b1 = new JButton("View");  
b1.setBounds(90,70,90,20); 
    b2 = new JButton("Update"); 
    b2.setBounds(10,200,90,20); 
    c.add(l1); 
    c.add(l2); 
    c.add(l4); 
    c.add(l5); 
    c.add(cust_id); 
    c.add(cust_nm); 
    c.add(cont); 
    c.add(b1); 
    c.add(b2); 
    setVisible(true); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    b1.addActionListener(this); 
    b2.addActionListener(this); 
} 
public static void main(String[] args) 
{ 
    customer_details cpap=new customer_details() {}; 
} 
public void actionPerformed(ActionEvent e) 
{    
    System.out.println("You clicked the button"); 
    if(e.getSource()==b1) 
    { 
     try 
     { 
      Connection con; 
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
      con = DriverManager.getConnection("jdbc:odbc:Dalvi"); 
      ResultSet rs = null; 
      try 
      { 
       java.sql.Statement st = con.createStatement(); 
       PreparedStatement ps = con.prepareStatement("select from customer_details (customer_name,contact_no) where customer_id=?)");       
       ps.setString(1,cust_id.getText());      
       if (rs.next()) 
       {      
        cust_nm.setText(rs.getString("customer_name")); 
        cont.setText(rs.getString("contact_no"));      
       } 
       JOptionPane.showMessageDialog(null,"You Successfully Deleted The Customer");       
        } 
        catch (SQLException s) 
        { 
         System.out.println("SQL code does not execute."); 
         JOptionPane.showMessageDialog(null,"Please Enter the Detail Correctly"); 
        } 
       } 
       catch (ClassNotFoundException | SQLException ee) 
       { 
        System.out.println("Error:connection not created"); 
        JOptionPane.showMessageDialog(null,"Please Enter the Detail Correctly"); 
       } 
      }    
     } 

}

спасибо за помощь :)

+0

Ahhh, 'null' layout ... преодолевая это на секунду ... что не работает? – MadProgrammer

ответ

1
  1. Ваш запрос неправильно. Посмотрите на ответ @MadProgrammer.
  2. Вы - ResultSet - null. Вы не выполнили запрос, чтобы вернуть ResultSet

    PreparedStatement ps = con.prepareStatement("select customer_name, contact_no from customer_details where customer_id=?");       
    ps.setString(1,cust_id.getText()); 
    
    rs = ps.executeQuery(); 
    
    if (rs.next()) 
    {      
        cust_nm.setText(rs.getString("customer_name")); 
        cont.setText(rs.getString("contact_no"));      
    } 
    
  3. Я также заметил это после того, как результирующего набора кода

    JOptionPane.showMessageDialog(null,"You Successfully Deleted The Customer"); 
    

    Это говорит мне, может быть, вы хотите удалить запись. Только FYI, SELECT не удаляет запись базы данных.

+0

благодарит @MadProgrammer, @ peeskillet, @ Vinod Satpute – Devendra

2

В moement, основываясь на вашем примере, вы должны получить исключение, как ...

select from customer_details (customer_name,contact_no) where customer_id=? 

не является допустимым запрос. Он должен больше походить на:

select customer_name, contact_no from customer_details where customer_id=? 
1

В вашем запросе проблема. Исправьте его, а также верните свой контроль, когда у вас есть какая-либо запись.

if (rs.next()) {
cust_nm.setText(rs.getString("customer_name")); cont.setText(rs.getString("contact_no")); return;
}

+0

спасибо, сэр. Моя проблема уже решена. Бог благословит вас всех .. – Devendra

Смежные вопросы