我已经写了代码,现在我得到了一个nullpointer异常,请你能帮助我,或者只是指出我正确的方向
我仍然是一个大型的java程序员,这是我的最后一个任务,因为tommorow
import java.sql.*;
import java.io.*;
import java.util.*;
public class QueryDB_N
{
private Connection con = null;
String userID,firstName,lastName,name,symbol;
public QueryDB_N() throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:StockTracker";
Connection con = DriverManager.getConnection (url);
Statement stmt = con.createStatement();
PreparedStatement pStmt = null;
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter User ID: ");
userID = dataIn.readLine();
System.out.println("\nStock holdings for User: " + userID);
System.out.println("Stock - Description");
System.out.println("------- ----------------------------");
pStmt = con.prepareStatement("SELECT FROM (Users INNER JOIN UserStocks ON Users.userID = UserStocks.userID) INNER JOIN Stocks ON UserStocks.symbol = Stocks.symbol");
pStmt.setString(1, userID);
pStmt.setString(2, symbol);
System.out.println("\n" + userID + "\t" + firstName + "\t" + lastName + "\t");
System.out.println("\t\t" + symbol + "\t" + name);
System.out.println("\n\nReport done.");
}
public static void main(String[] args)throws Exception
{
QueryDB_N q = new QueryDB_N();
}
}
我已经重新编写了代码,现在它将值返回为null而不是实际的符号和描述,我真的需要你的帮助,我已经尝试了所有我无法让它正常工作
/*
Chapter 11: the QueryDB_N Class
Programmer: Jacques Duvenage
Date: November, 2014
Program Name: QueryDB_N.java
*/
import java.sql.*;
import java.io.*;
import java.util.*;
public class QueryDB_N
{
private Connection con = null;
String userID,firstName,lastName,name,symbol;
public QueryDB_N() throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:StockTracker";
Connection con = DriverManager.getConnection (url);
Statement stmt = con.createStatement();
PreparedStatement pStmt = null;
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter User ID: ");
userID = dataIn.readLine();
System.out.println("\nStock holdings for User: " + userID);
System.out.println("Stock - Description");
System.out.println("------- ----------------------------");
pStmt = con.prepareStatement("SELECT * FROM (Users INNER JOIN UserStocks ON Users.? = UserStocks.?) INNER JOIN Stocks ON UserStocks.symbol = Stocks.symbol");
pStmt.setString(1, userID);
pStmt.setString(2, symbol);
System.out.println(symbol + "\t" + name);
System.out.println("\n\nReport done.");
}
public static void main(String[] args)throws Exception
{
QueryDB_N q = new QueryDB_N();
}
}
答案 0 :(得分:0)
这样只有你在同一个PS语句中运行2个查询。这是错误。
public static void getUserID(Connection con, PreparedStatement pStmt, String userID, String stockSymbol) throws Exception
{
try
{
pStmt = con.prepareStatement("INSERT INTO UserStocks VALUES(?,?)");
pStmt.setString(1, userID);
pStmt.setString(2, stockSymbol);
pStmt.executeQuery();
pstmt.close();
pStmt = con.prepareStatement("INSERT INTO Stocks VALUES(?,?)");
pStmt.setString(1, userID);
pStmt.setString(2, stockSymbol);
pStmt.executeQuery();
pStmt.close();
}
catch(Exception e)
{
}