我正在尝试用Java实现一个单例DB连接。我认为它基本上是正确的,但是当我尝试在其他类中使用它时,我一直得到java.lang.NullPointerExceptions。
我所得到的错误发生在我的FileManipulation类中,在PreparedStatement行中,我执行conn.PreparedStatement(查询);我认为conn被传递为null,但是我不知道为什么或者如何修复它。
这是DB代码:
public class dbConnection {
static String newLine = System.getProperty("line.separator");
volatile static dbConnection s;
public static Connection conn;
public static Statement stmt = null;
private dbConnection() throws SQLException {
// create single instance of DB connection
Properties connectionProps = new Properties();
connectionProps.put("user", "root");
connectionProps.put("password", "password");
// port & db name
conn = DriverManager.getConnection("jdbc:mysql://"
+ "localhost" + ":" + "3306" + "/" + "DBNAME",
connectionProps);
}
public Connection getConnection() throws SQLException {
return conn;
}
public static dbConnection getInstance() throws SQLException {
if (s == null) {
synchronized(dbConnection.class) {
if (s == null) {
s = new dbConnection();
System.out.print("First DB connection created." + newLine);
}
}
}
conn = s.getConnection();
return s;
}
}下面是我在课堂上使用它的方法:
public class FileManipulation {
final static String authoritiesTable = "Authorities";
static Connection conn = null;
static Statement stmt = null;
static BufferedReader reader = null;
static String position;
static String username;
static String password;
public static void authorities_check() {
try {
Scanner scan = new Scanner( System.in );
boolean authentication = false;
while (authentication == false) {
//get user input
System.out.print("Enter your position: ");
position = scan.nextLine();
System.out.print("Enter your username: ");
username = scan.nextLine();
System.out.print("Enter your password: ");
password = scan.nextLine();
if (position != null && username != null && password != null) {
authentication = true;
}
}
String query = "SELECT * FROM " + authoritiesTable + " WHERE position = ?" + " AND " + "username = ?" + " AND " + "password = ?";
PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(query);
pstmt.setString(1, position);
pstmt.setString(2, username);
pstmt.setString(3, password);
ResultSet rs = pstmt.executeQuery();
//if user with position, username, and password exists
if (rs.next()) {
//let them edit the file
System.out.println("User authenticated...");
}
else {
System.out.println("Error: Could not authenticate!");
}
scan.close();
}
catch(SQLException se) {
se.printStackTrace();
}
finally {
try {
if(stmt != null || conn != null) {
conn.close();
}
}
catch(SQLException se) {
se.printStackTrace();
}
}
}
public static void main (String[] args) throws SQLException {
dbConnection connect = dbConnection.getInstance();
authorities_check();
}
}发布于 2015-03-09 20:19:46
用于查询的connection不是您在dbConnection类中定义的连接,请执行以下操作:
public static void main (String[] args) throws SQLException {
dbConnection connect = dbConnection.getInstance();
conn=connect.getConnection();
authorities_check();
}发布于 2015-03-09 20:16:43
您必须使用来自您的getConnection()的dbConnection。您正在做dbConnection connect = dbConnection.getInstance();,但没有使用它。相反,您使用的是一个从未初始化的实例变量。
https://stackoverflow.com/questions/28950830
复制相似问题