gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:46:54 25 4
gpt4 key购买 nike

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

我已经创建了我的程序中使用的表。我正在使用 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)

最佳答案

正如堆栈跟踪所示,您的连接 URL 无效。

您的连接字符串必须是

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@ 之间必须有 :

关于java - 无法通过java应用程序连接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23977324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com