Solved

SQL statements in Java

Posted on 2004-08-26
11
302 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java  and programming certification ? 4 86
Java DateChooser? 3 29
javap bin 2 24
java stored proc example 9 18
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

929 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now