Solved

SQL statements in Java

Posted on 2004-08-26
11
304 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
  • 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 250 total points
ID: 11901891
stmt = con.createStatement();
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web services creation SOAP vs REST 5 57
pagenation logic how it is working in my code 1 55
CSV file parsing thru Java 13 34
SequenceInputStream example 3 19
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

828 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