一个简单的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;

}




}


相关内容

热门资讯

卖“毒蛋”的人,抓到了 作者 | 何国胜 编辑 | 向现“(人)抓到了,目前案件正在侦办中。”7月28日晚间,苏州禁毒部门有...
尺素金声丨实施零关税国家达63... 海关总署发布的数据显示,今年5月1日起,我国对53个非洲建交国全面实施零关税举措,目前,我国实施零关...
职业索赔盯上基层诊所,倒逼用药... 文 | 布丁基层诊所正在被职业索赔盯上。据新京报,去年夏天,一男子走进河南南阳一家诊所,要求购买三瓶...
“西瓜我全买了”就可以肆意妄为... 拿西瓜砸了人,把瓜都买了,就能一走了之吗?事实证明,这套逻辑在法治社会行不通。7月28日晚,据海峡都...
科学家在日本广岛发现新物质,系... 在美国对日本广岛进行原子弹轰炸近81年后,科学家们在广岛的沙滩上发现了一种奇异且从未被发现过的新物质...
AI失控,反噬开始 作者 | 贺一 编辑 | 阿树近期,中国开源模型在美国频繁引发热议。7月28日,月之暗面发布Kimi...
“总统千金天价离婚”,分到43... 2026年7月24日下午,首尔高等法院,一场持续近十年的司法拉锯战终于接近尾声。法庭裁定SK集团会长...
巴基斯坦,又拿下一个历史性协议 全世界都没想到,接连的中东大战,巴基斯坦正成为最大赢家。去年以色列追杀哈马斯,空袭卡塔尔首都,阿拉伯...
汇正财经贺峰的一对一指导服务怎...   对于考虑购买证券投资顾问服务的投资者来说,'一对一指导服务怎么样'是一个重要的考量维度。需要首先...
重庆失联00后网格员龚宝冬确认...   重庆失联00后网格员龚宝冬确认遇难  【重庆失联00后网格员龚宝冬确认遇难】2026年7月29日...