How to get a photo into mySql with java

I wonder how I can get a photo from a client-macine through web, into a mySql database. Communications too the web-user is going through a Servlet who is runnin in a Server, where the mySql database are too.

I haved tryed this code-sequense but it dosent seems to work

public void acceptInput( String overskrift, String ingress, String artikkel, String forfatter,

String bildeTekst, String picture )throws SQLException{

try{

String ny = "";

PreparedStatement pstmt = null;

File file = null;

if( !picture.equals( "" ) ){

file = new File( picture );

if( file.isFile() ){

ny = "insert into NEWS values( NULL, ?, ?, ?, ?, ?, NULL,'"

+year+ "-" + month+ "-" +day+ "')";

System.out.println( "Er if if testen som tester om det er en fil" + file.length() );

FileInputStream fileIn = new FileInputStream( file );

pstmt.setBinaryStream(6, fileIn, (int)file.length() );

fileIn.close();

}//End of if( file.isFile() )

}//End of if( !picture.equals( "" ) )

else if( picture.equals( "" ) || !file.isFile() ){

ny = "insert into NEWS values( NULL, ?, ?, ?, ?, ?, NULL,'"

+year+ "-" + month+ "-" +day+ "')";

}//End of else if(....)

pstmt = dbKobling.prepareStatement( ny );

pstmt.setString(1 , overskrift );

pstmt.setString(2, ingress );

pstmt.setString(3, artikkel );

pstmt.setString(4, forfatter );

pstmt.setString(5, bildeTekst );

pstmt.executeUpdate();

pstmt.close();

}//End of try-block

catch( Exception e ){

JOptionPane.showMessageDialog(null, "Class:News.java Metode:acceptInput(...) " + e.getMessage() +

".", "Det har skjedd en feil !!!", 1);

}//End of catch( Exeption e )

}//End of method acceptInput

I hope somone can help me :-)

paulsep

[1890 byte] By [paulsep] at [2007-9-26 18:56:09]
# 1

FileInputStream fileIn = new FileInputStream( file );

pstmt.setBinaryStream(6, fileIn, (int)file.length() );

fileIn.close();

Almost correct! Just don't close the stream before the executeUpdate() method tries to use it.

DrClap at 2007-7-3 3:53:36 > top of Java-index,Archived Forums,Java Programming...