Hey,

My program is to add images to database using class GUI in java. Ang i can not get into a code that can insert into database

I'am using this codes:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.event.TableModelListener;

public class DForm extends JInternalFrame implements ItemListener,MouseListener,ActionListener
{

    private JPanel pBook = new JPanel ();
    private Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

    private JLabel lbCategory,lbls;
    private JTextField txts,txtid;

    private Connection con=null;
    private Statement st=null;
    private ResultSet rs=null;
    private int resp;


    private JButton btsearch,btdelete;

    private JScrollPane scroller;
    private JTable table;   //Table for Displaying Records.
//  private Statement st;   //Statement for Getting the Required Table.

    private int rec = 0;
    private int total = 0;

    private String rowRec[][];  //String Type Array use to Load Records into File.
//  private Connection con=null;

    //Constructor of Class.

    public DForm () {


        super("Delete Form");
        setSize (500, 330);

        //Setting Panel's Layout.

        pBook.setLayout (null);

        getRecords ("");    //Getting Records.

        table = makeTable ();           //Creating Table.
        scroller = new JScrollPane (table); //Adding Table to ScrollPane.
        scroller.setBounds (20, 50, 460, 200);  //Aligning ScrollPane.

        //Setting the Form's Labels.

        lbCategory = new JLabel ("Search by Operetor");
        lbCategory.setForeground (Color.black);
        lbCategory.setBounds (20, 18, 275, 25);

        //Setting the Form's ComboBox.



        lbls = new JLabel(" ");
        lbls.setForeground(Color.black);
        lbls.setBounds(100,18,75,25);

        txts= new JTextField();
        txts.setBounds(150,18,330,25);

        txtid= new JTextField();
        txtid.setBounds(0,0,0,0);

        btdelete = new JButton("Delete");
        btdelete.setBounds(150,255,200,30);
        btdelete.setToolTipText ("Next");
        btdelete.addActionListener(this);



        DocumentListener documentListener = new DocumentListener()
        {
        public void changedUpdate (DocumentEvent documentEvent)
                {
                rec = 0;
                total = 0;
                getRecords (txts.getText());
                scroller.getViewport().remove (table);
                scroller.getViewport().add (makeTable ());
                repaint();
                }

        public void insertUpdate (DocumentEvent documentEvent)
                {
                rec = 0;
                total = 0;
                getRecords (txts.getText());
                scroller.getViewport().remove (table);
                scroller.getViewport().add (makeTable ());
                repaint();
                }

        public void removeUpdate (DocumentEvent documentEvent)
                {
                rec = 0;
                total = 0;
                getRecords (txts.getText());
                scroller.getViewport().remove (table);
                scroller.getViewport().add (makeTable ());
                repaint();
                }
        };

        txts.getDocument().addDocumentListener(documentListener);


        //Adding All the Controls in Panel.

        btsearch = new JButton("Search");
        btsearch.setBounds(185, 18, 75, 25);
        btsearch.addItemListener(this);

        pBook.add (lbCategory);
        pBook.add (scroller);
        pBook.add (lbls);
        pBook.add (txts);
        pBook.add (btdelete);
        pBook.add (txtid);



        //Adding Panel to the Form.

        getContentPane().add (pBook);


        setVisible (true);

    }

    //Function Perform By the ComboBox of Form.

    public void itemStateChanged (ItemEvent e) {

        rec = 0;
        total = 0;
        getRecords ("");    //Getting Records.
        scroller.getViewport().remove (table);
        scroller.getViewport().add (makeTable ());
        this.repaint();

    }

    //Function use to Getting Records.

    private void getRecords (String bb) {

        String records[][] = new String [500][5];       //String Type Array use to Load Records From Table.

        try {   //SELECT Query to Retrieve Records From Table.
            DataConnection condb = new DataConnection();
            con=condb.dbConnect("jdbc:mysql://localhost:3306/john");
            Statement st = con.createStatement();

            String q = "SELECT * FROM reg where owner Like '%" +bb+ "%' ORDER By engine;";

            ResultSet rs = st.executeQuery (q);     //Running Query.

            while (rs.next ()) {
                records[rec][0] = "" + rs.getString ("engine");
                records[rec][1] = rs.getString ("owner");
                records[rec][2] = rs.getString ("address");
                records[rec][3] = "" + rs.getString ("made");
                records[rec][4] = rs.getString ("body");
                rec++;
            }

            total = rec;
            rowRec = new String [total][5];

            if (total == 0) {
            }
            else {
                for (int i = 0; i < total; i++) {
                    rowRec[i][0] =  records[i][0];
                    rowRec[i][1] =  records[i][1];
                    rowRec[i][2] =  records[i][2];
                    rowRec[i][3] =  records[i][3];
                    rowRec[i][4] =  records[i][4];
                }
            }
        }
        catch (Exception sqlEx) { }

    }

    //Function to Create the Table and Add Data to Show.

    private JTable makeTable () {

        //String Array For Table Columns.
        String colsName[] = {"Engine No.", "Operator", "Address", "Made","Body Type"};

        table = new JTable (rowRec, colsName) {
            public boolean isCellEditable (int iRows, int iCols) {
                return false;   //Disable All Columns of Table.
            }
        };

        table.setRowHeight (20);    //Set Rows Height.
        table.addMouseListener(this);

        return table;

    }





        public void mouseClicked (MouseEvent e)
            {
                int row=table.rowAtPoint(e.getPoint());
                int col=table.columnAtPoint(e.getPoint());

                String tid="";

                tid="" + table.getModel().getValueAt(row,0);
                txtid.setText(tid);


            }

        public void mouseExited(MouseEvent e)
        {}
        public void mouseReleased(MouseEvent e)
        {}
        public void mousePressed(MouseEvent e)
        {}
        public void mouseEntered(MouseEvent e)
        {}


        public void actionPerformed(ActionEvent ae)
                {
                    Object obj = ae.getSource();

                    if (obj == btdelete)
                    {
                    resp = JOptionPane.showConfirmDialog(null,"Are yOu sUre you want to delete the data where no. "+txtid.getText()+"?","Confirmation",JOptionPane.YES_NO_OPTION);
                            if (resp == JOptionPane.YES_OPTION)
                        {

                        try
                             {
                                DataConnection conn= new DataConnection();
                                Connection con= conn.dbConnect("jdbc:mysql://127.0.0.1:3306/john");
                                Statement st=con.createStatement();


                                String sql="delete from reg where engine='"+txtid.getText()+"';";
                                int rs=st.executeUpdate(sql);
                                if(rs>0)
                                {
                                    JOptionPane.showMessageDialog(null,"Record Delete");
                                    rec = 0;
                                    total = 0;
                                    getRecords (txts.getText());
                                    scroller.getViewport().remove (table);
                                    scroller.getViewport().add (makeTable ());
                                    repaint();
                                    txtid.setText("");


                                }
                                else
                                {
                                    JOptionPane.showMessageDialog(null,"Error in Saving ","Error RecordDelete",JOptionPane.ERROR_MESSAGE);
                                }


                                st.close();
                                con.close();

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

                    }



                }                   


}

i want to open a browse file for me to add my images to database

Edited 3 Years Ago by Nick Evan: Fixed formatting

Here is my code for file open

import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
import java.util.*;
import javax.imageio.*;
//panel used to draw image on
public class ImagePanel extends JFrame
{
//path of image
private String path;

//image object
private Image img;

public ImagePanel(String path) throws IOException
{
//save path
this.path = path;

//load image
img = ImageIO.read(new File(path));

}

//override paint method of panel
public void paint(Graphics g)
{
//draw the image
if( img != null)
g.drawImage(img,0,0, this);
}

}

//example of using image panel
class ImageFrame
{
public static void main(String[] args) throws IOException
{

try
{

//create frame
JFrame f = new JFrame();

//ask for image file
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(f);

//create panel with selected file
ImagePanel panel = new ImagePanel( chooser.getSelectedFile().getPath() );

//add panel to pane
f.getContentPane().add(panel);


//show frame
f.setBounds(0,0,800,800);
f.setVisible(true);
}
catch(Exception e)
{
System.out.println ( "Please verify that you selected a valid image file");
}
}
}

This article has been dead for over six months. Start a new discussion instead.