cancel
Showing results for 
Search instead for 
Did you mean: 

writing picture into the MySQL database problem

Former Member
0 Kudos

Hi, Im following this blog https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/5892. [original link is broken] The only change I did is connection method since I couldnt use same as in the blog. Code from NW pov is clear, I think that photo uploading which I took after "Donwloading and Uploading Files" tutorial works fine. After deploying, uploading the photo and pressing "save" button I get " java.lang.NullPointerException ". No, if we assume that there is uploaded photo in "Resource" context what can couse that error? Ill be gratefull for every idea/suggestion. Regards, Balmer.


try {		
		{
			String userName = "user";
			String password = "pswd";
			String url = "jdbc:mysql://dbadress";
			Class.forName ("com.mysql.jdbc.Driver").newInstance();
			Connection con = DriverManager.getConnection (url, userName, password);
			System.out.println ("Database connection established");	   
			
		   	//Context ctx = new InitialContext();
			//DataSource ds = (DataSource) ctx.lookup("jdbc/dataSourceName");			 		
		 	//Connection con=ds.getConnection();				
		 
		   Statement  st=con.createStatement();
		   PreparedStatement pSt=con.prepareStatement( "insert into photo values(?)");
		   	
			IWDResource res=wdContext.currentContextElement().getResource() ;
			InputStream in = res.read(false);
			ByteArrayOutputStream bOut = new ByteArrayOutputStream();
			int length;
			byte[] part = new byte[10 * 1024];
			while ((length = in.read(part)) != -1) {
				 bOut.write(part, 0, length);
			}
			in.close();
		   pSt.setBytes( 1,bOut.toByteArray());
		   pSt.execute() ;
		   bOut.close();
		  wdComponentAPI .getMessageManager().reportSuccess("sukces");
		}
			catch(Exception e)
			{
				wdComponentAPI.getMessageManager() .reportWarning( e.toString() );
			}

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Balmer,

Use Following code to register your driver.

DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());

Then call the connection and write proper connection string.

Regards.

Deepak

Former Member
0 Kudos

but my database is "normal" MySQL db, not MS, anyway I can try.

What do you mean by "proper connection string"?

Thanks, Regards

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Balmer,

Where are u getting Null Pointer Error?

While connecting to DataBase or while executing the statement??

-

Shyam

Former Member
0 Kudos

When I hash writing photo into db part of code theres no NullPointer error, but no "Database connection established" as well. So I really dont know if it is connected or not. Unhashing writing part generates Nullpointer error however that part of code is copied from blog so I dont thing it doesnt work properly. Only thing I changed slightly comparing to blog is connection. Thanks for answers, Balmer.

Former Member
0 Kudos

Hi,

try only with this code.. So that u can figure out whether problem in Database connection or not.

try {

String userName = "user";

String password = "pswd";

String url = "jdbc:mysql://dbadress";

Class.forName ("com.mysql.jdbc.Driver").newInstance();

Connection con = DriverManager.getConnection (url, userName, password);

wdComponentAPI.getMessageManager() .reportSuccess

("Database connection established");

} catch(Exception e) {

wdComponentAPI.getMessageManager() .reportWarning( e.toString() );

}

-


Shyam.

Former Member
0 Kudos

that piece of code works properly, I get "Connection to database established". Once I add writing picture part from the blog it gives both "Connection to database established" and "java.lang.NullPointerException" errors. Picture was not written into db.

Former Member
0 Kudos

Hi,

try to get photo resource like this..

IWDResource resource = null;

byte[] display = new byte[

wdContext . currentContextElement(). getPhotoResource().read(true).available() ];

and try to set photo in prepare

pSt.setBytes( 1,display);

--

Shyam

Edited by: Shyam on Mar 7, 2008 4:30 PM

Former Member
0 Kudos

Thank you Shyam for answers, I really appreciate it. That last piece of code didnt change a thing. Still those two messages appear, no picture was written. Something must be wrong, problem is that it comes from blog and it should work but it doesnt and that makes me worried... Not much ideas left.

Do you by any chance have working tutorial or example project or whatever that shows how things (strings from inputfields, strings from DropBoxes, booleans from checkboxes etc) are written into MySQL database? Enything that will lighten up the issue. Regards, Balmer