我在java netbeans中关注某些项目的MVC模式。我正在尝试将数据库与我的应用程序连接,并运行一个简单的查询来显示JTable中的所有数据。我编码完美,但是当我运行我的项目时,它会抛出一个错误:
package com.innovatraasolutions.eev.dal;
import com.innovatraasolutions.eev.bo.Voters;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.*;
/**
*
* @author SuperMan
*/
public class VoterDAO {
/**
*
* @param voter
* @return
*/
public List<Voters> verifyVoter(Voters voter){
String filename = null;
FileReader userFileReader = null;
FileReader userFileReader2 = null;
Connection connection = null;
List<Voters> list = new ArrayList<Voters>();
try{
String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName);
String serverName = "SpiderMan";
String serverPort = "1521";
String sid = "XE";
String url = "jdbc|oracle|thin|@" + serverName + "|" + serverPort + "|";
String userName = "eev";
String password = "123";
connection = DriverManager.getConnection(url, userName, password);
System.out.println("Sucessfully connected to the DataBase");
} catch(ClassNotFoundException e){
System.out.println("Class Not Found Exeption::Couldn't find the database driver" + e.getMessage());
} catch(SQLException e) {
System.out.println("Couldn't connect to the database" + e.getMessage());
}
/*---------------------------------------------------*/
//for retrieving data from database
try{
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select *from Voter");
while(resultSet.next()){
String vcnic = resultSet.getString(1);
voter.setCnic(vcnic);
String Fname = resultSet.getString(2).toString();
voter.setFname(Fname);
String Lname = resultSet.getString(3).toString();
voter.setLname(Lname);
String VoteRegisteredIn = resultSet.getString(4).toString();
voter.setVoteRegistered(VoteRegisteredIn);
String Address = resultSet.getString(5).toString();
voter.setAddress(Address);
list.add(voter);
}
resultSet.close();
}catch(Exception e){
e.printStackTrace();
}
return list;
/* try{
userFileReader = new FileReader("voters.txt");
String line = "a";
BufferedReader voterFileBuffReader = new BufferedReader(userFileReader);
while((line = voterFileBuffReader.readLine()) != null){
// System.out.println("Yesss");
//filename = line + ".txt";
//System.out.println(filename);
//System.out.println("first While");
userFileReader2 = new FileReader("voters.txt");
BufferedReader studFileBuffReader = new BufferedReader(userFileReader2);
while((line = studFileBuffReader.readLine()) != null){
//System.out.println("Second While");
String Cnic = line;
voter.setCnic(Cnic);
String Fname = studFileBuffReader.readLine();
voter.setFname(Fname);
String Lname = studFileBuffReader.readLine();
voter.setLname(Lname);
String VoteRegistered = studFileBuffReader.readLine();
voter.setVoteRegistered(VoteRegistered);
String Address = studFileBuffReader.readLine();
voter.setAddress(Address);
list.add(voter);
}
}
}*/
/* catch(Exception e){
System.err.println("Exception::");
} */
}
}
它出现以下错误:
run:
File not found!
Couldn't connect to the databaseNo suitable driver found for jdbc|oracle|thin|@SpiderMan|1521|
java.lang.NullPointerException
at com.innovatraasolutions.eev.dal.VoterDAO.verifyVoter(VoterDAO.java:58)
at com.innovatraasolutions.eev.bl.VoterHandler.verifyVoter(VoterHandler.java:27)
at com.innovatraasolutions.eev.controller.Controller.verifyVoter(Controller.java:83)
at com.innovatraasolutions.eev.controller.Controller.processRequest(Controller.java:43)
at com.innovatraasolutions.eev.ui.Voter.viewVoterActionPerformed(Voter.java:125)
at com.innovatraasolutions.eev.ui.Voter.access$000(Voter.java:21)
at com.innovatraasolutions.eev.ui.Voter$2.actionPerformed(Voter.java:75)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD SUCCESSFUL (total time: 31 seconds)
答案 0 :(得分:2)
您无法创建连接,因此您在日志中看到以下内容&#34;无法连接到数据库没有为jdbc找到合适的驱动程序| oracle | thin | @SpiderMan | 1521 |&#34; < / p>
您应该更改以下行:
String url = "jdbc|oracle|thin|@" + serverName + "|" + serverPort + "|";
要
String url = "jdbc:oracle:thin:@" + serverName + ":" + serverPort + "/" + sid;
如果您知道数据库名称,可以将其附加到:
String url = "jdbc:oracle:thin:@" + serverName + ":" + serverPort + ":" + dbname;
答案 1 :(得分:0)
检查您的oracle驱动程序.jar文件是否附加到您的项目中。点击此链接[https://www.youtube.com/watch?v=jsS_cRbNv3o]