jsp+jdbc實現(xiàn)連接數(shù)據(jù)庫的方法
本文實例講述了jsp+jdbc實現(xiàn)連接數(shù)據(jù)庫的方法。分享給大家供大家參考。具體如下:
初次嘗試JSP+jdbc,按照書上的例子折騰了半天,就是連不上數(shù)據(jù)庫。于是在網(wǎng)上找材料,終于發(fā)現(xiàn),老的jar包與新版數(shù)據(jù)庫直接不兼容。于是下了新的數(shù)據(jù)庫jdbc包,試了一下,果然搞定。這里,把這個程序跟大家共享下,程序實現(xiàn)了網(wǎng)頁登錄界面上提取用戶名與密碼,然后與數(shù)據(jù)庫中用戶名密碼對應,從而決定程序是否通過登錄。
inc.jsp文件:
<%@ page import="java.sql.Connection"%> <%@ page import="java.sql.DriverManager"%> <%@ page import="java.sql.Statement"%> <%@ page import="java.sql.ResultSet"%> <%@ page import="java.sql.ResultSetMetaData"%> <% String drv = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/demo"; String usr = "nari"; String pwd = "nari"; %>
welcome.jsp文件:
<html> <body> welcome<br> </body> </html>
login_action.jsp文件:
<%@ include file="inc.jsp" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username == null || password == null){
response.sendRedirect("index.jsp");
}
boolean isValid = false;
String sql = "select * from user where username='"+username+"'and password='"+password+"'";
out.println("===>"+sql);
try{
Class.forName(drv).newInstance();
Connection conn = DriverManager.getConnection(url, usr,pwd);
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery(sql);
if(rs.next())isValid = true;
rs.close();
stm.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
out.println(e);
}
if(isValid){
response.sendRedirect("welcome.jsp");
}else response.sendRedirect("index.jsp");
%>
<% /*
if(username.endsWith("a"))response.sendRedirect("welcome.jsp");
else response.sendRedirect("index.jsp");
*/%>
index.jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath %>"/>
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="description" content="This is my page">
</head>
<body>
miThis is my JSP page.<br>
</body>
</html>
<form name="form1" action="login_action.jsp" method="post">
<table width="200" border="1">
<tr>
<td colspan="2">登錄窗口</td>
</tr>
<tr>
<td>用戶名</td>
<td><input type="text" name="username" size="10"/></td>
</tr>
<tr>
<td>密碼</td>
<td><input type ="password" name="password" size="10"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="登錄">
<a href="register.jsp">注冊新用戶</a></td>
</tr>
</table>
</form>
程序使用tomcat進行發(fā)布,myeclipse進行編輯和調(diào)試
希望本文所述對大家的jsp程序設計有所幫助。
- JSP使用JDBC連接MYSQL數(shù)據(jù)庫的方法
- JSP實現(xiàn)從數(shù)據(jù)庫導出數(shù)據(jù)到Excel下載的方法
- jsp從數(shù)據(jù)庫獲取數(shù)據(jù)填充下拉框實現(xiàn)二級聯(lián)動菜單的方法
- JSP上傳excel及excel插入至數(shù)據(jù)庫的方法
- jsp+mysql數(shù)據(jù)庫操作常用方法實例總結
- JSP中使用JDBC訪問SQL Server 2008數(shù)據(jù)庫示例
- Java實現(xiàn)JSP在Servelt中連接Oracle數(shù)據(jù)庫的方法
- jsp讀取數(shù)據(jù)庫實現(xiàn)分頁技術簡析
- jsp 從web.xml讀取連接數(shù)據(jù)庫的參數(shù)
- jsp頁面顯示數(shù)據(jù)庫的數(shù)據(jù)信息表
相關文章
jsp實現(xiàn)針對excel及word文檔的打印方法
這篇文章主要介紹了jsp實現(xiàn)針對excel及word文檔的打印方法,涉及JSP操作excel及word的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
Jsp中解決session過期跳轉到登陸頁面并跳出iframe框架的方法
這里我們是介紹一個網(wǎng)站管理后臺三個框架頁面當我們的jsp定義的session變量超時時用戶點擊時自動退出框架頁面并跳到登錄頁面去了,下面我來給大家演示一個實例2013-08-08
jsp response.sendRedirect不跳轉的原因分析及解決
最近做項目時遇到一個問題,明明加了response.sendRedirect() ,系統(tǒng)也執(zhí)行了,但是它就是不跳轉,具體的原因如下,感興趣的各位可以參考下哈,希望對大家有所幫助2013-07-07

