在myeclispe中使用JDBC直接訪問oracle數據庫

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();

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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章