2013-08-04 53 views
0

Oracle 10g中創建JDBC連接i的JDBC連接了一個未知的源錯誤消息:使用的Java Swing

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

請幫我從這個錯誤...! 下面是我使用的代碼:

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); 
    } 
} 
+0

值應該如何填充到框架?並且不要使用jdbc-odbc驅動程序連接到Oracle數據庫,這是不好的做法。 –

回答

1

這將是你很難連接到使用該驅動程序的Oracle DB:Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

您應該加載oracle驅動程序。

下面是一個例子來連接到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!"); 
     } 
    } 

} 

編輯:

從Oracle文檔:

建議您從 獲得商業JDBC驅動程序供應商,如您的數據庫供應商或您的數據庫中間件供應商。檢查當前可用的驅動程序列表。 JDBC-ODBC 橋接驅動程序建議僅用於實驗使用,或者當沒有可用的其他替代方法時。

您可以查看更多詳細信息:http://docs.oracle.com/javase/1.3/docs/guide/jdbc/getstart/bridge.doc.html

+0

謝謝你loki但我的代碼在另一個系統上工作,如果它在另一個系統上工作,如果它是錯誤的 – user2650543

+0

我編輯了我的帖子,jdbcodbc橋驅動程序可能工作,但可能無法工作,具體取決於系統配置。無法正常工作的系統,您可能沒有安裝odbc驅動程序。但我絕不會使用jdbcodbc連接到oracle。 – Lokesh

+0

那麼哪個是使用java swing編碼與oracle連接的好方法 – user2650543

0

檢查您是否已經在數據源創建DNS?