无法使用Java Swing更新数据库 [英] Not able to update database using java swing

查看:88
本文介绍了无法使用Java Swing更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

public class Datas extends JFrame implements ActionListener
{

    String query;
    JTextField id_t;
    JTextField name_t;
    JButton next;
    JButton add,del,up;
    JPanel p;
    static ResultSet res;
    static Connection conn;
    static Statement stat;

// Data class constructor

    public Datas()
    {
    
         super("Our Application");
         Container c=getContentPane();
         c.setLayout(new GridLayout(5,1));
         id_t=new JTextField(20);
         name_t=new JTextField(20);
         next=new JButton("Next");
         add=new JButton("Add");
         del=new JButton("Delete");
         up=new JButton("Update");
         p=new JPanel();
         c.add(new JLabel("Customer ID",JLabel.CENTER));
         c.add(id_t);
         c.add(new JLabel("Customer Name",JLabel.CENTER));
         c.add(name_t);
         c.add(p);
         p.add(add);
         p.add(next);
         p.add(del);
         p.add(up);
         next.addActionListener(this);
         add.addActionListener(this);
         del.addActionListener(this);
         up.addActionListener(this);
         pack();
         setVisible(true);
         setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

//Main creating connection between code and cust DATABASE

    public static void main(String s[])
    {
         Datas d=new Datas();
         try
         {
             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
             conn=DriverManager.getConnection("jdbc:odbc:cust");
             stat=conn.createStatement();
             res=stat.executeQuery("select * from table1");
             res.next();
         }

         catch(Exception e)
         {
             System.out.println("Error"+e);
         }

         d.showRecord(res);

         }


// Called when some button is clicked

         public void actionPerformed(ActionEvent e)
         {
             if(e.getSource()==next)
             {
                 try
                 {
                     res.next();
                 }
                 catch(Exception ee){}
             
                 showRecord(res);
             }

//This inserts record in table1 taking values from textfields

             else if(e.getSource()==add)
             {
                 try
                 {
                     query="insert into table1 values(' "+id_t.getText()+"   ','                                        "+name_t.getText()+" ')";
                 stat.executeUpdate(query);
                 }
                 catch(Exception ee){}
                 try
                 {
                     res=stat.executeQuery("select * from table1");
                     showRecord(res);
                 }
                 catch(Exception ee){}
             }

//This delete record from table1 where id matches id in textfield

             else if(e.getSource()==del)
             {
                 try
                 {
                     System.out.println(id_t.getText());
                     query="delete from table1 where ID="+id_t.getText()+"     ";
                     stat.executeUpdate(query);
                 }

                 catch(Exception ee){}
                 try
                 {
                     res=stat.executeQuery("select * from table1");
                     showRecord(res);
                 }
                 catch(Exception ee){}
             }

/*This does not work,updation in table1 is not done when you change value in textfield and click update */


             else if(e.getSource()==up)
             {
                 try
                 {
                     query="update table1 set name=' "+name_t.getText()+" '                      where id="+id_t.getText()+")";
                     stat.executeUpdate(query);
                 }
                 catch(Exception ee){}

                 try
                 {
                     res=stat.executeQuery("select * from table1");

                     showRecord(res);
                 }
                 catch(Exception ee){}
             }
        }

         public void showRecord(ResultSet res)
         {
             try
             {
                  id_t.setText(res.getString(1));
                  name_t.setText(res.getString(2));
             }
             catch(Exception ee){}
         }

}

推荐答案

大概是下面的代码引起了错误;你得到一个错误吗?您还会在捕获任何异常时消耗任何异常,这对诊断问题没有帮助.

Presumably the error is with the code below; do you get an error? You are also consuming any exceptions when caught, which does not help to diagnose the problem.

query="update table1 set name='' "+name_t.getText()+" ''                      where id="+id_t.getText()+")";
                     stat.executeUpdate(query);


您可以使用此代码(针对Google)对SQL注入敞开大门,这有可能损害您的系统.


You are leaving yourself wide open to SQL injection with this code (Google for it), which has the potential to damge your system.


这篇关于无法使用Java Swing更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆