Maybe if you post the full code we can understand better what you are trying to do.
This is my code
please help:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.sql.*;
public class JUploadManager extends JFrame {
private JTextField addTextField = new JTextField(30);
public JUploadManager() {
super(":::Upload Manager:::");
setSize(600, 300);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
JMenuItem fileExitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X);
fileExitMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
fileMenu.add(fileExitMenuItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
// Set up add panel.
JPanel addPanel = new JPanel();
addPanel.add(addTextField);
JButton addButton = new JButton("Upload");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionAdd();
}
});
JButton findButton = new JButton("Find File");
findButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
actionFindfile();
}
});
addPanel.add(findButton);
addPanel.add(addButton);
JPanel buttonsPanel = new JPanel();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(addPanel, BorderLayout.NORTH);
}
private void actionAdd() {
File fileName = actionFindfile();
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/im","root","1234");
PreparedStatement ps = con.prepareStatement("insert into assignment values('henry','"+fileName.getName() + "',?)");
int fileLength = (int) fileName.length();
InputStream streamedFile = new FileInputStream(fileName);
ps.setBinaryStream(1, streamedFile, fileLength);
ps.executeUpdate();
ps.close();
streamedFile.close();
}
catch(Exception err)
{
System.err.println("Exception: " + err.getMessage());
}
}
public File actionFindfile() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES);
int result = fileChooser.showOpenDialog(this);
if(result == JFileChooser.CANCEL_OPTION)
System.exit(1);
File fileName = fileChooser.getSelectedFile();
if ((fileName == null) || (fileName.getName().equals("")))
{
JOptionPane.showMessageDialog(this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
return fileName;
}
// Run the Upload Manager.
public static void main(String[] args) {
JUploadManager manager = new JUploadManager();
manager.setVisible(true);
}
}