Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL statements in Java

Posted on 2004-08-26
11
Medium Priority
?
310 Views
Last Modified: 2010-03-31
Hello.  I'm writing a test program that connects to my sample Northwood database that comes with SQL Server.  Here is the code, and I'll describe the error after:

import java.net.URL;
import java.sql.*;

class ODBC  
{
      public static Connection con;
      public static Statement stmt;
      public static String url = "jdbc:odbc:Northwind";
            
      public static void main (String args[])
      {
            try
            {
                  Driver d = (Driver)Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
                  Connection con = DriverManager.getConnection(url,
                        "sa", "!dbpass");
                  
                  String query = "select Password from Employees where LastName = 'Doe'";
                  ResultSet rs = stmt.executeQuery(query);
                  while(rs.next())
                  {
                        String s = rs.getString("password");
                        System.out.println(s);
                  }
            }
            catch(Exception e)
            {
                  e.printStackTrace();
            }
      }              
 }

I'm getting a NullPointerException at the line with ResultSet rs = stmt.executeQuery(query).  Any tips?

p.s.  on the line String s = rs.getString("password"), I'm pretty sure I can just put 0 instead of "password".  Is this correct if I'm just pulling one value?  
0
Comment
Question by:ike2010
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 2
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11901861
Check the connection string
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11901874
Doh - sorry - you haven't initialised the Statement
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 11901891
stmt = con.createStatement();
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 86

Expert Comment

by:CEHJ
ID: 11901900
You should also close the ResultSet and Statement
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11901903
>>I'm pretty sure I can just put 0 instead of "password".  

If you're fairly sure there's no password, then just do ""
0
 

Expert Comment

by:dsj_shock
ID: 11901919
That is because you have not initialized the Statement stmt yet. Use the following code in the try..catch region:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection con = DriverManager.getConnection(url, "sa", "!dbpass");
Statement stmt = con.createStatement(); // <--- this is the new line
...

By the way, 'Driver d = (Driver)' is not necessary when creating a new instance of the driver.

You cannot put 0 instead of the password, because the getConnection()-method does not accept an int as password, only a String.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11901968
>>That is because you have not initialized the Statement stmt yet

Please read the thread before posting - that was said five minutes earlier
0
 

Expert Comment

by:dsj_shock
ID: 11902005
Sry, it was not posted when I started typing :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11902033
OK :-)
0
 

Author Comment

by:ike2010
ID: 11902059
I can't believe I missed that.  Stupid me.  Thanks to both of you for your help.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11902133
8-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question