Results 1 to 6 of 6
- 06-16-2012, 01:30 PM #1
Member
- Join Date
- May 2012
- Posts
- 10
- Rep Power
- 0
Insert photos into SQL database using java
Hello!
I am struggling to insert pictures into "Books" table in column "photo" from a SQL server database, using the following code:
The following error pops out: "Exception in thread "main" java.lang.NullPointerExceptionJava Code:package insertPhotos; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import persistency.Database; public class Pictures { static String location="xxxxxxxxx"; static String password="xxx"; static String username="xxx"; public static void main(String[] args) throws Exception, IOException, SQLException { Connection con = DriverManager.getConnection(location,username,password); con.setAutoCommit(false); String INSERT_PICTURE="INSERT INTO Books(ISBN, photo) values(?,?)"; FileInputStream fis = null; PreparedStatement ps = null; try { con.setAutoCommit(false); File file = new File("cien_anos_de_soledad.jpg"); fis = new FileInputStream(file); ps = con.prepareStatement(INSERT_PICTURE); ps.setString(1, "002356"); ps.setBinaryStream(2, fis, (int) file.length()); ps.executeUpdate(); con.commit(); } finally { ps.close(); fis.close(); } } }
at insertPhotos.Pictures.main(Pictures.java:40)"
I really don't understand what is wrong, as I am new to java programming. Any suggestions?
Thank you :)
-
Re: Insert photos into SQL database using java
The code line number in your posted code may not correspond to the line number in your actual code. Please clarify for us which line is causing your exception.
- 06-16-2012, 02:20 PM #3
Member
- Join Date
- May 2012
- Posts
- 10
- Rep Power
- 0
Re: Insert photos into SQL database using java
It is the same line - ps.close(); as I copy pasted all.
-
Re: Insert photos into SQL database using java
- 06-16-2012, 02:35 PM #5
Member
- Join Date
- May 2012
- Posts
- 10
- Rep Power
- 0
Re: Insert photos into SQL database using java
Well...again I am getting a more huge error:
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint 'PK_Books'. Cannot insert duplicate key in object 'dbo.Books'.
at com.microsoft.sqlserver.jdbc.SQLServerException.ma keFromDatabaseError(SQLServerException.java:216)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.ge tNextResult(SQLServerStatement.java:1515)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStat ement.doExecutePreparedStatement(SQLServerPrepared Statement.java:404)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStat ement$PrepStmtExecCmd.doExecute(SQLServerPreparedS tatement.java:350)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IO Buffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.e xecuteCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.ex ecuteCommand(SQLServerStatement.java:180)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.ex ecuteStatement(SQLServerStatement.java:155)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStat ement.executeUpdate(SQLServerPreparedStatement.jav a:314)
at insertPhotos.Pictures.main(Pictures.java:35)
Line 35 is ps.executeUpdate();
I also changed this line File file = new File("D:\\Java work\\Ultimate_project_library\\src\\cien_anos_de_ soledad.jpg");
:( :( :(
- 06-16-2012, 08:13 PM #6
Member
- Join Date
- May 2012
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
How to read from text file and insert into database using java
By ryanalhakim in forum New To JavaReplies: 4Last Post: 05-27-2011, 04:13 PM -
Attach Photos in SWT to database
By ashin in forum SWT / JFaceReplies: 0Last Post: 08-09-2010, 06:48 AM -
java insert data into database problem
By chyeeqi in forum NetBeansReplies: 3Last Post: 03-29-2010, 01:36 AM -
How to insert large data into database using one insert query
By sandeepsai39 in forum New To JavaReplies: 3Last Post: 02-28-2009, 09:17 AM -
How to insert java Object in oracle database
By Thilkumar82 in forum Advanced JavaReplies: 9Last Post: 08-13-2008, 11:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks