jsp连接oracle的方法,包括对数据库进行操作的方法。最好给个简单的例子
发布网友
发布时间:2022-04-09 12:32
我来回答
共3个回答
懂视网
时间:2022-04-09 16:54
"oracle.jdbc.driver.OracleDriver");
Connection conn=null;
String user=request.getParameter("userName");
String password=request.getParameter("password");
try
{
conn =DriverManager.getConnection("oracle.jdbc.driver.OracleDriver",user,password);
out.write("连接成功!");
Statement st = conn.createStatement();
st.close();
//管理连接
conn.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
%>
</body>
<body>
<form name=‘frm‘ action=‘dengru.jsp‘>
用户名:<input type="text" name="userName">
密码:<input type="password" name="password">
<input type="submit" value="确定">
</form>
</body>
jsp登入oracle
标签:管理 color ack ram exce catch body stack ext
热心网友
时间:2022-04-09 14:02
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上去就行了
热心网友
时间:2022-04-09 15:20
#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]);}