jsp连接oracle的方法,包括对数据库进行操作的方法。最好给个简单的例子

如题,通过jsp调用oracle数据库,用最简洁的语言告诉我,需要一个简单的链接例子高悬赏哦!... 如题,通过jsp调用oracle数据库,用最简洁的语言告诉我,需要一个简单的链接例子高悬赏哦! 展开
 我来答
匿名用户
2013-09-08
展开全部
package com.function;

import java.io.StringReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Vector;import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import com.function.DealString;import sun.jdbc.rowset.CachedRowSet;public class DBManager {
private static Connection conn = null;
private static final String jndiname="java:comp/env/jdbc/fb";
/** 以下为数据连接方式是连接池操作,需要事先配置好相应的连接池以及数据源 */
public static Connection getConnection(){ try {
Context initCtx = new InitialContext(); DataSource ds = (DataSource) initCtx.lookup(jndiname);
conn = ds.getConnection();
conn.setAutoCommit(true);
} catch (Exception e) {
e.printStackTrace();
}
return conn;

} /** 以下是连接ORACLE数据库 */
public static Connection getOracleConnection() {
String url = "jdbc:oracle:thin:@10.10.10.1:1521:hzw";
String driver = "oracle.jdbc.driver.OracleDriver";
String uid = "govstock";
String pwd = "govstock";
try {
Class.forName(driver);
} catch (ClassNotFoundException ex) {
System.out.println("装载驱动时出错!" + ex.getMessage());
}
try {
//conn = DriverManager.getConnection(url, uid, pwd);
conn=getConnection();
} catch (Exception ex1) {
System.out.println("得到连接时出错" + ex1.getMessage());
}
return conn;
} /** 以下是连接MSSQL数据库 */
public static Connection getMsSqlConnection() {
String url = "jdbc:microsoft:sqlserver://10.10.10.86:1433;DatabaseName=ccgp-cs1";
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String uid = "sa";
String pwd = "sa";
try {
Class.forName(driver);
} catch (ClassNotFoundException ex) {
System.out.println("装载驱动时出错!" + ex.getMessage());
}
try {
conn = DriverManager.getConnection(url, uid, pwd);
} catch (SQLException ex1) {
System.out.println("得到连接时出错" + ex1.getMessage());
}
return conn;
}
/** 以下是释放数据库资源Connection */
public static void cleanup(Connection conn) { try {
if (conn != null && !conn.isClosed()) {
conn.close();

}
} catch (Exception e) {
e.printStackTrace();
}
}
/** 以下是释放数据库资源Connection,PreparedStatement */
public static void cleanup(Connection conn, PreparedStatement ps) {
try { if (ps != null) {
ps.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/** 以下是释放数据库资源Connection,PreparedStatement,ResultSet */
public static void cleanup(Connection conn, PreparedStatement ps,
ResultSet rs) { try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {
}
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/** 以下是执行查询操作时需要带入查询参数 返回CachedRowSet*/
public static CachedRowSet executeQuery(String sql, Object[] values) {
CachedRowSet crs = null;
PreparedStatement st = null;
Connection conn = null;
ResultSet rs = null;
try {
crs = new CachedRowSet();
if (values == null || values.length < 1) {
return executeQueryCachedRowSet(sql);
}
conn = getOracleConnection();
st = conn.prepareStatement(sql);
for (int i = 0; i < values.length; i++) {
st.setObject(i + 1, values[i]);
}
rs = st.executeQuery();
crs.populate(rs);
} catch (Exception se) {
System.out
.println("SQLException in DBManager.exceuteQuery, sql is :\n"
+ sql);
se.printStackTrace();
} finally {
DBManager.cleanup(conn, null, rs);
}
return crs;
}

/** 以下是执行查询操作时需要带入查询参数、数据库连接 返回CachedRowSet */
public static CachedRowSet executeQuery(String sql, Object[] values,Connection conn) {
CachedRowSet crs = null;
PreparedStatement st = null;
// Connection conn = null;
ResultSet rs = null;
try {
crs = new CachedRowSet();
if (values == null || values.length < 1) {
return executeQueryCachedRowSet(sql);
}
// conn = getOracleConnection();
st = conn.prepareStatement(sql);
for (int i = 0; i < values.length; i++) {
st.setObject(i + 1, values[i]);
}
rs = st.executeQuery();
crs.populate(rs);
} catch (Exception se) {
System.out
.println("SQLException in DBManager.exceuteQuery, sql is :\n"
+ sql);
se.printStackTrace();
} finally {
DBManager.cleanup(null, null, rs);
}
return crs;
}
/**执行sql返回CachedRowSet 带参数、数据库连接 */
public static CachedRowSet executeQueryCachedRowSet(String sql,Connection conn) {
CachedRowSet crs = null;
Statement st = null;

ResultSet rs = null;
try {
crs = new CachedRowSet();
st = conn.createStatement();
rs = st.executeQuery(sql);
crs.populate(rs);
}
catch (Exception se) {
System.out.println("SQLException in DBManager.executeQueryCachedRowSet, sql is :\n" +
sql);
se.printStackTrace();
}
finally {
DBManager.cleanup(null, null, rs);
}
return crs;
}
/**执行sql返回CachedRowSet 不带参数*/
public static CachedRowSet executeQueryCachedRowSet(String sql) {
CachedRowSet crs = null;
Statement st = null;
Connection conn = null;
ResultSet rs = null;
try {
crs = new CachedRowSet();
conn =getOracleConnection();
st = conn.createStatement();
rs = st.executeQuery(sql);
crs.populate(rs);
}
catch (Exception se) {
System.out.println("SQLException in DBManager.executeQueryCachedRowSet, sql is :\n" +
sql);
se.printStackTrace();
}
finally {
DBManager.cleanup(conn, null, rs);
}
return crs;
}
/** 以下是用来执行增加,删除,修改操作 */
public static int executeByLong(String sql, Object[] values) {
PreparedStatement ps = null;
int rows = 0;
try {
conn = getOracleConnection();
conn.setAutoCommit(false);
ps = conn.prepareStatement(sql);
if (values == null || values.length < 1) {
rows = ps.executeUpdate();
return rows;
}
for (int i = 0; i < values.length; i++) {
ps.setCharacterStream(i + 1,new StringReader(values[i].toString()),values[i].toString().length());
}
rows = ps.executeUpdate();
conn.commit();
} catch (Exception e) {
try {
conn.rollback();
} catch (Exception ex) {
ex.printStackTrace();

}
e.printStackTrace();
} finally {
cleanup(conn, ps);
}
return rows;
} }
DBManager类 想用的话,把里面的代码拷到jsp上去就行了
匿名用户
2013-09-08
展开全部
#include <stdio.h>
#include <string.h>
void main()
{
char *str[5],*tmp; //5个字符数组 str[0] str[1] str[2] str[3] str[4] 分别指向一个字符数组
int i,j;
str[0]="vsfsfs";
str[1]="hdgds";
str[2]="nddfc";
str[3]="khfgvcx";
str[4]="afcghhf";
for(j=0;j<4;j++)
for(i=0;i<4-j;i++)
if(strcmp(str[i],str[i+1])>0) //如果前面比后面大就交换
{
tmp=str[i];
str[i]=str[i+1];
str[i+1]=tmp;
}
for(i=0;i<5;i++)
printf("%s\n",str[i]);}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式