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
<%@page contentType="text/htmlcharset=gb2312"%><%@page import="java.sql.*"%>
<HTML><BODY>
<hr align="center" width="55%" color="#991111" size="5"/>
<%
Connection conn = null
PreparedStatement stmt = null
ResultSet rs = null
request.setCharacterEncoding("GBK")
try{
Class.forName("oracle.jdbc.driver.OracleDriver")
}
catch(ClassNotFoundException ce){
out.println(ce.getMessage())
}
try{
String url="jdbc:oracle:thin:@localhost:1521:orcl"
conn=DriverManager.getConnection(url,"scott","tiger")
stmt=conn.prepareStatement("select * from emp")
rs=stmt.executeQuery()
out.print("<TABLE BORDER='1' width='55%' cellpadding='3' align=center>")
out.print("<TR><TD>"+"<b>Empno</b>"+"</TD>")
out.print("<TD>"+"<b>Ename</b>"+"</TD>")
out.print("<TD>"+"<b>Job</b>"+"</TD>")
out.print("<TD>"+"<b>Mgr</b>"+"</TD>")
out.print("<TD>"+"<b>hiredate</b>"+"</TD>")
out.print("<TD align=center>"+"<b>Sal</b>"+"</TD>")
out.print("<TD>"+"<b>Comm</b>"+"</TD>")
out.print("<TD>"+"<b>Deptno</b>"+"</TD></TR>")
while(rs.next()){
out.print("<TR><TD>"+rs.getString(1)+"</TD>")
out.print("<TD>"+rs.getString(2)+"</TD>")
out.print("<TD >"+rs.getString(3)+"</TD>")
out.print("<TD>"+rs.getString(4)+"</TD>")
out.print("<TD>"+rs.getString(5)+"</TD>")
out.print("<TD>"+rs.getString(6)+"</TD>")
out.print("<TD>"+rs.getString(7)+"</TD>")
out.print("<TD>"+rs.getString(8)+"</TD></TR>")
}
out.print("</TABLE>")
rs.close()
stmt.close()
conn.close()
}
catch(Exception e){
System.out.println(e.getMessage())
}
%>
</BODY></HTML>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)