日期:2014-05-20  浏览次数:21733 次

用JAVA连接ORACLE数据库的问题
我用JAVA连接ORACLE数据库,但是出现问题了,提示:
Exception   in   thread   "main "   java.lang.NoClassDefFoundError:   JDBCConn
我的源程序如下:
import   java.sql.*;

public   class   JDBCConn
{
private   static   String   url= " ";
private   static   String   username= " ";
private   static   String   password= " ";

private   Connection   conn()
{
try
{
Class.forName( "oracle.jdbc.driver.OracleDriver ");
Connection   con=DriverManager.getConnection(url,username,password);
return   con;
}
catch(ClassNotFoundException   cnf)
{
System.out.println( "driver   not   find: "+cnf);
return   null;
}
catch(SQLException   sqle)
{
System.out.println( "can 't   connection   db: "+sqle);
return   null;
}
catch(Exception   e)
{
System.out.println( "failed   to   load   JDBC   driver. ");
return   null;
}
}

public   void   query(Connection   con,String   sql)
{
try
{
if(con==null)throw   new   Exception( "datebase   connection   can 't   use! ");
if(sql==null)throw   new   Exception( "check   your   parameter: 'sql '!   don 't   input   null! ");
Statement   stmt   =con.createStatement();
ResultSet   rs=stmt.executeQuery(sql);
ResultSetMetaData   rmeta=rs.getMetaData();
int   numColumns=rmeta.getColumnCount();
while(rs.next())
{
for(int   i=0;i <numColumns;i++)
{
String   sTemp=rs.getString(i+1);
System.out.println(sTemp+ " ");
}
System.out.println( " ");
}
}
catch(Exception   e)
{
System.out.println( "query   error: "+e);
}
}

public   void   execute(Connection   con,String   sql)
{
try
{
if(con==null)return;
Statement   stmt   =con.createStatement();
stmt.executeUpdate(sql);
}
catch(Exception   e)
{
System.out.println( "execute   error:sql= "+sql);
System.out.println(e);
}
}

public   void   demo()
{
try
{
JDBCConn   oc=new   JDBCConn();
Connection   conn=oc.conn();
String   sql= "insert   into   TBL_USER(id,name,password)   values   (seq_user.nextval, 'switch ', 'haorenpingan ') ";
oc.execute(conn,sql);
sql= "select   *   from   TBL_USER ";
oc.query(conn,sql);
conn.close();
}
catch(SQLException   se)
{
System.out.println(se);
}
catch(Exception   e)
{
System.out.println(e);
}
}

public   static   void   main(String[]   arg)
{
if(arg.length!=3)
{
System.out.println( "use   :java   JDBCConn   url   username   password ");
return;
}
JDBCConn   oc=new   JDBCConn();
oc.url=arg[0];
oc.username=arg[1];
oc.password=arg[2];
oc.demo();
}
}

在运行时我输入:
java   JDBCConn   jdbc:oracle:thin:@127.0.0.1:1521:MISDB   scott   tiger