Java code for accessing the Oracle database

Source: Internet
Author: User

Function: Read the data in the database to the java cube class and calculate the cube size.

1. Use PL/SQL Developor to design a table named first;
Table-> new Table: design the tablespace, the size of the field, and the primary key;
Table-> first-> edit data: Enter the required data, and a Table is generated.

2. Use java code to access the Oracle database. The Code is as follows:
Import java. SQL .*;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. util. Vector;


Public class Test {
Static Vector <Cube> CubeObjects = new Vector <Cube> ();
Public static void main (String [] args) throws ClassNotFoundException, SQLException {
String driver = "oracle. jdbc. driver. OracleDriver ";
String strUrl = "jdbc: oracle: thin: @ a9ae3e7c1051465: 1521: idpdb"; // idpdb indicates the database name.
Connection conn = null;
ResultSet rsResult = null;
Statement stmt = null;
Class. forName (driver );
// The second parameter is the database user name, and the third parameter is the database password.
Conn = DriverManager. getConnection (strUrl, "idp_pub_m", "idpapp ");
Stmt = conn. createStatement ();
RsResult=stmt.exe cuteQuery ("select * from first ");
While (rsResult. next ()){
Cube cub = new Cube (rsResult. getInt (1), rsResult. getInt (2), rsResult. getInt (3), rsResult. getInt (4 ));
CubeObjects. add (cub );
}
Int I = 0;
For (Cube cube: CubeObjects ){
System. out. println ("rectangular order" ++ I + ":" + calculate (cube ));
}
}
Static int calculate (Cube cube ){
Try {
If (cube. Height = cube. Length & cube. Length = cube. Width ){
Return cube. Height * cube. Length * cube. Width;
} Else {
Throw new Exception ("not cube ");
}
} Catch (Exception e ){
System. out. println (e. getMessage ());
}
Return 0;
}
}


Class Cube {
Int ID = 0;
Int Length = 0;
Int Width = 0;
Int Height = 0;
Public Cube (int ID, int Length, int Width, int Height ){
This. ID = ID;
This. Length = Length;
This. Width = Width;
This. Height = Height;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.