简体   繁体   English

Java程序中的oracle数据库连接

[英]oracle database connection in java program

在此处输入图片说明

 import java.sql.*;
 import java.io.*;

 public class OracleCon {


public static void main(String []args)throws ClassNotFoundException,SQLException,IOException
{

Class.forName("oracle.jdbc.OracleDriver");
    Connection con=DriverManager.getConnection("jdbc:oracle:thin@localhost:1521:xe", "system", "123456789");
Statement st=con.createStatement();
String sql="insert into citylist values ('vijay','54222','110001')";
int r=st.executeUpdate(sql);

if(r>0)
{
    System.out.print("value inserted");
}
else
{
    System.out.print("value not inserted");
}
//ResultSet rs=st.executeQuery(sql);

//while(rs.next())
//  System.out.println(rs.getString(1)+""+rs.getString(2)+""+rs.getString(3));
con.close();


  }
}

I have installed oracle 11g and jdk 1.8 at windows 7 我已经在Windows 7上安装了oracle 11g和jdk 1.8

Exception in thread "main" java.lang.ClassNotFoundException:oracle.jdbc.driver.OracleDriver at java.net.URLClassLoader.findClass(Unknown source) at java.lang.ClassLoader.loadClass (Unknown source) 线程“主”中的异常java.lang.ClassNotFoundException:java.net.URLClassLoader.findClass上的oracle.jdbc.driver.OracleDriver(java.lang.ClassLoader.loadClass上的未知源)(未知源)

在命令行中运行程序时,必须将jar文件路径包括在classpath参数前面,如下所示:

java OracleCon -classpath c:\somepath\ojdbc6.jar

It could be that you are missing the actual jdbc driver for your Java version. 可能是您缺少Java版本的实际jdbc驱动程序。

ojdbc6.jar from Oracle should do the trick for versions 6, 7 and 8. Oracle的 ojdbc6.jar应该可以解决版本6、7和8的问题。

Also don't forget to add the driver file to your classpath. 同样不要忘记将驱动程序文件添加到您的类路径中。 If you are using Eclipse you can do that via right click on your project: 如果您使用的是Eclipse,则可以通过右键单击项目来实现:

Properties > Java Build Path > Libraries

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM