0

I am following MVC pattern in java netbeans for some project. I am trying to connect database with my application and run a simple query of show all data in a JTable. I coded perfectly but when I run my project it throws an error:

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::");
       } */ 

 }

}

And it is giving the following error:

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)
2

2 Answers 2

2

You were not able to create connection and hence you see following in log "Couldn't connect to the databaseNo suitable driver found for jdbc|oracle|thin|@SpiderMan|1521|"

You should change the following line from:

String url = "jdbc|oracle|thin|@" + serverName + "|" + serverPort + "|";

To

String url = "jdbc:oracle:thin:@" + serverName + ":" + serverPort + "/" + sid;

if you know the database name you can append it to be like:

String url = "jdbc:oracle:thin:@" + serverName + ":" + serverPort + ":" + dbname;
Sign up to request clarification or add additional context in comments.

3 Comments

sir now when i correct what you correct it, it's shows error:
File not found! java.lang.NullPointerException at com.innovatraasolutions.eev.dal.VoterDAO.verifyVoter(VoterDAO.java:59) Couldn't connect to the databaseListener refused the connection with the following error: ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
See i updated answer. You might need to append "/" + sid to url
0

Check whether your oracle driver .jar file is attached to your project. check this link [https://www.youtube.com/watch?v=jsS_cRbNv3o]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.