Thursday, 15 January 2015

java - NullPointerException when run executeQuery(sql) -



java - NullPointerException when run executeQuery(sql) -

i have simple query in java run in sql server 2008. when reaches to

rs = stmt.executequery(sql); gives me java.lang.nullpointerexception

1-i utilize jtds driver connect code database.

2-when execute query straight in database works.

3-to create code short , easy understand omitted try-catch

public class databases { private connection link; private java.sql.statement stmt; public resultset rs; public databases() { class.forname("net.sourceforge.jtds.jdbc.driver"); string connectionurl = "jdbc:jtds:sqlserver://localhost:1433;databasename=db;integratedsecurity=true"; connection link = drivermanager.getconnection(connectionurl); } public resultset select(string sql) { rs = stmt.executequery(sql); homecoming rs; } } public static void main(string[] args) { databases s=new databases(); string sql="select * [db].[dbo].[quantities] "; resultset rs=s.select(sql); }

you need instantiate stmt somewhere (in constructor or within select function)

you can move stmt field variable of select function.

public resultset select(string sql) { statement stmt = link.createstatement(); rs = stmt.executequery(sql); homecoming rs; }

java sql-server-2008 nullpointerexception jtds executequery

No comments:

Post a Comment