Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Problem whit insert pdf file into mySQL Database

Reply
Thread Tools

Problem whit insert pdf file into mySQL Database

 
 
sahm
Guest
Posts: n/a
 
      05-22-2010
Hi every one

I'm trying to insert PDF file in to mySQL Data Base but I keep receive
error and this is my code

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\\\
\
public static File file;
public static FileInputStream fis;

void get_Doc()
{
try
{
String fl = null;
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Select File");
FileFilter filter, filter2;
filter = new FileNameExtensionFilter("PDF", "PDF");
filter2 = new FileNameExtensionFilter("JPG", "JPG");
fc.addChoosableFileFilter(filter2);
fc.addChoosableFileFilter(filter);
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.setAcceptAllFileFilterUsed(false);
int returnVal = fc.showOpenDialog(this);

if (returnVal == JFileChooser.APPROVE_OPTION)
{
file = fc.getSelectedFile();
//fl = file.toString();
fis = new FileInputStream(file);

JOptionPane.showMessageDialog(this, fl.toString(),
"Information", JOptionPane.INFORMATION_MESSAGE);

}
else
{
JOptionPane.showMessageDialog(this, "Please select a
file" , "Error", JOptionPane.ERROR_MESSAGE);

}
this.dcument_Location_jTextField.setText(fl.toStri ng());
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e.toString(), "Error",
JOptionPane.ERROR_MESSAGE);
}
}
///////////////////////////////////////////////////////////////////////

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
void insert_Doc()
{
try
{
IPClass ipc = new IPClass();

Class.forName("com.mysql.jdbc.Driver");
Connection con;
con = DriverManager.getConnection("jdbc:mysql://" + ipc.ip
+ "/" + ipc.db,ipc.user,ipc.ps);
//Statement stat = con.createStatement();


String inserting_Data = "insert into document_archive ("
+
"Doc_ID, Doc_Type, Doc_Name,
Doc_Date, Doc_file, " +
"Doc_Bank_NO, Doc_Bank_Name,
Doc_Add_user ) " +
"values (?, ?, ?, ?, ?, ?, ?, ?)";


PreparedStatement ps = null;

ps = con.prepareStatement(inserting_Data);




ps.setInt(1,
Integer.valueOf(documen_NO_jTextField.getText()));
ps.setString(2,
String.valueOf(document_Type_jComboBox.getSelected Item()));
ps.setString(3, document_Name_jTextField.getText());
String today = getDocumentDate();
ps.setString(4, String.valueOf(today));
ps.setBinaryStream(5, fis, (int), file.length());
ps.setString(6,
String.valueOf(bankNOjComboBox.getSelectedItem())) ;
ps.setString(7, bankNOjTextField.getText());
ps.setString(8, userName.useName);
//ps.setBinaryStream(5, f1, (int) file.length());
//ps.setString(5, fd.st_FName);


ps.executeUpdate();
JOptionPane.showMessageDialog(this, "Document inserted
successfully", "Information", JOptionPane.INFORMATION_MESSAGE);
clean_Data();

}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e.toString(), "Error",
JOptionPane.ERROR_MESSAGE);
}
}
////////////////////////////////////

Best
Salim
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      05-22-2010
sahm wrote:
> Hi every one
>
> I'm trying to insert PDF file in to mySQL Data Base but I keep receive
> error and this is my code



What is the error you receive? A full stack trace and showing us which
line it is too would be helpful.

Otherwise, not much chance someone is just going to pick out the problem
by eyeball.
 
Reply With Quote
 
 
 
 
Jeff Higgins
Guest
Posts: n/a
 
      05-22-2010
On 5/22/2010 5:11 AM, sahm wrote:
> Hi every one
>
> I'm trying to insert PDF file in to mySQL Data Base but I keep receive
> error and this is my code
>


This fails to compile. There may be other issues.

class SQLTest {

String inserting_Data = "insert into document_archive ("
+
"Doc_ID, Doc_Type, Doc_Name,
Doc_Date, Doc_file, " +
"Doc_Bank_NO, Doc_Bank_Name,
Doc_Add_user ) " +
"values (?, ?, ?, ?, ?, ?, ?, ?)";



}
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      05-22-2010
sahm wrote:

> I'm trying to insert PDF file in to mySQL [sic] Data Base but I keep receive
> error and this is my code


markspace and Jeff Higgins have already pointed out that your code will not
compile and that you fail to reveal your actual problem.

But wait, there's more.

> public static File file;
> public static FileInputStream fis;


Sorry, 'public' members? And why 'static'?

> void get_Doc()
> {


(Unconventional indentation - should line up with 'void getDoc()' line.)

> try
> {
> String fl = null;


It's not often a good idea to initialize a variable to a value you never
intend to use. In the code you show us, doing so pushes a compiler error to a
runtime exception. You want to do the opposite.

> JFileChooser fc = new JFileChooser();
> fc.setDialogTitle("Select File");
> FileFilter filter, filter2;
> filter = new FileNameExtensionFilter("PDF", "PDF");
> filter2 = new FileNameExtensionFilter("JPG", "JPG");
> fc.addChoosableFileFilter(filter2);
> fc.addChoosableFileFilter(filter);
> fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
> fc.setAcceptAllFileFilterUsed(false);
> int returnVal = fc.showOpenDialog(this);
>
> if (returnVal == JFileChooser.APPROVE_OPTION)
> {
> file = fc.getSelectedFile();
> //fl = file.toString();
> fis = new FileInputStream(file);


Be very, very careful mixing long-running actions with the Event Dispatch
Thread. You should separate non-GUI actions to a different thread.

> JOptionPane.showMessageDialog(this, fl.toString(),


Invoking 'toString()' on a 'String' is wacky; invoking on value 'null' throws
'NullPointerException'.

> "Information", JOptionPane.INFORMATION_MESSAGE);
>
> }
> else
> {
> JOptionPane.showMessageDialog(this, "Please select a
> file" , "Error", JOptionPane.ERROR_MESSAGE);
>
> }
> this.dcument_Location_jTextField.setText(fl.toStri ng());


Invoking 'toString()' on a 'String' is wacky; invoking on value 'null' throws
'NullPointerException'.

Variable names (for non-constant or non-final variables) should not contain
underscores.

> }
> catch(Exception e)
> {
> JOptionPane.showMessageDialog(this, e.toString(), "Error",
> JOptionPane.ERROR_MESSAGE);
> }
> }
> ///////////////////////////////////////////////////////////////////////
>
> \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
> void insert_Doc()
> {
> try
> {
> IPClass ipc = new IPClass();
>
> Class.forName("com.mysql.jdbc.Driver");


You don't need to attempt to reload a class over and over and over and over
and over and ...

Once, in a static initializer, is enough.

> Connection con;
> con = DriverManager.getConnection("jdbc:mysql://" + ipc.ip
> + "/" + ipc.db,ipc.user,ipc.ps);


Why on separate lines?

> //Statement stat = con.createStatement();


Use 'PreparedStatement' instead of plain 'Statement' almost always.

> String inserting_Data = "insert into document_archive ("
> +
> "Doc_ID, Doc_Type, Doc_Name,
> Doc_Date, Doc_file, " +


Whoa, there, sport! That's some wide indentation you got there!

> "Doc_Bank_NO, Doc_Bank_Name,
> Doc_Add_user ) " +
> "values (?, ?, ?, ?, ?, ?, ?, ?)";
>
>
> PreparedStatement ps = null;


Why in the world do you bother initializing 'ps' to 'null'?

> ps = con.prepareStatement(inserting_Data);
>
> ps.setInt(1,
> Integer.valueOf(documen_NO_jTextField.getText()));
> ps.setString(2,
> String.valueOf(document_Type_jComboBox.getSelected Item()));
> ps.setString(3, document_Name_jTextField.getText());
> String today = getDocumentDate();
> ps.setString(4, String.valueOf(today));
> ps.setBinaryStream(5, fis, (int), file.length());
> ps.setString(6,
> String.valueOf(bankNOjComboBox.getSelectedItem())) ;
> ps.setString(7, bankNOjTextField.getText());
> ps.setString(8, userName.useName);
> //ps.setBinaryStream(5, f1, (int) file.length());
> //ps.setString(5, fd.st_FName);
>
>
> ps.executeUpdate();
> JOptionPane.showMessageDialog(this, "Document inserted
> successfully", "Information", JOptionPane.INFORMATION_MESSAGE);
> clean_Data();


Dude! Separate worker threads from the EDT!

This is covered in the Swing tutorial on java.sun.com. Read it.

> }
> catch(Exception e)
> {
> JOptionPane.showMessageDialog(this, e.toString(), "Error",
> JOptionPane.ERROR_MESSAGE);
> }
> }


--
Lew
 
Reply With Quote
 
sahm
Guest
Posts: n/a
 
      05-23-2010
On May 22, 6:27*pm, Lew <(E-Mail Removed)> wrote:
> sahm wrote:
> > I'm trying to insert PDF file in to mySQL [sic] Data Base but I keep receive
> > error and this is my code

>
> markspace and Jeff Higgins have already pointed out that your code will not
> compile and that you fail to reveal your actual problem.
>
> But wait, there's more.
>
> > public static File file;
> > public static FileInputStream fis;

>
> Sorry, 'public' members? *And why 'static'?
>
> > void get_Doc()
> > * * *{

>
> (Unconventional indentation - should line up with 'void getDoc()' line.)
>
> > * * * * *try
> > * * * * *{
> > * * * * * * *String fl = null;

>
> It's not often a good idea to initialize a variable to a value you never
> intend to use. *In the code you show us, doing so pushes a compiler error to a
> runtime exception. *You want to do the opposite.
>
>
>
> > * * * * * * *JFileChooser fc = new JFileChooser();
> > * * * * * * *fc.setDialogTitle("Select File");
> > * * * * * * *FileFilter filter, filter2;
> > * * * * * * *filter = new FileNameExtensionFilter("PDF", "PDF");
> > * * * * * * *filter2 = new FileNameExtensionFilter("JPG", "JPG");
> > * * * * * * *fc.addChoosableFileFilter(filter2);
> > * * * * * * *fc.addChoosableFileFilter(filter);
> > * * * * * * *fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
> > * * * * * * *fc.setAcceptAllFileFilterUsed(false);
> > * * * * * * *int returnVal = fc.showOpenDialog(this);

>
> > * * * * * * *if (returnVal == JFileChooser.APPROVE_OPTION)
> > * * * * * * *{
> > * * * * * * * * *file = fc.getSelectedFile();
> > * * * * * * * * *//fl = file.toString();
> > * * * * * * * * *fis = new FileInputStream(file);

>
> Be very, very careful mixing long-running actions with the Event Dispatch
> Thread. *You should separate non-GUI actions to a different thread.
>
> > * * * * * * * * *JOptionPane.showMessageDialog(this, fl.toString(),

>
> Invoking 'toString()' on a 'String' is wacky; invoking on value 'null' throws
> 'NullPointerException'.
>
> > "Information", JOptionPane.INFORMATION_MESSAGE);

>
> > * * * * * * *}
> > * * * * * * *else
> > * * * * * * *{
> > * * * * * * * * *JOptionPane.showMessageDialog(this, "Please select a
> > file" , "Error", JOptionPane.ERROR_MESSAGE);

>
> > * * * * * * *}
> > * * * * * * *this.dcument_Location_jTextField.setText(fl.toStri ng());

>
> Invoking 'toString()' on a 'String' is wacky; invoking on value 'null' throws
> 'NullPointerException'.
>
> Variable names (for non-constant or non-final variables) should not contain
> underscores.
>
>
>
> > * * * * *}
> > * * * * *catch(Exception e)
> > * * * * *{
> > * * * * * * *JOptionPane.showMessageDialog(this, e.toString(), "Error",
> > JOptionPane.ERROR_MESSAGE);
> > * * * * *}
> > * * *}
> > ///////////////////////////////////////////////////////////////////////

>
> > \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
> > void insert_Doc()
> > * * *{
> > * * * * *try
> > * * * * *{
> > * * * * * * IPClass ipc = new IPClass();

>
> > * * * * * * *Class.forName("com.mysql.jdbc.Driver");

>
> You don't need to attempt to reload a class over and over and over and over
> and over and ...
>
> Once, in a static initializer, is enough.
>
> > * * * * * * *Connection con;
> > * * * * * * *con = DriverManager.getConnection("jdbc:mysql://" + ipc.ip
> > + "/" + ipc.db,ipc.user,ipc.ps);

>
> Why on separate lines?
>
> > * * * * * * *//Statement stat = con.createStatement();

>
> Use 'PreparedStatement' instead of plain 'Statement' almost always.
>
> > * * * * * * * String inserting_Data = "insert into document_archive ("
> > +
> > * * * * * * * * * * * * * * * * * * *"Doc_ID, Doc_Type, Doc_Name,
> > Doc_Date, Doc_file, " +

>
> Whoa, there, sport! *That's some wide indentation you got there!
>
> > * * * * * * * * * * * * * * * * * * *"Doc_Bank_NO, Doc_Bank_Name,
> > Doc_Add_user ) " +
> > * * * * * * * * * * * * * * * * * * *"values (?, ?, ?, ?, ?, ?, ?, ?)";

>
> > * * * * * * *PreparedStatement ps = null;

>
> Why in the world do you bother initializing 'ps' to 'null'?
>
>
>
> > * * * * * * *ps = con.prepareStatement(inserting_Data);

>
> > * * * * * * *ps.setInt(1,
> > Integer.valueOf(documen_NO_jTextField.getText()));
> > * * * * * * *ps.setString(2,
> > String.valueOf(document_Type_jComboBox.getSelected Item()));
> > * * * * * * *ps.setString(3, document_Name_jTextField.getText());
> > * * * * * * *String today = getDocumentDate();
> > * * * * * * *ps.setString(4, String.valueOf(today));
> > * * * * * * *ps.setBinaryStream(5, fis, (int), file.length());
> > * * * * * * *ps.setString(6,
> > String.valueOf(bankNOjComboBox.getSelectedItem())) ;
> > * * * * * * *ps.setString(7, bankNOjTextField.getText());
> > * * * * * * *ps.setString(8, userName.useName);
> > * * * * * * *//ps.setBinaryStream(5, f1, (int) file.length());
> > * * * * * * *//ps.setString(5, fd.st_FName);

>
> > * * * * * * *ps.executeUpdate();
> > * * * * * * *JOptionPane.showMessageDialog(this, "Document inserted
> > successfully", "Information", JOptionPane.INFORMATION_MESSAGE);
> > * * * * * * *clean_Data();

>
> Dude! *Separate worker threads from the EDT!
>
> This is covered in the Swing tutorial on java.sun.com. *Read it.
>
> > * * * * *}
> > * * * * *catch(Exception e)
> > * * * * *{
> > * * * * * * *JOptionPane.showMessageDialog(this, e.toString(), "Error",
> > JOptionPane.ERROR_MESSAGE);
> > * * * * *}
> > * * *}

>
> --
> Lew


Thank you every body
I solve the problem
Best
Salim
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How to insert PDF file in to MySql and read it from MySql toJAVA App Jeffrey H. Coffield Java 1 07-24-2009 12:29 AM
Re: How to insert PDF file in to MySql and read it from MySql to JAVA App Roedy Green Java 0 07-23-2009 05:30 PM
PDF::Writer, create pdf and insert in other pdf file. Ricardo Pog Ruby 1 03-26-2008 08:24 PM
Parsing XML into PHP to insert into a MySQL DB impulse() XML 0 10-13-2006 03:05 PM
How Read Database whit datos in Format RTF Edison ASP .Net 0 10-02-2003 08:51 AM



Advertisments