Search code examples
javaswinguser-interfaceclassnotfoundexception

How to get the values from database?


I have tried to get the values from DB using Java. I need to get the student name and his father name which already saved in the DB when entering his roll number.

I tried the below code and I am getting an error. (Edit error after stack trace printed.)

ClassNotFoundException: oracle.jdbc.driver.OracleDriver

Suggest the solution or issues.

import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class Test1  {

   JFrame f;
   JLabel l1,l2;
   JTextField f1;
   JButton b1;
    Connection con;
    Statement st;
    ResultSet rs;

    /**
     * Creates new form Register
     */
    public static void main(String args[]) {
       Test1 r=new Test1();
        r.frame();
        r.connect();
    }

    public void connect() {
        try {

            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection("jdbc:oracle:thin:@//hostname:port/servicename","username","password");
            st = (Statement) con.createStatement();
            String str = "select student_name,father_name from student_db where roll_no='&roll_no'";
            st.executeUpdate(str);
            System.out.println(str);
        } catch (Exception ex) {
        }


    }

    public void frame() 
    {
     f=new JFrame ("studentrecord");
     f1=new JTextField(10);
     b1=new JButton("submit");
     l1=new JLabel("student_name");
     l2=new JLabel("father_name");

     f.setLayout(new GridBagLayout());
     f.add(l1);
     f.add(f1);
     f.add(l2);
     f.add(b1);
     f.setVisible(true);
     f.pack();
     b1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
            String student_name=l1.getText();
            String father_name=l2.getText();
            String roll_no=f1.getText();
            System.out.println(student_name);
            System.out.println(father_name);
            System.out.println(roll_no);

            connect();
        }
    });
    }
    }

Also the student name and father name label values should display under the roll number but its displayed next to the submit button.is there any other better layout there to display like that?


Solution

  • Something like the code below will do the read query. But for God's sake, read the documentation before trying to write something.

    public String studentName(String rollNo) throws SQLException {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rst = null;
        String names = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection("jdbc:oracle:thin:@hostname:port/servicename","username","password");
            stm = con.prepareStatement("select student_name,father_name from student_db where roll_no=?");
            stm.setString(1,  rollNo);
            rst = stm.executeQuery();
            if (rst.next())
                names = rst.getString(1)+","+rst.getString(2);
            rst.close();
            rst = null;
            stm.close();
            stm=null;
            con.close();
            con = null;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (rst!=null) rst.close();
            if (stm!=null) stm.close();
            if (con!=null) con.close();
        }
        return names;
    }