一個(gè)簡(jiǎn)單的JDBC連接程序

package com;

創(chuàng)新互聯(lián)是一家從事企業(yè)網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、行業(yè)門(mén)戶網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)制作的專(zhuān)業(yè)網(wǎng)絡(luò)公司,擁有經(jīng)驗(yàn)豐富的網(wǎng)站建設(shè)工程師和網(wǎng)頁(yè)設(shè)計(jì)人員,具備各種規(guī)模與類(lèi)型網(wǎng)站建設(shè)的實(shí)力,在網(wǎng)站建設(shè)領(lǐng)域樹(shù)立了自己獨(dú)特的設(shè)計(jì)風(fēng)格。自公司成立以來(lái)曾獨(dú)立設(shè)計(jì)制作的站點(diǎn)超過(guò)千家。

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;

}

}

網(wǎng)站題目:一個(gè)簡(jiǎn)單的JDBC連接程序
文章來(lái)源:http://bm7419.com/article20/jjsgjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、微信小程序、服務(wù)器托管響應(yīng)式網(wǎng)站、網(wǎng)站維護(hù)小程序開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)