求一myeclipse(Java)链接oracle数据库的完整实例代码
展开全部
一、连接数据库
<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.sql.*"%>
<%!
public static final String DBDRIVER="oracle.jdbc.driver.OracleDriver";
public static final String DBURL="jdbc:oracle:thin:@localhost:1521:MLDN";
public static final String DBUSER="用户名";
public static final String DBPASS="密码";
%>
<%
Connection conn=null; //数据库连接
PreparedStatement pre=null; //数据库操作
ResultSet rs=null; //数据库的查询结果集
%>
<%
Class.forName(DBDRIVER); //加载驱动程序
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
String sql="select empno,ename,job,hiredate,sal,comm from emp";
pre=conn.prepareStatement(sql);
rs=pre.executeQuery();
%>
<center>
<h1>官员管理</h1>
<table border="1" width="80%">
<tr>
<td>雇员编号</td>
<td>雇员姓名</td>
<td>工作</td>
<td>雇佣日期</td>
<td>工资</td>
<td>奖金</td>
</tr>
<% while(rs.next()){
int empno=rs.getInt(1);
String ename=rs.getString(2);
String job=rs.getString(3);
java.util.Date hiredate=rs.getDate(4);
float sal=rs.getFloat(5);
float comm=rs.getFloat(6);
%>
<tr>
<td><%=empno%></td>
<td><%=ename%></td>
<td><%=job%></td>
<td><%=hiredate%></td>
<td><%=sal%></td>
<td><%=comm%></td>
</tr>
<%
}
%>
<%
conn.close();
pre.close();
rs.close();
%>
</table>
</center>
<%@page contentType="text/html;charset=GBK"%>
<%@page import="java.sql.*"%>
<%!
public static final String DBDRIVER="oracle.jdbc.driver.OracleDriver";
public static final String DBURL="jdbc:oracle:thin:@localhost:1521:MLDN";
public static final String DBUSER="用户名";
public static final String DBPASS="密码";
%>
<%
Connection conn=null; //数据库连接
PreparedStatement pre=null; //数据库操作
ResultSet rs=null; //数据库的查询结果集
%>
<%
Class.forName(DBDRIVER); //加载驱动程序
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
String sql="select empno,ename,job,hiredate,sal,comm from emp";
pre=conn.prepareStatement(sql);
rs=pre.executeQuery();
%>
<center>
<h1>官员管理</h1>
<table border="1" width="80%">
<tr>
<td>雇员编号</td>
<td>雇员姓名</td>
<td>工作</td>
<td>雇佣日期</td>
<td>工资</td>
<td>奖金</td>
</tr>
<% while(rs.next()){
int empno=rs.getInt(1);
String ename=rs.getString(2);
String job=rs.getString(3);
java.util.Date hiredate=rs.getDate(4);
float sal=rs.getFloat(5);
float comm=rs.getFloat(6);
%>
<tr>
<td><%=empno%></td>
<td><%=ename%></td>
<td><%=job%></td>
<td><%=hiredate%></td>
<td><%=sal%></td>
<td><%=comm%></td>
</tr>
<%
}
%>
<%
conn.close();
pre.close();
rs.close();
%>
</table>
</center>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
String dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver" ;
String dbPath = "jdbc:odbc:hello" ;
String username = "sa" ;
String password= "" ;
Connection conn = null ;
Statement st = null ;
ResultSet rs = null ;
public DB() throws Exception
{
Class.forName(dbDriver).newInstance() ;
//创建数据库连接对象
conn = DriverManager getConnection(dbPath, username, password) ;
//创建数据操作对象
st = conn.createStatment() ;
}
public ResultSet query() throws Exception
{
ResultSet rs = st.executeQuery("查询语句") ;
return rs ;
closeDB() ;
}
public void closeDB() throws Exception
{
if(rs != null)
{
rs.close() ;
rs = null ;
}
if(st != null)
{
st.close() ;
st = null ;
}
if(conn != null)
{
conn.close() ;
conn = null ;
}
}
String dbPath = "jdbc:odbc:hello" ;
String username = "sa" ;
String password= "" ;
Connection conn = null ;
Statement st = null ;
ResultSet rs = null ;
public DB() throws Exception
{
Class.forName(dbDriver).newInstance() ;
//创建数据库连接对象
conn = DriverManager getConnection(dbPath, username, password) ;
//创建数据操作对象
st = conn.createStatment() ;
}
public ResultSet query() throws Exception
{
ResultSet rs = st.executeQuery("查询语句") ;
return rs ;
closeDB() ;
}
public void closeDB() throws Exception
{
if(rs != null)
{
rs.close() ;
rs = null ;
}
if(st != null)
{
st.close() ;
st = null ;
}
if(conn != null)
{
conn.close() ;
conn = null ;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<body>
<% Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String user="system";
String password="a123";
Connection conn=DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from table1";
ResultSet rs=stmt.executeQuery(sql);
if(rs.next())
%>
<% {%>
第一字段:<%=rs.getString(1) %>
<br>
第二字段:<%=rs.getString(2) %>
<br>
<% }%>
<%out.print("数据库操作成功");%>
<%
rs.close();
stmt.close();
conn.close();
%>
</body>
这是我在jsp中写的,完全可以连
<% Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String user="system";
String password="a123";
Connection conn=DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from table1";
ResultSet rs=stmt.executeQuery(sql);
if(rs.next())
%>
<% {%>
第一字段:<%=rs.getString(1) %>
<br>
第二字段:<%=rs.getString(2) %>
<br>
<% }%>
<%out.print("数据库操作成功");%>
<%
rs.close();
stmt.close();
conn.close();
%>
</body>
这是我在jsp中写的,完全可以连
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询