您好,欢迎访问一九零五行业门户网

Java访问Oracle数据库代码

功能:将数据库中的数据读到java类cube中,并计算出正方体的体积。1.使用pl/sql developor设计一个表格,名为first; table -gt
功能:将数据库中的数据读到java类cube中,,并计算出正方体的体积。
1.使用pl/sql developor设计一个表格,名为first;
table -> new table,设计表空间,与字段的大小,以及主键;
table -> first -> edit data,填入所需要的数据,则一个表格就生成了。
2.使用java代码访问oracle数据库,代码如下:
import java.sql.*;
import java.sql.resultset;
import java.sql.sqlexception;
import java.util.vector;
public class test {
static vector cubeobjects = new vector();
public static void main(string[] args) throws classnotfoundexception, sqlexception{
string driver = oracle.jdbc.driver.oracledriver;
string strurl = jdbc:oracle:thin:@a9ae3e7c1051465:1521:idpdb;//idpdb为数据库名称
connection conn = null;
resultset rsresult=null;
statement stmt=null;
class.forname(driver);
//第二个参数为数据库用户名,第三个参数为数据库密码
conn = drivermanager.getconnection(strurl,idp_pub_m,idpapp);
stmt=conn.createstatement();
rsresult=stmt.executequery(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(矩形次序  + ++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(不是正方体);
}
}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;
}
}
其它类似信息

推荐信息