4 Replies - 7028 Views - Last Post: 10 June 2011 - 03:00 AM Rate Topic: -----

#1 drilli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 105
  • Joined: 26-May 09

Execute sql select query in java ?

Posted 10 June 2011 - 02:00 AM

Hi all.I want to ask something and i hope you can help me.

I am trying to execute an sql select query in java but i have problem with result.

here ise the code:


import java.sql.*;
public class Acces
{	public static void main(String[] args)
	{	int pin_kodi = 2043;
		try { 	
				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				Connection conn = DriverManager.getConnection("jdbc:odbc:databaza","","");
				Statement stm = conn.createStatement();
				String sql = "select*from Klienti where PIN_KODI = pin_kodi";
				ResultSet rs = stm.executeQuery(sql);
				while(rs.next())
						{	System.out.print(rs.getInt("PIN_KODI"));
							System.out.print("\t");
							System.out.print(rs.getDouble("Bilanci"));
							System.out.println();
						}
				
				rs.close();
				stm.close();
				conn.close();
			}
		catch (ClassNotFoundException e)
				{	e.printStackTrace();
				}
	   catch(SQLException e)
				{	e.printStackTrace();
				}
	}
}




when I Write : String sql = "select*from Klienti where PIN_KODI = 2043"; result is OK but when I Write
String sql = "select*from Klienti where PIN_KODI = pin_kodi"; the result is all fields not only the field with PIN_KODI = 2043

Klienti is a table with two columns : PIN_KODI and Bilanci.

Is This A Good Question/Topic? 0
  • +

Replies To: Execute sql select query in java ?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Execute sql select query in java ?

Posted 10 June 2011 - 02:07 AM

Sure that:

String sql = "select*from Klienti where PIN_KODI = pin_kodi";
should be, if pin_kodi is a litteral:
String sql = "select * from Klienti where PIN_KODI = 'pin_kodi';";

or if pin_kodi is a int variable

String sql = "select * from Klienti where PIN_KODI = " + pin_kodi + ";";

or if pin_kodi is a String variable

String sql = "select * from Klienti where PIN_KODI = '" + pin_kodi + "';";

Also some database engines are less leniant that other. The SQL standard says that an SQL statement should be terminated by a ";" so add it or use PreparedStament that add it for you.

This post has been edited by pbl: 10 June 2011 - 03:11 AM

Was This Post Helpful? 1
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1905
  • View blog
  • Posts: 3,949
  • Joined: 11-December 07

Re: Execute sql select query in java ?

Posted 10 June 2011 - 02:08 AM

Try this:

String sql = "select*from Klienti where PIN_KODI = " + pin_kodi;


I have been ninjaed :(
Was This Post Helpful? 1
  • +
  • -

#4 drilli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 105
  • Joined: 26-May 09

Re: Execute sql select query in java ?

Posted 10 June 2011 - 02:14 AM

View Postcfoley, on 10 June 2011 - 03:08 AM, said:

Try this:

String sql = "select*from Klienti where PIN_KODI = " + pin_kodi;


I have been ninjaed :(



Thank You ...Now it is OK.
Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1905
  • View blog
  • Posts: 3,949
  • Joined: 11-December 07

Re: Execute sql select query in java ?

Posted 10 June 2011 - 03:00 AM

Good stuff!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1