【发布时间】:2016-11-22 13:50:03
【问题描述】:
我正在完成一个 java 中级项目,并且在运行我的测试类时遇到了运行时错误。我已经提供了我的 DB_SETUP、DB_MANAGER 和我的 TEST_INSTRUMENT 类的代码。该错误与 JDBC 连接有关。任何帮助,将不胜感激。谢谢 !
DB_MANAGER
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DB_MANAGER {
Logger logger = Logger.getLogger(DB_MANAGER.class.getName());
void loadJDBCDriver() {
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
} catch (ClassNotFoundException ex) {
logger.log(Level.SEVERE, null, ex);
}
}
void checkConnection() {
loadJDBCDriver();
Connection dbConnection = null;
String strUrl = "jdbc:derby:billsdb;create=true";
try {
dbConnection = DriverManager.getConnection(strUrl);
} catch (SQLException sqle) {
logger.log(Level.SEVERE, null, sqle.getStackTrace());
}
}
Connection getConnection() {
loadJDBCDriver();
Connection dbConnection = null;
String strUrl = "jdbc:derby:billsdb;create=true";
try {
dbConnection = DriverManager.getConnection(strUrl);
} catch (SQLException sqle) {
logger.log(Level.SEVERE, null, sqle.getStackTrace());
}
return dbConnection;
}
}
DB_SETUP
import java.io.File;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DB_SETUP {
Logger logger = Logger.getLogger(DB_MANAGER.class.getName());
void createTables() {
DB_MANAGER dmbgr = new DB_MANAGER();
Connection con = dmbgr.getConnection();
executeSqlScript(con, new File("sql\\createdb.txt"));
}
void insertSetupData() {
DB_MANAGER dmbgr = new DB_MANAGER();
Connection con = dmbgr.getConnection();
executeSqlScript(con, new File("sql\\insertdata.txt"));
}
public void showData() {
Statement stmt;
DB_MANAGER dmbgr = new DB_MANAGER();
Connection con = dmbgr.getConnection();
/** Prints all data from the USERDETAILS Table **/
try {
stmt = con.createStatement();
ResultSet results = stmt.executeQuery("select * from USERDETAILS");
System.out.println("\n-------------------------------------------------");
while (results.next()) {
int id = results.getInt(1);
String userName = results.getString(2);
String fName = results.getString(3);
String lName = results.getString(4);
logger.info(id + "\t\t" + userName + "\t\t" + fName + "\t\t" + lName);
}
results.close();
stmt.close();
} catch (SQLException sqlExcept) {
logger.log(Level.SEVERE, null,sqlExcept);
}
/** Prints all data from the INSTRUMENTS Table **/
try {
stmt = con.createStatement();
ResultSet results = stmt.executeQuery("select * from INSTRUMENTS");
System.out.println("\n-------------------------------------------------");
while (results.next()) {
int id = results.getInt(1);
String type = results.getString(2);
String name = results.getString(3);
logger.info(id + "\t\t" + type + "\t\t" + name );
}
results.close();
stmt.close();
} catch (SQLException sqlExcept) {
logger.log(Level.SEVERE, null,sqlExcept);
}
/** Prints all data from the VOTE Table **/
try {
stmt = con.createStatement();
ResultSet results = stmt.executeQuery("select * from VOTE");
System.out.println("\n-------------------------------------------------");
while (results.next()) {
int id = results.getInt(1);
String opendate = results.getString(2);
String closedate = results.getString(3);
logger.info(id + "\t\t" + opendate + "\t\t" + closedate);
}
results.close();
stmt.close();
} catch (SQLException sqlExcept) {
logger.log(Level.SEVERE, null,sqlExcept);
}
/** Prints all data from the VOTEINST Table **/
try {
stmt = con.createStatement();
ResultSet results = stmt.executeQuery("select * from VOTEINST");
System.out.println("\n-------------------------------------------------");
while (results.next()) {
int id = results.getInt(1);
int voteid = results.getInt(2);
int instrid = results.getInt(3);
logger.info(id + "\t\t" + voteid + "\t\t" + instrid);
}
results.close();
stmt.close();
} catch (SQLException sqlExcept) {
logger.log(Level.SEVERE, null,sqlExcept);
}
/** Prints all data from the BANKDETAILS Table **/
try {
stmt = con.createStatement();
ResultSet results = stmt.executeQuery("select * from BANKDETAILS");
System.out.println("\n-------------------------------------------------");
while (results.next()) {
int accnum = results.getInt(1);
String name = results.getString(2);
int branch = results.getInt(3);
String collect = results.getString(4);
int userid = results.getInt(5);
logger.info(accnum + "\t\t" + name + "\t\t" + branch + "\t\t" + collect + "\t\t" + userid);
}
results.close();
stmt.close();
} catch (SQLException sqlExcept) {
logger.log(Level.SEVERE, null,sqlExcept);
}
/** Prints all data from the USERVOTE Table **/
try {
stmt = con.createStatement();
ResultSet results = stmt.executeQuery("select * from USERVOTE");
System.out.println("\n-------------------------------------------------");
while (results.next()) {
int id = results.getInt(1);
int userid = results.getInt(2);
int voteinstid = results.getInt(3);
float percent = results.getFloat(4);
logger.info(id + "\t\t" + userid + "\t\t" + voteinstid + "\t\t" + percent );
}
results.close();
stmt.close();
} catch (SQLException sqlExcept) {
logger.log(Level.SEVERE, null,sqlExcept);
}
/** Prints all data from the VOTERESULT Table **/
try {
stmt = con.createStatement();
ResultSet results = stmt.executeQuery("select * from VOTERESULT");
System.out.println("\n-------------------------------------------------");
while (results.next()) {
int id = results.getInt(1);
int voteinstid = results.getInt(2);
float avgpercent = results.getFloat(3);
logger.info(id + "\t\t" + voteinstid + "\t\t" + voteinstid + "\t\t" + avgpercent);
}
results.close();
stmt.close();
} catch (SQLException sqlExcept) {
logger.log(Level.SEVERE, null,sqlExcept);
}
/** Prints all data from the MKTHISTORY Table **/
try {
stmt = con.createStatement();
ResultSet results = stmt.executeQuery("select * from MKTHISTORY");
System.out.println("\n-------------------------------------------------");
while (results.next()) {
int id = results.getInt(1);
float price = results.getFloat(2);
String update = results.getString(3);
int instrid = results.getInt(4);
logger.info(id + "\t\t" + price + "\t\t" + update + "\t\t" + instrid);
}
results.close();
stmt.close();
} catch (SQLException sqlExcept) {
logger.log(Level.SEVERE, null,sqlExcept);
}
/** Prints all data from the CORPEVENTS Table **/
try {
stmt = con.createStatement();
ResultSet results = stmt.executeQuery("select * from CORPEVENTS");
System.out.println("\n-------------------------------------------------");
while (results.next()) {
int id = results.getInt(1);
String type = results.getString(2);
String date = results.getString(3);
String desc = results.getString(4);
int instrid = results.getInt(5);
logger.info(id + "\t\t" + type + "\t\t" + date + "\t\t" + desc + "\t\t" + instrid);
}
results.close();
stmt.close();
} catch (SQLException sqlExcept) {
logger.log(Level.SEVERE, null,sqlExcept);
}
try {
stmt = con.createStatement();
ResultSet results = stmt.executeQuery("select * from DIVIDEND");
System.out.println("\n-------------------------------------------------");
while (results.next()) {
int id = results.getInt(1);
double payment = results.getDouble(2);
String date = results.getString(3);
int instrid = results.getInt(4);
logger.info(id + "\t\t" + payment + "\t\t" + date + "\t\t" + instrid);
}
results.close();
stmt.close();
} catch (SQLException sqlExcept) {
logger.log(Level.SEVERE, null,sqlExcept);
}
}
public void executeSqlScript(Connection conn, File inputFile) {
// Delimiter
String delimiter = ";";
// Create scanner
Scanner scanner;
try {
scanner = new Scanner(inputFile).useDelimiter(delimiter);
} catch (FileNotFoundException e1) {
logger.log(Level.SEVERE, null, e1);
return;
}
// Loop through the SQL file statements
Statement currentStatement = null;
while (scanner.hasNext()) {
// Get statement
String rawStatement = scanner.next();
try {
// Execute statement
currentStatement = conn.createStatement();
currentStatement.execute(rawStatement);
} catch (SQLException e) {
logger.log(Level.SEVERE, null, e);
} finally {
// Release resources
if (currentStatement != null) {
try {
currentStatement.close();
} catch (SQLException e) {
logger.log(Level.SEVERE, null, e);;
}
}
currentStatement = null;
}
}
}
}
TEST_INSTRUMENT
public class TEST_INSTRUMENT {
public static void main(String[] args){
DB_MANAGER mngr = new DB_MANAGER();
mngr.checkConnection();
DB_SETUP sdb = new DB_SETUP();
sdb.createTables();
sdb.insertSetupData();
DAO_INSTRUMENT iDAO = new DAO_INSTRUMENT();
iDAO.getAllInstruments();
/** This displays the newly edited tables, after each DAO is tested **/
System.out.println("");
System.out.println("All Instruments.");
for(CLS_INSTRUMENT instru: iDAO.getAllInstruments()){
System.out.println(instru.getName());}
iDAO.insertInstru(new CLS_INSTRUMENT("BOND", "DAC BOND", "Blah", "Blah blah blah", "17.11.2014", 9, "sdsdas", 2, null, null, 0, 0.85));
System.out.println("");
System.out.println("Instrument Added.");
for(CLS_INSTRUMENT instru: iDAO.getAllInstruments()){
System.out.println(instru.getName());}
iDAO.updateInstrument(new CLS_INSTRUMENT(1, "SHARE", "ABC LTD"));
System.out.println("");
System.out.println("Instrument Editted.");
for(CLS_INSTRUMENT instru: iDAO.getAllInstruments()){
System.out.println(instru.getName());}
iDAO.deleteInstrument(new CLS_INSTRUMENT(1, "SHARE", "ABC LTD"));
System.out.println("");
System.out.println("Instrument Deleted.");
for(CLS_INSTRUMENT instru: iDAO.getAllInstruments()){
System.out.println(instru.getName());
}
}
}
错误堆栈跟踪
run:
Nov 22, 2016 2:19:39 PM is3312project.DB_MANAGER loadJDBCDriver
SEVERE: null
java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at is3312project.DB_MANAGER.loadJDBCDriver(DB_MANAGER.java:26)
at is3312project.DB_MANAGER.checkConnection(DB_MANAGER.java:34)
at is3312project.TEST_INSTRUMENT.main(TEST_INSTRUMENT.java:18)
【问题讨论】:
-
发布错误堆栈跟踪。
-
我已经更新了。谢谢