473,383 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to upload file to MySQL database in java application GUI?

14
I am facing problem to writting file to MySQL database in java application. Can show me a example code? Thanks!
Mar 19 '07 #1
21 7751
sicarie
4,677 Expert Mod 4TB
I am facing problem to writting file to MySQL database in java application. Can show me a example code? Thanks!
Have a look at this.

What part are you having trouble with?
Mar 20 '07 #2
ding
14
Have a look at this.

What part are you having trouble with?

I trouble in part reading file from FileChooser.
Mar 20 '07 #3
sicarie
4,677 Expert Mod 4TB
What trouble are you having with FileChoosers? Is there an error message, are you not getting the right directory....?
Mar 20 '07 #4
ding
14
What trouble are you having with FileChoosers? Is there an error message, are you not getting the right directory....?
I create two function. one is for looking file function while another is upload file function. I have error when sending filename from looking file function to upload file function.

The error code is:

incompatible types
found : java.io.File
required: java.lang.String
String fileName = actionFindfile();
^
1 error

Tool completed with exit code 1
Mar 20 '07 #5
sicarie
4,677 Expert Mod 4TB
I create two function. one is for looking file function while another is upload file function. I have error when sending filename from looking file function to upload file function.

The error code is:

incompatible types
found : java.io.File
required: java.lang.String
String fileName = actionFindfile();
^
1 error

Tool completed with exit code 1
Ok, so it's saying that actionFindfile() is not returning a String. Can you post the declaration of actionFindfile()?
Mar 20 '07 #6
ding
14
Ok, so it's saying that actionFindfile() is not returning a String. Can you post the declaration of actionFindfile()?
The declaration of actionFindfile is:

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;

}
Mar 20 '07 #7
sicarie
4,677 Expert Mod 4TB
The declaration of actionFindfile is:

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;

}
And there's your issue. You are returning a File, and trying to save it in a String. What do you want to do here? Turn the filename into a string, or just return the file and use it later?
Mar 20 '07 #8
ding
14
And there's your issue. You are returning a File, and trying to save it in a String. What do you want to do here? Turn the filename into a string, or just return the file and use it later?
i wan to return the value of the file and filename. And both will be calling in function upload file so tat file can be upload.
Mar 20 '07 #9
sicarie
4,677 Expert Mod 4TB
i wan to return the value of the file and filename. And both will be calling in function upload file so tat file can be upload.
So to get your code working, you can change your declaration to File (instead of String). That will remove that code, and then you can use the getFileName function you use in the actionFindfile to get the name - I'm pretty sure that returns a string.
Mar 20 '07 #10
ding
14
So to get your code working, you can change your declaration to File (instead of String). That will remove that code, and then you can use the getFileName function you use in the actionFindfile to get the name - I'm pretty sure that returns a string.
So how to get the value of file in function upload file?
Mar 20 '07 #11
sicarie
4,677 Expert Mod 4TB
So how to get the value of file in function upload file?
Not sure what you are asking. As long as you declare it as File, you can call that same function to get the filename, and then pass that variable you declared.
Mar 20 '07 #12
ding
14
Not sure what you are asking. As long as you declare it as File, you can call that same function to get the filename, and then pass that variable you declared.

I have error in function upload file
The code is :

private void actionAdd() {

Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/im","root","1234");

File fileName = actionFindfile();
PreparedStatement ps = con.prepareStatement("insert into assignment values('henry','"+fileName.toString() + "',?)");
int fileLength = (int) fileName.length();
InputStream streamedFile = new FileInputStream(fileName);
ps.setBinaryStream(1, streamedFile, fileLength);
ps.executeUpdate();
ps.close();
streamedFile.close();

}
Mar 20 '07 #13
sicarie
4,677 Expert Mod 4TB
I have error in function upload file
The code is :

private void actionAdd() {

Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/im","root","1234");

File fileName = actionFindfile();
PreparedStatement ps = con.prepareStatement("insert into assignment values('henry','"+fileName.toString() + "',?)");
int fileLength = (int) fileName.length();
InputStream streamedFile = new FileInputStream(fileName);
ps.setBinaryStream(1, streamedFile, fileLength);
ps.executeUpdate();
ps.close();
streamedFile.close();

}
Ummmm, I wouldn't recommend connecting as root, but besides that... did you associate the driver with the project, either in the classpath, or attach it as a library?
Mar 20 '07 #14
ding
14
Ummmm, I wouldn't recommend connecting as root, but besides that... did you associate the driver with the project, either in the classpath, or attach it as a library?
i already install JDBC driver and already successful connecting to database through other program.
Mar 20 '07 #15
ding
14
I have successfully run the code. However it call back the function FileChooser in function upload file. This is not what i want and i just want to pass the value.How to solve this?
Mar 20 '07 #16
r035198x
13,262 8TB
I have successfully run the code. However it call back the function FileChooser in function upload file. This is not what i want and i just want to pass the value.How to solve this?
Can you explain more? When is it calling back FileChooser?
Mar 20 '07 #17
ding
14
Can you explain more? When is it calling back FileChooser?
The function fileChooser is calling when i click the button "browse". When the file was chooser, i need to pass the value and name of the file to function upload so that when i click the button "upload" it can automatic get the value and save it to database. So how to call the value between two function?
Mar 20 '07 #18
r035198x
13,262 8TB
The function fileChooser is calling when i click the button "browse". When the file was chooser, i need to pass the value and name of the file to function upload so that when i click the button "upload" it can automatic get the value and save it to database. So how to call the value between two function?
Maybe if you post the full code we can understand better what you are trying to do.
Mar 20 '07 #19
ding
14
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);
}

}
Mar 20 '07 #20
r035198x
13,262 8TB
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);
}

}
You can use code tags next time when posting code like I've done here


Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.awt.BorderLayout;
  3. import java.awt.Component;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import javax.swing.*;
  7. import java.sql.*;
  8.  
  9. public class JUploadManager extends JFrame {
  10.  private JTextField addTextField = new JTextField(30);
  11.  public JUploadManager() {
  12.   super(":::Upload Manager:::");
  13.   setSize(600, 300);
  14.   addWindowListener(new WindowAdapter() {
  15.   public void windowClosing(WindowEvent e) {
  16.    System.exit(0);
  17.    }
  18.   });
  19.   JMenuBar menuBar = new JMenuBar();
  20.   JMenu fileMenu = new JMenu("File");
  21.   fileMenu.setMnemonic(KeyEvent.VK_F);
  22.   JMenuItem fileExitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X);
  23.   fileExitMenuItem.addActionListener(new ActionListener() {
  24.   public void actionPerformed(ActionEvent e) {
  25.  
  26.   }
  27.   });
  28.   fileMenu.add(fileExitMenuItem);
  29.   menuBar.add(fileMenu);
  30.   setJMenuBar(menuBar); 
  31.   // Set up add panel.
  32.   JPanel addPanel = new JPanel();
  33.   addPanel.add(addTextField);
  34.   JButton addButton = new JButton("Upload");
  35.   addButton.addActionListener(new ActionListener() {
  36.   public void actionPerformed(ActionEvent e) {
  37.   actionAdd();
  38.   }
  39.   });
  40.   JButton findButton = new JButton("Find File");
  41.   findButton.addActionListener(new ActionListener() {
  42.   public void actionPerformed(ActionEvent e) {
  43.   actionFindfile();
  44.   }
  45.   });
  46.   addPanel.add(findButton);
  47.   addPanel.add(addButton);
  48.   JPanel buttonsPanel = new JPanel();
  49.   getContentPane().setLayout(new BorderLayout());
  50.   getContentPane().add(addPanel, BorderLayout.NORTH);
  51.  
  52.  }
  53.  
  54.  private void actionAdd() {
  55.   File fileName = new File(addTextField.getText()); //changed here
  56.   try {
  57.    Class.forName("com.mysql.jdbc.Driver");
  58.    Connection con=DriverManager.getConnection("jdbc:mysql://localhost/im","root","1234");
  59.    PreparedStatement ps = con.prepareStatement("insert into assignment values('henry','"+fileName.getName() + "',?)");
  60.    int fileLength = (int) fileName.length();
  61.    InputStream streamedFile = new FileInputStream(fileName);
  62.    ps.setBinaryStream(1, streamedFile, fileLength);
  63.    ps.executeUpdate();
  64.    ps.close();
  65.    streamedFile.close();
  66.   }
  67.   catch(Exception err) {
  68.    System.err.println("Exception: " + err.getMessage());
  69.   }
  70.  }
  71.  public File actionFindfile() {
  72.   JFileChooser fileChooser = new JFileChooser();
  73.   fileChooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES);
  74.   int result = fileChooser.showOpenDialog(this);
  75.   if(result == JFileChooser.CANCEL_OPTION)
  76.   System.exit(1);
  77.   File fileName = fileChooser.getSelectedFile();
  78.   if ((fileName == null) || (fileName.getName().equals(""))) {
  79.    JOptionPane.showMessageDialog(this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE);
  80.       System.exit(1);
  81.   }
  82.   addTextField.setText(fileName.getName()); //added here
  83.   return fileName;
  84.     }
  85.  // Run the Upload Manager.
  86.  public static void main(String[] args) {
  87.   JUploadManager manager = new JUploadManager();
  88.   manager.setVisible(true);
  89.  }
  90. }
  91.  
  92.  
Mar 20 '07 #21
ding
14
You can use code tags next time when posting code like I've done here


Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.awt.BorderLayout;
  3. import java.awt.Component;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import javax.swing.*;
  7. import java.sql.*;
  8.  
  9. public class JUploadManager extends JFrame {
  10.  private JTextField addTextField = new JTextField(30);
  11.  public JUploadManager() {
  12.   super(":::Upload Manager:::");
  13.   setSize(600, 300);
  14.   addWindowListener(new WindowAdapter() {
  15.   public void windowClosing(WindowEvent e) {
  16.    System.exit(0);
  17.    }
  18.   });
  19.   JMenuBar menuBar = new JMenuBar();
  20.   JMenu fileMenu = new JMenu("File");
  21.   fileMenu.setMnemonic(KeyEvent.VK_F);
  22.   JMenuItem fileExitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X);
  23.   fileExitMenuItem.addActionListener(new ActionListener() {
  24.   public void actionPerformed(ActionEvent e) {
  25.  
  26.   }
  27.   });
  28.   fileMenu.add(fileExitMenuItem);
  29.   menuBar.add(fileMenu);
  30.   setJMenuBar(menuBar); 
  31.   // Set up add panel.
  32.   JPanel addPanel = new JPanel();
  33.   addPanel.add(addTextField);
  34.   JButton addButton = new JButton("Upload");
  35.   addButton.addActionListener(new ActionListener() {
  36.   public void actionPerformed(ActionEvent e) {
  37.   actionAdd();
  38.   }
  39.   });
  40.   JButton findButton = new JButton("Find File");
  41.   findButton.addActionListener(new ActionListener() {
  42.   public void actionPerformed(ActionEvent e) {
  43.   actionFindfile();
  44.   }
  45.   });
  46.   addPanel.add(findButton);
  47.   addPanel.add(addButton);
  48.   JPanel buttonsPanel = new JPanel();
  49.   getContentPane().setLayout(new BorderLayout());
  50.   getContentPane().add(addPanel, BorderLayout.NORTH);
  51.  
  52.  }
  53.  
  54.  private void actionAdd() {
  55.   File fileName = new File(addTextField.getText()); //changed here
  56.   try {
  57.    Class.forName("com.mysql.jdbc.Driver");
  58.    Connection con=DriverManager.getConnection("jdbc:mysql://localhost/im","root","1234");
  59.    PreparedStatement ps = con.prepareStatement("insert into assignment values('henry','"+fileName.getName() + "',?)");
  60.    int fileLength = (int) fileName.length();
  61.    InputStream streamedFile = new FileInputStream(fileName);
  62.    ps.setBinaryStream(1, streamedFile, fileLength);
  63.    ps.executeUpdate();
  64.    ps.close();
  65.    streamedFile.close();
  66.   }
  67.   catch(Exception err) {
  68.    System.err.println("Exception: " + err.getMessage());
  69.   }
  70.  }
  71.  public File actionFindfile() {
  72.   JFileChooser fileChooser = new JFileChooser();
  73.   fileChooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES);
  74.   int result = fileChooser.showOpenDialog(this);
  75.   if(result == JFileChooser.CANCEL_OPTION)
  76.   System.exit(1);
  77.   File fileName = fileChooser.getSelectedFile();
  78.   if ((fileName == null) || (fileName.getName().equals(""))) {
  79.    JOptionPane.showMessageDialog(this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE);
  80.       System.exit(1);
  81.   }
  82.   addTextField.setText(fileName.getName()); //added here
  83.   return fileName;
  84.     }
  85.  // Run the Upload Manager.
  86.  public static void main(String[] args) {
  87.   JUploadManager manager = new JUploadManager();
  88.   manager.setVisible(true);
  89.  }
  90. }
  91.  
  92.  
Thanks for helping. However now i facing problem for upload file to database.
The error code is:
Exception: BlobTest.java (The system cannot find the file specified)
Mar 20 '07 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: ree | last post by:
I have a tables for a database that I have entried data in to. I have used the table on my local comp. and used apache & mySql to test it out. Now I need to upload the table to the site. But...
3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
1
by: Luis | last post by:
Hi, I can this code, but I can't save the archive in the blob camp into my MySQL Database... ¿why? can you help me? I work with c# and MySQL in a ASP.NET application... Now, Theoretically in the...
2
by: shahram.shirazi | last post by:
Hi I am looking to develop an application that provides decision support through various graphs and charts using a mysql backend. My problem is between the data collection and data...
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
2
by: timberwolf2006 | last post by:
Hello Everyone. I'm new around here but i'll like some help from you guys. I have a VB6 application that connects to a MySQL 5 server and works just fine!!! The problem is that i was asked to...
0
by: cwho.work | last post by:
Hi! We are using apache ibatis with our MySQL 5.0 database (using innodb tables), in our web application running on Tomcat 5. Recently we started getting a number of errors relating to...
8
by: Ananthu | last post by:
Hi I have done all the codings part for connecting mysql with java in eclipse environment. Coding Part: import java.sql.Connection; import java.sql.DriverManager; public class...
2
by: hotflash | last post by:
Hi All, I found the best pure ASP code to upload a file to either server and/or MS Access Database. It works fine for me however, there is one thing that I don't like and have tried to fix but...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.