Solved

SQL statements in Java

Posted on 2004-08-26
11
301 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

743 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

15 Experts available now in Live!

Get 1:1 Help Now