您的位置:首页 > 数据库 > Oracle

在myeclispe中使用JDBC直接访问oracle数据库

2013-11-19 21:47 274 查看
1.新建项目



2.文件名称重构





3.修改编码方式,将pageEncoding修改为GB18030,以支持中文。

 


 


 

4.添加Oracle访问类库,只需要ojdbc5.jar一个就可以了。

\\vmware-host\Shared Folders\F\待处理20130611\小软件\JAVA类库\spring类库\ojdbc5.jar





 

5.重新部署web程序



 



6.先将自动启动的tomcat服务器停止。

D:\ZhaohyInstalledSoftware\Apache Software Foundation\Tomcat 6.0\bin





7.在myeclipse中启动服务器



程序启动,使用网址测试:

http://localhost:8090/Spring_2300_Registration_1/register.jsp



8.表脚本。

-- Create table
createtable myuser
(
  id      numbernotnull,
  username
varchar2(20)notnull,
  passwordvarchar2(20)notnull
)
;
-- Create/Recreate primary, unique and foreign key constraints
altertable myuser
  addconstraint idpkprimarykey
(ID);

9.代码文件:

register.jsp

<%@ pagelanguage="java"import="java.util.*"pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <basehref="<%=basePath%>">
   
    <title>用户注册</title>
      <metahttp-equiv="pragma"content="no-cache">
      <metahttp-equiv="cache-control"content="no-cache">
      <metahttp-equiv="expires"content="0">   
      <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
      <metahttp-equiv="description"content="This
is my page">
      <!--
      <link rel="stylesheet" type="text/css" href="styles.css">
      -->
  </head>
 
  <body>
   <formmethod="post"action="registerDeal.jsp">
   用户名:<inputtype="text"name="username"><br>
   密码:<inputtype="password"name="password"><br>
   确认密码:<inputtype="password"name="password2"><br>
   <inputtype="submit"value="提交">
   </form>  <br>
  </body>
</html>
registerDeal.jsp

<%@ pagelanguage="java"import="java.util.*,java.sql.*"pageEncoding="GB18030"%>
<%
String path = request.getContextPath
ff04
();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String username=request.getParameter("username");
String password=request.getParameter("password");
String password2=request.getParameter("password2");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCLLEAR","888888","666666");
 
String sqlQuery="select count(*) from myuser where username = ?";
PreparedStatement psQuery=conn.prepareStatement(sqlQuery);
psQuery.setString(1,username);
ResultSet rs=psQuery.executeQuery();
rs.next();
int count=rs.getInt(1);
if(count >0){
 
 
response.sendRedirect("registerFail.jsp");
psQuery.close();
psQuery.close();
return;
}
 
 
 
 
 
 
String sql="insert into myuser (id, username, password) values (?, ?, ?)";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1,1);
ps.setString(2,username);
ps.setString(3,password);
ps.executeUpdate();
ps.close();
conn.close();
 
response.sendRedirect("registerSuccess.jsp");
%>
 
registerFail.jsp

<%@ pagelanguage="java"import="java.util.*"pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <basehref="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
      <metahttp-equiv="pragma"content="no-cache">
      <metahttp-equiv="cache-control"content="no-cache">
      <metahttp-equiv="expires"content="0">   
      <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
      <metahttp-equiv="description"content="This
is my page">
      <!--
      <link rel="stylesheet" type="text/css" href="styles.css">
      -->
  </head>
 
  <body>
    注册失败<br>
  </body>
</html>
registerSuccess.jsp

<%@ pagelanguage="java"import="java.util.*"pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <basehref="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
      <metahttp-equiv="pragma"content="no-cache">
      <metahttp-equiv="cache-control"content="no-cache">
      <metahttp-equiv="expires"content="0">   
      <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
      <metahttp-equiv="description"content="This
is my page">
      <!--
      <link rel="stylesheet" type="text/css" href="styles.css">
      -->
  </head>
 
  <body>
    注册成功!<br>
  </body>
</html>
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  myeclipse oracle