一个简单的JDBC连接程序
admin
2023-05-03 05:41:37
0

package com;


import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;


public class NavalMgmtDAO {


public static void main(String[] args) {


NavalMgmtDAO navalMgmtDAO=new NavalMgmtDAO();

double sal=navalMgmtDAO.getOfficersTotalSalOnBaseCamp(38);

System.out.println(sal);

}


public int addNavalOfficer(NavalOfficer navalOfficer)

{

int no=navalOfficer.getOfficerNo();

String name=navalOfficer.getOfficerName();

String rank=navalOfficer.getOfficerRank();

double sal=navalOfficer.getOfficerSal();

int campid=navalOfficer.getBaseCampId();


Connection connection = null;

int status = 0;

DBConnectionHelper helper = new DBConnectionHelper();

connection = helper.getOracleConnection();

Statement statement = null;

try {

connection.setAutoCommit(false);

statement = connection.createStatement();

String sql = "INSERT INTO TBL_Officer_1273752 values (" +no+",'"+name+"','"+rank+"',"+sal+","+campid+")";

status = statement.executeUpdate(sql);

connection.commit();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

finally {

try {

if (statement != null)

statement.close();

// System.out.println("STATEMENT SUCCESSFULLY CLOSED");

} catch (SQLException se) {

se.printStackTrace();

}// do nothing

try {

if (connection != null)

connection.close();

// System.out.println("CONNECTION SUCCESSFULLY CLOSED");

} catch (SQLException se) {

se.printStackTrace();

}// end finally try

}// end try

if (status == 1) {

System.out.println("Data inserted successfully");

}else{

System.out.println("Connection error");

}

return status;

}


public int addBaseCamp(BaseCamp baseCamp)

{

int id=baseCamp.getBaseCampId();

String name=baseCamp.getBaseCampName();

int loc=baseCamp.getBaseCampLoc();


Connection connection = null;

int status = 0;

DBConnectionHelper helper = new DBConnectionHelper();

connection = helper.getOracleConnection();

Statement statement = null;

try {

connection.setAutoCommit(false);

statement = connection.createStatement();

String sql = "INSERT INTO TBL_Base_Camp_1273752 values (" +id+",'"+name+"','"+loc+"')";

status = statement.executeUpdate(sql);

connection.commit();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

finally {

try {

if (statement != null)

statement.close();

// System.out.println("STATEMENT SUCCESSFULLY CLOSED");

} catch (SQLException se) {

se.printStackTrace();

}// do nothing

try {

if (connection != null)

connection.close();

// System.out.println("CONNECTION SUCCESSFULLY CLOSED");

} catch (SQLException se) {

se.printStackTrace();

}// end finally try

}// end try

if (status == 1) {

System.out.println("Data inserted successfully");

}else{

System.out.println("Connection error");

}

return status;

}



public ArrayList  getOfficersNameSortedBySal()

{


ArrayList arrayList=new ArrayList();


Connection connection = null;

DBConnectionHelper helper = new DBConnectionHelper();

connection = helper.getOracleConnection();

Statement statement = null;

ResultSet resultSet = null;

try {

statement = connection.createStatement();

String sql = "select officer_name from tbl_officer_1273752 order by officer_sal";

resultSet = statement.executeQuery(sql);


while (resultSet.next()) {

String name = resultSet.getString(1);

arrayList.add(name);

}


} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

if (statement != null)

statement.close();


} catch (SQLException se) {

}// do nothing

try {

if (connection != null)

connection.close();


} catch (SQLException se) {

se.printStackTrace();

}// end finally try

if (resultSet != null) {

try {

resultSet.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}// end try

return arrayList;


}



public ArrayList  getOfficersNameForBaseCampLoc(String baseCampLoc)

{

ArrayList arrayList=new ArrayList();

Connection connection = null;

DBConnectionHelper helper = new DBConnectionHelper();

connection = helper.getOracleConnection();

Statement statement = null;

ResultSet resultSet = null;

try {

statement = connection.createStatement();

String sql = "select officer_name from tbl_officer_1273752 where base_camp_id in (select base_camp_id from tbl_base_camp_1273752 where base_camp_loc='"+baseCampLoc+"')";

resultSet = statement.executeQuery(sql);


while (resultSet.next()) {

String name = resultSet.getString(1);

arrayList.add(name);

}


} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

if (statement != null)

statement.close();


} catch (SQLException se) {

}// do nothing

try {

if (connection != null)

connection.close();


} catch (SQLException se) {

se.printStackTrace();

}// end finally try

if (resultSet != null) {

try {

resultSet.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}// end try

return arrayList;


}

public double getOfficersTotalSalOnBaseCamp(int baseCampId)

{

double sal=0;

Connection connection = null;

DBConnectionHelper helper = new DBConnectionHelper();

connection = helper.getOracleConnection();

Statement statement = null;

ResultSet resultSet = null;

try {

statement = connection.createStatement();

String sql = "select sum(officer_sal) from tbl_officer_1273752 where base_camp_id="+baseCampId+"";

resultSet = statement.executeQuery(sql);


while (resultSet.next()) {

sal= resultSet.getDouble(1);

}


} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

if (statement != null)

statement.close();


} catch (SQLException se) {

}// do nothing

try {

if (connection != null)

connection.close();


} catch (SQLException se) {

se.printStackTrace();

}// end finally try

if (resultSet != null) {

try {

resultSet.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}// end try

return sal;

}




}


相关内容

热门资讯

加盟商参加中通快运酒局后身亡,... 5月11日,新黄河客户端以《加盟商参加中通快运酒局后身亡》为题,报道了代理加盟商纪某某在连续参加两场...
5月全球航班计划已砍掉1.3万... 作者 | 高雅2026年的夏季出行季正在中东局势的余震中拉开帷幕。对于全球旅客而言,这一季的机票订单...
凯特王妃将赴意大利,系病情缓解... 【环球网报道】据英国广播公司(BBC)、美联社等媒体报道,英国威尔士王妃凯特将于13日重返国际舞台,...
河南拟发行392.99亿元地方... 【大河财立方消息】 5月13日,河南省财政厅披露2026年河南省政府一般债券(六至七期)、专项债券(...
美军侦察机未经授权进入领空,奥... 【文/观察者网 熊超然】据“今日俄罗斯”(RT)当地时间5月13日报道,奥地利方面表示,该国已紧急出...
男子骑车闯红灯被罚,疑问“没车... 近日,一男子骑车时因闯红灯被处罚,后与交警产生对话,交警的回答被网友称赞为“教科书级别的科普”。司机...
他信,笑着出狱了 监狱大门打开的那一刻,泰国前总理他信微笑着张开双臂,拥抱小女儿佩通坦。佩通坦随后在社交媒体发布两人合...
冰箱漏电怎么测 1、可以用灯泡检查法来检测冰箱漏电,将36V灯泡的两根导线,一根接冰箱外壳(无漆处)另一根接大地,如...
电饭煲通电就跳闸怎么回事 如果之前电饭煲从未出现过跳闸现象,此现象为新出现的,有可能是插板或者是电饭煲发生故障导致的。首先可以...
空调外机有温度传感器损坏 最可能是温度传感器泄漏导致的这个原因。可以使用热毛巾加热温度检测管,将按钮设置为最低温度,并使用测试...