1、连入SQL*Plus
以system/manager用户登录,
SQL>conn system/manager
创建新的用户:如user1/pass1,赋予connect,resource权限。
SQL>grant connect,resource to user1 identified by pass1
SQL>conn user1/pass1
SQL>create table test(a number,b char(10))
SQL>insert into test values(1,"一")
SQL>insert into test values(2,"二")
SQL>insert into test values(3,"三")
SQL>commit
SQL>select * from test
A B
---------- ----------
1 一
2 二
3 三
2、配置ODBC(在服务器端设置)
开始->设置->控制面板->管理工具->数据源 (ODBC)->系统DSN->添加->
选择“Oracle ODBC Driver”->完成->
Data Source Name:test名字随便取,好记就行,如:test
Service Name:oradb Oracle数据库的实例名,本例:oradb,一般Oracle默认安装为ORCL
UserID:user1 Oracle用户名,本例:user1
按“OK”->按“确定”退出
3、在C:\JBuilder4\tomcat\webapps\test下创建test.jsp,用notebook编辑,输入以下代码
<!--首先导入一些必要的packages-->
<%@ page info="database handler"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<!--告诉编译器使用SQL包-->
<%@ page import="java.sql.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%
//以try开始
try
{
Connection con
Statement stmt
ResultSet rs
//加载驱动程序,下面的代码为加载JDBD-ODBC驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
//用适当的驱动程序连接到数据库,test"是系统dsn名
String url="jdbc:odbc:test"
//建立连接,类似于ASP中的创建数据库联接
con=DriverManager.getConnection(url, "user1", "pass1")
//创建一个JDBC声明
stmt = con.createStatement()
//增加新记录
stmt.executeUpdate("INSERT INTO test (a,b) VALUES (1,"2")")
//查询记录
rs = stmt.executeQuery("SELECT a,b from test")
//输出查询结果
out.println("<table border=1 width=400>")
while (rs.next())
{
String col1 = rs.getString(1)
String col2 = rs.getString(2)
//打印所显示的数据
out.println("<tr><td>"+col1+"</td><td>"+col2+"</td></tr>")
}
out.println("</table>")
}
//如果加载时出错,给出相应的错误信息
catch (Exception e) {}
%>
4、打开一个浏览器窗口,输入以下地址来查看运行结果
http://localhost:8080/test/test.jsp
<span style="font-size:12px"><span style="font-size:14px">
<%@ page language="java" import="java.sql.*,java.io.*,java.util.*"%>
<%@ page contentType="text/htmlcharset=utf-8"%>
<%@ page import="com.oracle.enjoyshop.base.ConnectionFactory" %> /*引用数据库连接类*/
<%@ page import="com.oracle.enjoyshop.util.JDBCUtil" %> /*引用资源关闭类*/
<html>
<head>
<title>从数据库中的某个表拿到该表所有的数据</title>
<style type="text/css">
table {
border: 2px #CCCCCC solid
width: 360px
}
td,th {
height: 30px
border: #CCCCCC 1px solid
}
</style>
</head>
<body>
<%
Connection conn = null
Statement stmt = null
PreparedStatement pstmt = null
ResultSet rs = null
conn = ConnectionFactory.getConnection()
String findSQL = "select u_id,u_name,u_pwd,u_tel,u_email,u_birthday,u_money from zzl_user "
pstmt = conn.prepareStatement(findSQL)
rs=pstmt.executeQuery()
%>
<br>
<br>
<table align="center">
<tr>
<th>
<%
out.print("ID")
%>
</th>
<th>
<%
out.print("NAME")
%>
</th>
<th>
<%
out.print("PWD")
%>
</th>
<th>
<%
out.print("TEL")
%>
</th>
<th>
<%
out.print("EMAIL")
%>
</th>
<th>
<%
out.print("BRITH")
%>
</th>
<th>
<%
out.print("MENOY")
%>
</th>
</tr>
<%
while (rs.next()) {
%>
<tr>
<td>
<%
out.print(rs.getLong(1))
%>
</td>
<td>
<%
out.print(rs.getString(2))
%>
</td>
<td>
<%
out.print(rs.getString(3))
%>
</td>
<td>
<%
out.print(rs.getString(4))
%>
</td>
<td>
<%
out.print(rs.getString(5))
%>
</td>
<td>
<%
out.print(rs.getDate(6))
%>
</td>
<td>
<%
out.print(rs.getDouble(7))
%>
</td>
</tr>
<%
}
%>
</table>
<%
JDBCUtil.close(conn, pstmt, stmt, rs)
%>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)