一个简单的JDBC连接程序

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;

}

}

时间: 2024-07-30 07:19:44

一个简单的JDBC连接程序的相关文章

编写一个简单的jdbc例子程序

1 package it.cast.jdbc; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 public class Base { 10 11 public static void main(String[] args) th

【Java编程】建立一个简单的JDBC连接-Drivers, Connection, Statement and PreparedStatement

本blog提供了一个简单的通过JDBC驱动建立JDBC连接例程,并分别通过Statement和PreparedStatement实现对数据库的查询.在下一篇blog中将重点比较Statement与PreparedStatement的差异. 1.为项目添加JDBC驱动 1)JDBC驱动下载 官方下载地址:mysql-connector-java-5.0.8.zip CSDN资料下载地址:mysql-connector-java-5.0.8.zip 2)为项目添加JDBC驱动 建立项目Java项目J

一个简单的反射连接程序(修改文件时间,以及创建Windows服务)

program SvrDemo; uses  Windows,  WinSvc,  winsock; const  RegName = 'SvrDemo'; var  szServiceName: pchar = 'SvrDemo';  szFileName:pchar;  ServiceTable: array [0..1] of TServiceTableEntry;  Status: SERVICE_STATUS;  StatusHandle: SERVICE_STATUS_HANDLE;

用L脚本语言开发一个简单的局域网聊天程序

#scp #这是一个简单的局域网聊天程序的例子 定义:字符串,string1 定义:字符串,string2 #addr1是对方的地址 #addr2是自己的地址 #如果addr1和addr2相同,就是自己和自己聊天 定义:地址,addr1,127.0.0.1,27015 定义:地址,addr2,127.0.0.1,27015 定义:整数,字节数,0 #在自己的UDP端口上监听 定义:网络连接,conn2,UDP 监听:conn2,addr2 #连接对方的UDP端口 定义:网络连接,conn1,UD

一个简单的Java 连接SQL Server数据库连接驱动类

import java.sql.*; /** * SQL Server数据库连接类 * @author Administrator * */ public class Sqlsdc { static int a = 0; public Connection sqlsdc(String user, String pwd, String dn) { String url = "jdbc:sqlserver://localhost:1433;databaseName="+dn; final

利用java的Socket实现一个简单hello/hi聊天程序

利用java的Socket实现一个简单hello/hi聊天程序 首先,我们来用java实现一个简单的hello/hi聊天程序.在这个程序里,我学习到了怎么用socket套接套接字来进行编程.简单理解了一些关于socket套接字和底层调用的关系.关于java的封装思想,我学会了一些东西,java里真的是万物皆对象.还学到了一点多线程的知识. TCP 在这里,不得不先介绍以下TCP.TCP是传输层面向连接的协议.提供了端到端的进程之间的通信方式.TCP在通信之前要先建立连接.这里我们称这个建立连接的

iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序

iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序 一.plist文件和项目结构图 说明:这是一个嵌套模型的示例 二.代码示例: YYcarsgroup.h文件代码: // // YYcarsgroup.h // 07-汽车展示(高级) // // Created by apple on 14-5-28. // Copyright (c) 2014年 itcase. All rights reserved. // #import <Foundation/Foundation.h> @

一个简单 的Shell 显示程序

#!/bin/bash clear declare FirstName Greeting   Greeting="Hello ," echo "" echo "Enter Your First Name:" read FirstName echo "$Greeting $FirstName" 首先   vim  Print  回车 然后   i   进入插入状态 编辑以上代码,Esc 键回车 输入   chmod  711 P

一个简单的键盘钩子程序

实现适时监视键盘,并将按键信息保存在TXT文件中的程序       Windows系统是建立在事件驱动的机制上的,说穿了就是整个系统都是通过消息的传递来实现的.而钩子是Windows系统中非常重要的系统接口,用它可以截获并处理送给其他应用程序的消息,来完成普通应用程序难以实现的功能.钩子的种类很多,每种钩子可以截获并处理相应的消息,如键盘钩子可以截获键盘消息,外壳钩子可以截取.启动和关闭应用程序的消息等.本文在VC6编程环境下实现了一个简单的键盘钩子程序,并对Win32全局钩子的运行机制.Win