简体   繁体   中英

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

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)

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

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

It could be that you are missing the actual jdbc driver for your Java version.

ojdbc6.jar from Oracle should do the trick for versions 6, 7 and 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:

Properties > Java Build Path > Libraries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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