当我运行程序时,有一个与createCarMaker关联的空指针异常。我检查了数据库连接,看起来一切都还好。我可以请一些帮助排除故障。
CarDBHelper(){
try
{
//Establish a driver
Class.forName("com.mysql.jdbc.Driver").newInstance();
//Connect to the database
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
catch (Exception e)
{
System.err.println("Unable to load MySQL driver.");
e.printStackTrace();
}
}
/**
* Creates the tables carMaker and Vehicle with various parameters.
*
*/
public void createTables(){
try {
Statement stmt=conn.createStatement();
String createcarMaker = "CREATE TABLE carMaker " +
"(makerID INT AUTO_INCREMENT NOT NULL, " +
" makerName VARCHAR(50), " +
" PRIMARY KEY ( makerID ))";
statement.executeUpdate(createcarMaker);
String createVehicle = "CREATE TABLE Vehicle " +
"(id INT AUTO_INCREMENT NOT NULL, " +
" year INT, " +
" make INT, " +
" model VARCHAR(50), " +
" FOREIGN KEY (make) REFERENCES carMaker(makerID)," +
" PRIMARY KEY ( id ))";
statement.executeUpdate(createVehicle);
} catch (SQLException e) {
e.printStackTrace();
}
答案 0 :(得分:0)
我认为你在'陈述中使用了错误的声明变量声明'和' stmt'。
此外,请将堆栈跟踪放在此处。
答案 1 :(得分:0)
您正在使用未声明的变量。你声明:
Statement stmt=conn.createStatement();
但是参考:
statement.executeUpdate(createcarMaker);
stmt!=声明。