这要看你用的是hibenate还是ibatis
我用的是ibatis技术,在javabean里写list= ibatisDAOgetData("getdate", null)
然后再xml里写sql语句
<select id="getdate"
resultClass="cacheConf">
select
from GSG_KERNEL_CACHE_CONF_TAB
</select>
不用框架
ojdbc驱动
JDBC
加载驱动
Connection连接
Statment 准备
ResultSet取数据
循环
用outprint() 输出
avasqlSQLException: Io 异常: The Network Adapter could not establish the connection
异常:网络适配器无法建立连接。
oracle建立连接有2中模式
thin 和 oci
你写的String url="jdbc:oracle:oci8:@localhost:1521:oracle";
oci8????
-------------------
两种模式从使用上来说,oci必须在客户机上安装oracle客户端才能连接,而thin就不需要。原理上来看,thin是纯java实现tcp/ip的c/s通讯;而oci方式,客户端通过native java method调用c library访问服务端,而这个c library就是oci(oracle called interface),因此这个oci总是需要随着oracle客户端安装
你把url改为
String url="jdbc:oracle:thin:@localhost:1521:oracle";
=========================
问题就应该解决了
用spring框架连接oracle数据库可以通过配置数据源的方式:
Spring 数据库连接配置
oracle为例来说明:
<bean id="dataSource" class="orgapachecommonsdbcpBasicDataSource" destroy-method="close">
<property name="driverClassName" value="oraclejdbcdriverOracleDriver" />
<property name="url" value="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=1921681101)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=orcl)(SERVER=DEDICATED)))" />
<property name="username" value="rootl" />
<property name="password" value="1234" />
</bean>
扩展其他数据库:
二 DB2
<bean id="dataSource" class="orgapachecommonsdbcpBasicDataSource" destroy-method="close">
<property name="driverClassName" value="comibmdb2jdbcappDB2Driver" />
<property name="url" value="jdbc:db2:thin:@localhost:5000/testDB" />
<property name="username" value="rootl" />
<property name="password" value="1234" />
</bean>
三 SQL Server
<bean id="dataSource" class="orgapachecommonsdbcpBasicDataSource" destroy-method="close">
<property name="driverClassName" value="commicrosoftjdbcsqlserverSQLServerDriver" />
<property name="url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName = testDB" />
<property name="username" value="rootl" />
<property name="password" value="1234" />
</bean>
三 MySQL
<bean id="dataSource" class="orgapachecommonsdbcpBasicDataSource" destroy-method="close">
<property name="driverClassName" value="orgpostgresqlDriver" />
<property name="url" value="jdbc:postgresql://localhost/ testDB" />
<property name="username" value="rootl" />
<property name="password" value="1234" />
</bean>
orgapachecommonsdbcpBasicDataSource 需要commons-pooljar,commons-dbcp-122jar,commons-collections-32jar三个JAR包
我给你我原来写过的吧。很久以前的了。只给你连接数据库这些,其他的你自己看着写吧。
下面是我当初写的其中一个dao类,有注释。你看看,模仿着写吧,肯定能写出来。
package comdao;
import javasqlConnection;
import javasqlDate;
import javasqlDriverManager;
import javasqlPreparedStatement;
import javasqlResultSet;
import javasqlSQLException;
import javasqlStatement;
import javautilList;
import javaxservletjspjstlsqlResult;
import javaxservletjspjstlsqlResultSupport;
import comtoolBaseTool;
public class BillDAO {
// 数据库连接
private Connection con;
// 数据库SQL语句执行者对象
private Statement stmt;
// 数据库SQL语句执行者对象
private PreparedStatement pstmt;
// 数据库SQL查询结果集
private ResultSet rs;
// 打开数据库连接
private void openConnection() {
try {
// 加载数据库驱动
ClassforName("oraclejdbcdriverOracleDriver");
// 获得数据库连接
con = DriverManagergetConnection(
"jdbc:oracle:thin:@localhost:1521:orcllib", "zxl", "zxl");
} catch (ClassNotFoundException e) {
eprintStackTrace();
} catch (SQLException e) {
eprintStackTrace();
}
}
// 关闭数据库连接及资源
private void closeConnection() {
if (rs != null) {
try {
rsclose();
} catch (SQLException e) {
eprintStackTrace();
}
}
if (stmt != null) {
try {
stmtclose();
} catch (SQLException e) {
eprintStackTrace();
}
}
if (pstmt != null) {
try {
pstmtclose();
} catch (SQLException e) {
eprintStackTrace();
}
}
if (con != null) {
try {
conclose();
} catch (SQLException e) {
eprintStackTrace();
}
}
}
//检查账单编号是否存在的方法
@SuppressWarnings("finally")
public boolean checkBillNumber(long bill_number) {
boolean result = false;
openConnection();
// 编写SQL语句
String sql = "select bbill_number from bill b";
try {
// 实例化执行SQL语句的对象preparedstatement
stmt = concreateStatement();
// 执行查询语句并返回结果集
rs = stmtexecuteQuery(sql);
while (rsnext()) {
if (bill_number == rsgetLong("bill_number")) {
return result;
}
}
result = true;
} catch (SQLException e) {
eprintStackTrace();
} finally {
// 关闭数据库连接及资源
closeConnection();
return result;
}
}
//分页显示账单列表的方法
@SuppressWarnings("finally")
public Result getPagingBill(int page_number, int page_data_number) {
Result result = null;
// 打开数据库连接
openConnection();
// 编写SQL语句
String sql = "select rbill_number,rgoods_name,rgoods_number,rtransaction_amount," +
"rvalue_name,rsupplier_name,rgoods_description,rbill_date from " +
"(select rownum as rn,b from bill_list_view b) r where rrn between "+
BaseToolgetPagingString(page_number, page_data_number);
try {
// 实例化执行SQL语句的对象preparedstatement
stmt = concreateStatement();
// 执行查询语句并返回结果集
rs = stmtexecuteQuery(sql);
// 将结果集储存在Result对象当中
result = ResultSupporttoResult(rs);
} catch (SQLException e) {
eprintStackTrace();
} finally {
// 关闭数据库连接及资源
closeConnection();
return result;
}
}
//获得账单总数的方法
@SuppressWarnings("finally")
public int getBillCount() {
int result = 0;
// 打开数据库连接
openConnection();
// 编写SQL语句
String sql = "select count(bbill_number) from bill_list_view b ";
try {
// 实例化执行SQL语句的对象preparedstatement
stmt = concreateStatement();
// 执行查询语句并返回结果集
rs = stmtexecuteQuery(sql);
// 取得账单总数
while (rsnext()) {
result = rsgetInt(1);
}
} catch (SQLException e) {
eprintStackTrace();
} finally {
// 关闭数据库连接及资源
closeConnection();
return result;
}
}
//将用户组合查询的账单列表分页
@SuppressWarnings("finally")
public Result getFilterPagingBill(String goods_name, int pay_status ,
int page_number , int page_data_number){
Result result = null;
// 打开数据库连接
openConnection();
// 编写SQL语句
String sql = "select rbill_number,rgoods_name,rgoods_number,rtransaction_amount," +
"rvalue_name,rsupplier_name,rgoods_description,rbill_date " +
" from (select rownum as rn,b from bill_list_view b where ";
String sql_last = ") r where rrn between " + BaseToolgetPagingString(page_number, page_data_number);
// 将参数初始化
String value_name = "";
if (pay_status == 3)
value_name = "已付款";
else if (pay_status == 4)
value_name = "未付款";
else
value_name = null;
if (""equals(goods_nametrim()))
goods_name = null;
// 按照用户查询的条件处理
try {
if (value_name != null && goods_name == null) {
sql = sql + "bvalue_name=" + sql_last;
pstmt = conprepareStatement(sql);
pstmtsetString(1, value_name);
} else if (value_name == null && goods_name != null) {
goods_name = "%"+goods_name+"%";
sql = sql + "bgoods_name like " + sql_last;
pstmt = conprepareStatement(sql);
pstmtsetString(1, goods_name);
} else {
goods_name = "%"+goods_name+"%";
sql = sql + "bvalue_name= and bgoods_name like " + sql_last;
pstmt = conprepareStatement(sql);
pstmtsetString(1, value_name);
pstmtsetString(2, goods_name);
}
rs = pstmtexecuteQuery();
result = ResultSupporttoResult(rs);
} catch (SQLException e) {
eprintStackTrace();
} finally {
closeConnection();
return result;
}
}
//获得用户组合查询的账单总数
@SuppressWarnings("finally")
public int getFilterBillCount(String goods_name, int pay_status) {
int result = 0;
// 打开数据库连接
openConnection();
// 编写SQL语句
String sql = "select count(bbill_number) from bill_list_view b where ";
// 将参数初始化
String value_name = "";
if (pay_status == 3)
value_name = "已付款";
else if (pay_status == 4)
value_name = "未付款";
else
value_name = null;
if (""equals(goods_nametrim()))
goods_name = null;
// 按照用户查询的条件处理
try {
if (value_name != null && goods_name == null) {
sql = sql + "bvalue_name= ";
pstmt = conprepareStatement(sql);
pstmtsetString(1, value_name);
} else if (value_name == null && goods_name != null) {
goods_name="%"+goods_name+"%";
sql = sql + "bgoods_name like ";
pstmt = conprepareStatement(sql);
pstmtsetString(1, goods_name);
} else {
goods_name="%"+goods_name+"%";
sql = sql + "bvalue_name= and bgoods_name like ";
pstmt = conprepareStatement(sql);
pstmtsetString(1, value_name);
pstmtsetString(2, goods_name);
}
rs = pstmtexecuteQuery();
while(rsnext()){
result = rsgetInt(1);
}
} catch (SQLException e) {
eprintStackTrace();
} finally {
closeConnection();
return result;
}
}
//根据账单编号获得账单详细信息的方法
@SuppressWarnings("finally")
public Result getBillForBillNumber(long bill_number){
Result result = null;
// 打开数据库连接
openConnection();
// 编写SQL语句
String sql = "select b from bill_list_view b where bbill_number="+bill_number;
try {
// 实例化执行SQL语句的对象preparedstatement
stmt = concreateStatement();
// 执行查询语句并返回结果集
rs = stmtexecuteQuery(sql);
// 将结果集储存在Result对象当中
result = ResultSupporttoResult(rs);
} catch (SQLException e) {
eprintStackTrace();
} finally {
// 关闭数据库连接及资源
closeConnection();
return result;
}
}
//为账单增加数据的方法
@SuppressWarnings("finally")
public int updateBill(List<Object> values){
//用于返回执行结果
int result = 0;
// 打开数据库连接
openConnection();
// 编写SQL语句
String sql = "insert into bill values (,,,,,,,)";
try {
// 实例化执行SQL语句的对象preparedstatement
pstmt = conprepareStatement(sql);
//设置参数
for(int i=0,j=1;i<valuessize();i++,j++){
if(valuesget(i) instanceof Integer){
pstmtsetInt(j, (Integer)valuesget(i));
}else if(valuesget(i) instanceof Long){
pstmtsetLong(j, (Long)valuesget(i));
} else if(valuesget(i) instanceof String){
pstmtsetString(j, (String)valuesget(i));
}else{
pstmtsetDate(j, (Date)valuesget(i));
}
}
//获得执行结果
result = pstmtexecuteUpdate();
} catch (SQLException e) {
eprintStackTrace();
} finally {
// 关闭数据库连接及资源
closeConnection();
//返回执行结果
return result;
}
}
//为账单修改数据的方法
@SuppressWarnings("finally")
public int saveBill(List<Object> values){
//用于返回执行结果
int result = 0;
// 打开数据库连接
openConnection();
// 编写SQL语句
String sql = "update bill b set bgoods_name=,bgoods_number=,btransaction_amount=," +
"bpay_status=,bsupplier_number=,bgoods_description=,bbill_date= where bbill_number=";
try {
// 实例化执行SQL语句的对象preparedstatement
pstmt = conprepareStatement(sql);
//设置参数
for(int i=0,j=1;i<valuessize();i++,j++){
if(valuesget(i) instanceof Integer){
pstmtsetInt(j, (Integer)valuesget(i));
}else if(valuesget(i) instanceof Long){
pstmtsetLong(j, (Long)valuesget(i));
}else if(valuesget(i) instanceof String){
pstmtsetString(j, (String)valuesget(i));
}else{
pstmtsetDate(j, (Date)valuesget(i));
}
}
//获得执行结果
result = pstmtexecuteUpdate();
} catch (SQLException e) {
eprintStackTrace();
} finally {
// 关闭数据库连接及资源
closeConnection();
//返回执行结果
return result;
}
}
//删除账单中数据的方法
@SuppressWarnings("finally")
public int deleteBill(long bill_number){
//用于返回执行结果
int result = 0;
// 打开数据库连接
openConnection();
// 编写SQL语句
String sql = "delete bill b where bbill_number=";
try {
// 实例化执行SQL语句的对象preparedstatement
pstmt = conprepareStatement(sql);
//设置参数
pstmtsetLong(1, bill_number);
//获得执行结果
result = pstmtexecuteUpdate();
} catch (SQLException e) {
eprintStackTrace();
} finally {
// 关闭数据库连接及资源
closeConnection();
//返回执行结果
return result;
}
}
}
这个配置好像很容易找到例子的,或许在配置文件中,我记得是一个XML文件,把相应的注释去掉,然后把服务器、端口、数据库类型、用户名、密码、数据库等信息修改好就行了。
这是我的一段配置代码,希望对你有用:
D:\apache-tomcat-704\webapps\cas-server-webapp-3431\WEB-INF\spring-configuration\applicationContextxml
<bean id="dataSource" class="orgspringframeworkjdbcdatasourceDriverManagerDataSource">
<property name="driverClassName" value="commysqljdbcDriver"></property>
<property name="url" value="jdbc:mysql://localhost/database"></property>
<property name="username" value="username"></property>
<property name="password" value="password"></property>
</bean>
以上就是关于jsp中怎样向oracle中插入数据代码全部的内容,包括:jsp中怎样向oracle中插入数据代码、怎么在jsp页面中读取Oracle数据并把数据读取结果放在div中、简单的jsp连接oracle数据库问题!但是连不通,我是新手请大家帮忙!!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)