0

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
sathya
  • 199
  • 5
  • 26
  • 2
    1) *"i am not getting any error. .. `} catch (Exception ex) { }`"* You won't have any idea about errors when writing code like that. Don't ignore exceptions! They inform us exactly what went wrong. Unless logging is implemented, at least call `Throwable.printStackTrace()` 2) Is the problem getting the data, or displaying it? *"Also the student name and father name label values should display.."* Test them separately in order to narrow the problem down. SO is not a help desk, or a tutoring service. It is a Q&A site. – Andrew Thompson Dec 11 '16 at 11:42
  • yes, the problem is displaying the data – sathya Dec 11 '16 at 11:43
  • For [example](http://stackoverflow.com/a/34742409/230513). – trashgod Dec 11 '16 at 11:44
  • *"the problem is displaying the data"* Factor out the database by hard coding some `String` data in the code itself. Populate the display with that data when the user actions a button. – Andrew Thompson Dec 11 '16 at 11:48
  • actually i took the code from online for insert the values in db then converted the query for view the values in db. but i have doubt the code which written is correct to display the data. – sathya Dec 11 '16 at 11:55
  • yeah hardcode will be helpful only – sathya Dec 11 '16 at 11:55
  • Now i have updated the above code and added the print statement,when executing the code it prints what the value in the str but the System.out.println(student_name); prints the same variable name student_name only , i am not getting any values from student_name table for that roll no.similar to the father name and rollno too. – sathya Dec 11 '16 at 12:55

2 Answers2

1

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;
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Serg M Ten
  • 5,568
  • 4
  • 25
  • 48
0

stm = con.prepareStatement("select student_name,father_name from student_db where roll_no=?"); stm.setString(1, rollNo); rst = stm.executeQuery();

This definitely works.