I am facing problem to writting file to MySQL database in java application. Can show me a example code? Thanks!
How to upload file to MySQL database in java application GUI?
Collapse
X
-
Have a look at this.Originally posted by dingI am facing problem to writting file to MySQL database in java application. Can show me a example code? Thanks!
What part are you having trouble with? -
Originally posted by sicarie
I trouble in part reading file from FileChooser.Comment
-
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.Originally posted by sicarieWhat trouble are you having with FileChoosers? Is there an error message, are you not getting the right directory....?
The error code is:
incompatible types
found : java.io.File
required: java.lang.Strin g
String fileName = actionFindfile( );
^
1 error
Tool completed with exit code 1Comment
-
Ok, so it's saying that actionFindfile( ) is not returning a String. Can you post the declaration of actionFindfile( )?Originally posted by dingI 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.Strin g
String fileName = actionFindfile( );
^
1 error
Tool completed with exit code 1Comment
-
The declaration of actionFindfile is:Originally posted by sicarieOk, so it's saying that actionFindfile( ) is not returning a String. Can you post the declaration of actionFindfile( )?
public File actionFindfile( ) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.set FileSelectionMo de( JFileChooser.FI LES_AND_DIRECTO RIES);
int result = fileChooser.sho wOpenDialog(thi s);
if(result == JFileChooser.CA NCEL_OPTION)
System.exit(1);
File fileName = fileChooser.get SelectedFile();
if ((fileName == null) || (fileName.getNa me().equals("") ))
{
JOptionPane.sho wMessageDialog( this, "Invalid File Name", "Invalid File Name", JOptionPane.ERR OR_MESSAGE);
System.exit(1);
}
return fileName;
}Comment
-
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?Originally posted by dingThe declaration of actionFindfile is:
public File actionFindfile( ) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.set FileSelectionMo de( JFileChooser.FI LES_AND_DIRECTO RIES);
int result = fileChooser.sho wOpenDialog(thi s);
if(result == JFileChooser.CA NCEL_OPTION)
System.exit(1);
File fileName = fileChooser.get SelectedFile();
if ((fileName == null) || (fileName.getNa me().equals("") ))
{
JOptionPane.sho wMessageDialog( this, "Invalid File Name", "Invalid File Name", JOptionPane.ERR OR_MESSAGE);
System.exit(1);
}
return fileName;
}Comment
-
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.Originally posted by sicarieAnd 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?Comment
-
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.Originally posted by dingi 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.Comment
-
So how to get the value of file in function upload file?Originally posted by sicarieSo 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.Comment
-
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.Originally posted by dingSo how to get the value of file in function upload file?Comment
-
Originally posted by sicarieNot 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=DriverManag er.getConnectio n("jdbc:mysql ://localhost/im","root","123 4");
File fileName = actionFindfile( );
PreparedStateme nt ps = con.prepareStat ement("insert into assignment values('henry', '"+fileName.toS tring() + "',?)");
int fileLength = (int) fileName.length ();
InputStream streamedFile = new FileInputStream (fileName);
ps.setBinaryStr eam(1, streamedFile, fileLength);
ps.executeUpdat e();
ps.close();
streamedFile.cl ose();
}Comment
-
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?Originally posted by dingI have error in function upload file
The code is :
private void actionAdd() {
Class.forName(" com.mysql.jdbc. Driver");
Connection con=DriverManag er.getConnectio n("jdbc:mysql ://localhost/im","root","123 4");
File fileName = actionFindfile( );
PreparedStateme nt ps = con.prepareStat ement("insert into assignment values('henry', '"+fileName.toS tring() + "',?)");
int fileLength = (int) fileName.length ();
InputStream streamedFile = new FileInputStream (fileName);
ps.setBinaryStr eam(1, streamedFile, fileLength);
ps.executeUpdat e();
ps.close();
streamedFile.cl ose();
}Comment
-
i already install JDBC driver and already successful connecting to database through other program.Originally posted by sicarieUmmmm, 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?Comment
Comment