Use Java to connect Oracle

xiaoxiao2021-03-05  31

Package edu.gzu.kyle;

Import java.sql. *;

/ **

* @Author administrator

*

* Todo to change the Template for this generated Type Comment Go To to

* WINDOW - Preferences - Java - Code Style - Code Templates

* /

Public class oracletest {

Protected connection conn = NULL;

protected statement stmt = null;

Private string url = "jdbc: Oracle: Thin: @ JH1: 1521: ORA92"; // Connecting string

Private string usr = "scott"; // Connect the user and password used

PRIVATE STRING PWD = "Tiger";

Public oracletest () {

Super ();

}

Public void dbconn () {

Try {

Class.Forname ("Oracle.jdbc.driver.OracleDriver");

CONN = DriverManager.getConnection (URL, USR, PWD);

STMT = conn.createstatement ();

} catch (classnotfoundexception e) {

System.out.println ("Class Not FOUNE");

E.PrintStackTrace ();

} catch (sqlexception e) {

E.PrintStackTrace ();

}

}

Public ResultSet Query (String SQL) THROWS SQLEXCEPTION {

Return Stmt.executeQuery (SQL);

}

Public void dbends () {

Try {

IF (stmt! = null) {

Stmt.close ();

STMT = NULL;

}

} catch (sqlexception e) {

E.PrintStackTrace ();

}

}

Public static void main (String [] args) {

String SQL = "SELECT * from Emp where Job = 'Manager'";

Oracletest OT = New Oracletest ();

Try {

t.dbconn ();

ResultSet RS = OT.Query (SQL);

While (rs.next ()) {

System.out.println (Rs.getstring (1));

}

Rs.close ();

} catch (sqlexception e) {

E.PrintStackTrace ();

} finally {

Ot.dbends ();

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-32847.html

New Post(0)