Solved

SQL statements in Java

Posted on 2004-08-26
11
303 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
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 …

808 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