0

i got an unkown source error message in jdbc connection :

IDE: java eclipse
Data Base: oracle 10g
code : java swing
DNS name : home
table name : lg (login )

please help me out from this error...! Here's the code that I am using :

package vijay;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.border.*;
import java.sql.*; 
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

public class jdbc extends JFrame {
    Connection con=null;
    Statement st=null;

    /**
     * 
     */
    private static final long serialVersionUID = 6517038751742780009L;
    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    jdbc frame = new jdbc();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public jdbc()throws Exception  {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        con = DriverManager.getConnection("jdbc:odbc:home","system","sa");
        st = con.createStatement();
        JOptionPane.showMessageDialog(null, "connected");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 566, 359);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.CENTER);
        panel.setLayout(null);

        JLabel lblUserName = new JLabel("user name");
        lblUserName.setBounds(144, 89, 70, 14);
        panel.add(lblUserName);

        JLabel lblPassword = new JLabel("password");
        lblPassword.setBounds(144, 142, 46, 14);
        panel.add(lblPassword);

        textField = new JTextField();
        textField.setBounds(248, 86, 86, 20);
        panel.add(textField);
        textField.setColumns(10);

        JButton btnLogin = new JButton("login");
        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String un=textField.getText();
                String pwd=passwordField.getText().trim();
                String str;
                if(un.equals("vijay")&&pwd.equals("123")) {
                    try {
                        str="insert into lg    values('"+un+"','"+pwd+"')";
                        st.executeUpdate(str);
                        JOptionPane.showMessageDialog(null, "record inserted");
                    }
                    catch(Exception e) {
                    }
                }
                else {
                    JOptionPane.showMessageDialog(null, "Wrong Login Details");
                }
            }
        });
        btnLogin.setBounds(144, 209, 89, 23);
        panel.add(btnLogin);
        btnLogin.setMnemonic(KeyEvent.VK_L);

        JButton btnExit = new JButton("Exit");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });
        btnExit.setBounds(254, 209, 89, 23);
        panel.add(btnExit);
        btnExit.setMnemonic(KeyEvent.VK_E);

        passwordField = new JPasswordField();
        passwordField.setBounds(247, 142, 86, 20);
        panel.add(passwordField);
    }
}
1
  • How the values should be populated to the frame? And don't use jdbc-odbc driver to connect to the Oracle database, it's a bad practice. Commented Aug 4, 2013 at 14:22

2 Answers 2

1

It will be difficult for you to connect to oracle DB using this driver: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");.

You should be loading oracle driver.

Following is an example to connect to oracle DB:

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class OracleJDBC {

    public static void main(String[] argv) {

        System.out.println("-------- Oracle JDBC Connection Testing ------");

        try {

            Class.forName("oracle.jdbc.driver.OracleDriver");

        } catch (ClassNotFoundException e) {

            System.out.println("Where is your Oracle JDBC Driver?");
            e.printStackTrace();
            return;

        }

        System.out.println("Oracle JDBC Driver Registered!");

        Connection connection = null;

        try {

            connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:test", "username",
                    "password");

        } catch (SQLException e) {

            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;

        }

        if (connection != null) {
            System.out.println("You made it, take control your database now!");
        } else {
            System.out.println("Failed to make connection!");
        }
    }

}

EDIT:

From oracle docs:

It is recommended that you obtain a commercial JDBC driver from a vendor such as your database vendor or your database middleware vendor. Check the list of drivers currently available. The JDBC-ODBC Bridge driver is recommended only for experimental use or when no other alternative is available.

You can check more details at : http://docs.oracle.com/javase/1.3/docs/guide/jdbc/getstart/bridge.doc.html

Sign up to request clarification or add additional context in comments.

4 Comments

Thank u loki but my code is working on another system, how it is working on an another system if it is wrong
I have edited my post, jdbcodbc bridge driver might work but might not work depending on system configuration. The system on which its not working, you might not be having odbc driver installed. But i will never use jdbcodbc to connect to oracle.
then which is the good way to connect with oracle using java swing coding
It doesn't matter if it is a swing code. In java you can use the code i pasted to connect to oracle DB. You just need to place ojdbc jar in your classpath to get oracle driver.
0

check whether you had created DNS in Data Sources?

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.