好的,所以由于某种原因,我在尝试连接到mySQL数据库时不断收到nullPointerException。我被告知要运行我的DbSetup文件以查看我得到的错误,这就是我得到的
POST
DbSetup的物理文件是:
Apr 04, 2018 9:10:37 PM my.pack.DbConnection getNewStatement
ERROR: Problem with DB connection:
SEVERE: ERROR: Problem with DB connection:
Exception in thread "main" java.lang.NullPointerException
at my.pack.collection.createItemTable(collection.java:29)
at my.pack.DbSetup.main(DbSetup.java:17)
C:\Users\Anastasia\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
由DbSetup
调用的集合文件public class DbSetup {
public static void main(String[] args) {
collection.createItemTable(); ***null error here***
itemBean testItem = new itemBean("itemCode", "song", "artist","category",
"description", "songURL", "rating");
collection.addItem(testItem);
UserDB uDB = new UserDB();
uDB.createUserTable();
User testUser = new User("userID", "firstName", "lastName", "email",
"address", "city", "state",
"zipcode", "country");
uDB.addUser(testUser);
}
}
DbConnection文件:这是数据库初始化的地方,但它没有连接
import java.util.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class collection{
public static void createItemTable(){
Statement statement = DbConnection.getNewStatement();
try {
statement.execute("CREATE TABLE item(" // **************error here** **************
+ "`itemCode` VARCHAR(255),`song` VARCHAR(255),"
+ "`artist` VARCHAR(255),`category` VARCHAR(255),"
+ "`description` VARCHAR(8000),`songURL` VARCHAR(255), `rating` VARCHAR(255)"
+ "PRIMARY KEY (itemCode))");
System.out.println("Created a new table: " + "ITEM");
} catch (SQLException se) {
if (se.getErrorCode() == 30000 && "X0Y32".equals(se.getSQLState())) {
// we got the expected exception when the table is already there
} else {
// if the error code or SQLState is different, we have an unexpected exception
System.out.println("ERROR: Could not create ITEM table: " + se);
}
}
}
public static itemBean addItem(String itemCode, String song, String artist,
String category, String description, String songURL, String rating){
Connection connection = DbConnection.getConnection();
PreparedStatement ps;
// insert the new row into the table
try {
ps = connection.prepareStatement("INSERT INTO item VALUES (?, ?, ?, ?, ?, ?, ?)");
ps.setString(1, itemCode);
ps.setString(2, song);
ps.setString(3, artist);
ps.setString(4, category);
ps.setString(5, description);
ps.setString(6, songURL);
ps.setString(7, song);
ps.executeUpdate();
} catch (SQLException se) {
if (((se.getErrorCode() == 30000) && ("23505".equals(se.getSQLState())))) {
System.out.println("ERROR: Could not insert record into ITEM; dup primary key: " + itemCode);
} else {
System.out.println("ERROR: Could not add row to ITEM table: " + itemCode + " " + se.getCause());
}
return null;
} catch (Exception e) {
System.out.println("ERROR: Could not add row to ITEM table: " + itemCode);
return null;
}
System.out.println("Added item to ITEM table: " + itemCode);
return new itemBean(itemCode, song, artist,category,
description, songURL, rating);
}
public static itemBean addItem(itemBean item){
Connection connection = DbConnection.getConnection();
PreparedStatement ps;
// insert the new row into the table
try {
ps = connection.prepareStatement("INSERT INTO ITEM VALUES (?, ?, ?, ?, ?, ?, ?)");
ps.setString(1, item.getItemCode());
ps.setString(2, item.getSong());
ps.setString(3, item.getCategory());
ps.setString(5, item.getDesc());
ps.setString(6, item.getSongURL());
ps.setString(7, item.getRating());
ps.executeUpdate();
} catch (SQLException se) {
if (((se.getErrorCode() == 30000) && ("23505".equals(se.getSQLState())))) {
System.out.println("ERROR: Could not insert record into ITEM; dup primary key: " + item.getItemCode());
} else {
System.out.println("ERROR: Could not add row to ITEM table: " + item.getItemCode() + " " + se.getCause());
}
return null;
} catch (Exception e) {
System.out.println("ERROR: Could not add row to ITEM table: " + item.getItemCode());
return null;
}
System.out.println("Added item to ITEM table: " + item.getItemCode());
// return the item object
return item;
}
public ArrayList<itemBean> getAllItem(){
ArrayList<itemBean> items = new ArrayList<itemBean>();
Statement statement = DbConnection.getNewStatement();
ResultSet resultSet = null;
String itemCode = "";
String song = "";
String artist = "";
String category = "";
String description = "";
String songURL = "";
String rating = "";
try {
resultSet = statement.executeQuery(
"SELECT itemCode, song, artist, category, description, songURL, rating FROM ITEM ORDER BY itemCode");
while (resultSet.next()) {
itemCode = resultSet.getString("itemCode");
song = resultSet.getString("song");
artist = resultSet.getString("artist");
category = resultSet.getString("category");
description = resultSet.getString("description");
songURL = resultSet.getString("songURL");
rating = resultSet.getString("rating");
itemBean item = new itemBean(itemCode, song, artist, category, description, songURL, rating);
items.add(item);
System.out.println("Found item in Item table: " + itemCode);
}
} catch (SQLException se) {
System.out.println("ERROR: Could not exicute SQL statement in: " + "ItemDB.getAllItems()");
System.out.println("ERROR: Could not exicute SQL statement: " + se);
return null;
}
return items;
}
public itemBean getItem(String pcode){
itemBean item = new itemBean();
item.setItemCode(pcode);
String query = "SELECT itemName, catalogCategory, description, imageUrl"
+ " FROM ITEM WHERE ITEM.itemCode = " + pcode;
Statement statement = DbConnection.getNewStatement();
ResultSet resultSet = null;
try {
resultSet = statement.executeQuery(query);
while (resultSet.next()) {
item.setSong(resultSet.getString("song"));
item.setSong(resultSet.getString("artist"));
item.setCategory(resultSet.getString("category"));
item.setDesc(resultSet.getString("description"));
item.setSongURL(resultSet.getString("songURL"));
item.setRating(resultSet.getString("rating"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return item;
}
}