欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java当中请给出一个oracle的helloworld例子

程序员文章站 2022-05-26 12:38:44
java当中请给出一个oracle的helloworld例子 ......

[学习笔记]

2.oracle的helloworld例子:

import java.sql.*;
public class oraclehello{
    public static void main(string args[]) {
        connection con;
        string createstring;
        createstring = "select empno from emp";
        statement stmt;
        try {
/*see above, import sun.jdbc.odbc.jdbcodbcdriver;*/
/*see above, import sun.jdbc.odbc.jdbcodbcdriver;for kkk this kind of url connection string, you must
            use sun.jdbc.odbc.jdbcodbcdriver. if you wan to use oracle.jdbc.driver.oracledriver, you must use
            jdbc:oracle:thin:@localhost:1521:qixy */
/*a sound blaster hardware need a driver to function, software draw awave, then when play,
through the driver, hardware can change it to sound you can hear. here your statement can
be sent to the database.

 public static class forname(string classname)
                      throws classnotfoundexceptionreturns the class object associated with the class or interface with the given string name. invoking this method is equivalent to:
   class.forname(classname, true, currentloader)
  where currentloader denotes the defining class loader of the current class.
 for example, the following code fragment returns the runtime class descriptor for the class named java.lang.thread:

    class t = class.forname("java.lang.thread")
  a call to forname("x") causes the class named x to be initialized.


 */
            class.forname("oracle.jdbc.driver.oracledriver");
            con = drivermanager.getconnection("jdbc:oracle:thin:@localhost:1521:qixy", "scott", "tiger");
            system.out.println("connection is ok");
            stmt = con.createstatement();
  //          stmt.execute(createstring);
            resultset rs=stmt.executequery(createstring);
            while(rs.next())
            {     string no=rs.getstring("empno");
                  system.out.println(no);
            }
            stmt.close();
            con.close();        }
        catch (exception ex) {
            ex.printstacktrace();
        }    }}

文章转载自原文: