无法通过Java应用程序连接到数据库

时间:2014-06-01 06:31:58

标签: java oracle

我无法从oracle 11g应用程序连接到数据库(java)。 我收到runtime错误。

我已经创建了我的程序中使用的表。我正在使用eclipse ide 这是我的计划:

    package db;
    import java.sql.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class db extends JFrame implements ActionListener
    {JFrame f;
    JButton b1,b2,b3,b4;
    JLabel l1,l2,l3,l4;
    JTextField t1,t2,t3,t4;
    String s;
    PreparedStatement ps; //=c.prepareStatement();
    ResultSet rs;
    Connection c;
    db()
    { try 
    {f=new JFrame();
    f.setSize(900,900);
    f.setVisible(true);

    b1=new JButton("insert");
    b2=new JButton("search"); 
    b3=new JButton("modify");
    b4=new JButton("delete");

    l1=new JLabel("eno");
    l2=new JLabel("ename");
    l3=new JLabel("salary");
    l4=new JLabel("DOJ");

    t1=new JTextField(30);
    t2=new JTextField(30);
    t3=new JTextField(30);
    t4=new JTextField(30);
    setLayout(null);

    f.add(l1);f.add(l2);f.add(l3);f.add(l4);
    l1.setBounds(70,70,50,50);
    l2.setBounds(70,150,50,50);
    l3.setBounds(70,230,50,50);
    l4.setBounds(70,310,50,50); 

    f.add(t1);f.add(t2);f.add(t3);f.add(t4);
    t1.setBounds(140,75,70,30);
    t2.setBounds(140,155,70,30);
    t3.setBounds(140,235,70,30);
    t4.setBounds(140,315,70,30);

    f.add(b1);f.add(b2);f.add(b3);f.add(b4);
    b1.setBounds(70,400,90,40);
    b2.setBounds(180,400,90,40);
    b3.setBounds(70,500,90,40);
    b4.setBounds(180,500,90,40);  
    b1.addActionListener(this); 
    b2.addActionListener(this); 
    b3.addActionListener(this); 
    b4.addActionListener(this); 
    Class.forName("oracle.jdbc.OracleDriver");
    c=DriverManager.getConnection("jdbc:oracle:thin@127.0.0.1:1521:xe","system","123");

    } 
    catch(Exception e2)
    { e2.printStackTrace();
    }  }
    public void actionPerformed(ActionEvent e)
    {     if(e.getSource()==b1)     //insert    
    { try
    { s = "INSERT INTO emp1 (eno,ename,salary,doj) VALUES (?,?,?,?)";
    ps = c.prepareStatement(s);
    ps.setInt(1,Integer.parseInt(t1.getText()));
    ps.setString(2,"t2.getText()");
    ps.setFloat(3,Float.parseFloat(t3.getText()));
    ps.setString(4,"t4.getText()");
    ps.executeUpdate(); 

    t1.setText("");
    t2.setText("");
    t3.setText("");
    t4.setText("");
    c.close();
    }
    catch(Exception e1)
    {e1.printStackTrace();
    }}//end if

    if(e.getSource()==b2)  //search
    { try{

    int id=Integer.parseInt(t1.getText());
    s="select * from emp1 where eno=?";
    ps.setInt(1,id);
    ps = c.prepareStatement(s);
    rs=ps.executeQuery();

    if(rs.next())
    {   t2.setText(rs.getString("ename"));
    t3.setText(String.valueOf(rs.getFloat("salary")));
    t4.setText(rs.getString("doj"));
    }

    else
    {JOptionPane.showMessageDialog(null,"NO RECORD FOUND");
    }
    c.close();
    }
    catch(Exception e1)
    {e1.printStackTrace();
    }}//end if

    if(e.getSource()==b4)//delete
    {try{
    s="delete from emp1 where eno=?";
    ps=c.prepareStatement(s);
    ps.setInt(1,Integer.parseInt(t1.getText()));
    int i=ps.executeUpdate();
    if(i==1)
    {
    JOptionPane.showMessageDialog(null,"RECORD DELETED");
    }
    else
    {JOptionPane.showMessageDialog(null,"RECORD NOT FOUND");
    }
    c.close();
    } 
    catch(Exception e1)
    {e1.printStackTrace();
    }
    }//end if
    if(e.getSource()==b3) //modify
    {try
    {int id=Integer.parseInt(t1.getText());
    ps=c.prepareStatement("set ename=?,salary=?,doj=? where eno=?");
    ps.setString(1,"t2.getText()");
    ps.setFloat(2,Float.parseFloat(t3.getText()));
    ps.setString(3,"t4.getText()");
    ps.setInt(4,id);
    int i=ps.executeUpdate();

    if(i==1)
    JOptionPane.showMessageDialog(null,"RECORD MODIFIED");
    else     
    JOptionPane.showMessageDialog(null,"RECORD NOT MODIFIED");
    }  
    catch(Exception e1)
    {e1.printStackTrace();
    }
    } // end if
    }
    public static void main(String arr[])
    {new db();
    }
    }



AND the run time ERRORS are:
java.sql.SQLException: Invalid Oracle URL specified
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:441)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at db.db.<init>(db.java:71)
at db.db.main(db.java:216)

2 个答案:

答案 0 :(得分:1)

`jdbc:oracle:thin:@127.0.0.1:1521:XE` try with this url. The error you post clearly says the url is invalid.

答案 1 :(得分:1)

由于堆栈跟踪表明您的连接网址无效。

您的连接字符串必须是

c=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe","system","123");

而不是

c=DriverManager.getConnection("jdbc:oracle:thin@127.0.0.1:1521:xe","system","123");

:thin之间必须有@