一个简单的JDBC连接程序
2016-04-27 23:34
375 查看
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;
}
}
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;
}
}
相关文章推荐
- 关于PHP浮点数你应该知道的(All 'bogus' about the float in PHP)
- AS3 中的package(包)应用实例代码
- Ruby 之 class 中的 private、 protected、public
- Orcle的package中访问其它Schema的表报错ORA-00942解决方法
- Ruby中的public、private、protected区别小结
- JavaScript的public、private和privileged模式
- Lua中的模块(module)和包(package)详解
- Java基础教程之包(package)
- C++中的三种继承public,protected,private详细解析
- Python开发常用的一些开源Package分享
- Python上传package到Pypi(代码简单)
- 深入理解C++中public、protected及private用法
- 详解Java编程中包package的内容与包对象的规范
- 【编程基础】Java 中的Package和Import
- C++中 public和private派生类继承问题和访问权限问题
- Found: decimal, expected: double precision
- Oracle package模板
- session 存取对象
- phpcms v9 前台无限制GETSHELL
- Android字符串进阶之三:字体属性及测量(FontMetrics)