文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

一个简单的JDBC连接程序

2024-04-02 19:55

关注

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<String>  getOfficersNameSortedBySal()

{


ArrayList<String> arrayList=new ArrayList<String>();


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<String>  getOfficersNameForBaseCampLoc(String baseCampLoc)

{

ArrayList<String> arrayList=new ArrayList<String>();

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;

}




}


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯