婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > jsp+jdbc實現連接數據庫的方法

jsp+jdbc實現連接數據庫的方法

熱門標簽:電銷機器人好賣么 地圖標注線上教程 電銷機器人是有一些什么技術 北票市地圖標注 高德地圖標注樣式 商洛電銷 四川保險智能外呼系統商家 杭州ai語音電銷機器人功能 杭州語音電銷機器人軟件

本文實例講述了jsp+jdbc實現連接數據庫的方法。分享給大家供大家參考。具體如下:

初次嘗試JSP+jdbc,按照書上的例子折騰了半天,就是連不上數據庫。于是在網上找材料,終于發現,老的jar包與新版數據庫直接不兼容。于是下了新的數據庫jdbc包,試了一下,果然搞定。這里,把這個程序跟大家共享下,程序實現了網頁登錄界面上提取用戶名與密碼,然后與數據庫中用戶名密碼對應,從而決定程序是否通過登錄。

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> 
  welcomebr>
  /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進行發布,myeclipse進行編輯和調試

希望本文所述對大家的jsp程序設計有所幫助。

您可能感興趣的文章:
  • JSP使用JDBC連接MYSQL數據庫的方法
  • JSP實現從數據庫導出數據到Excel下載的方法
  • jsp從數據庫獲取數據填充下拉框實現二級聯動菜單的方法
  • JSP上傳excel及excel插入至數據庫的方法
  • jsp+mysql數據庫操作常用方法實例總結
  • JSP中使用JDBC訪問SQL Server 2008數據庫示例
  • Java實現JSP在Servelt中連接Oracle數據庫的方法
  • jsp讀取數據庫實現分頁技術簡析
  • jsp 從web.xml讀取連接數據庫的參數
  • jsp頁面顯示數據庫的數據信息表

標簽:青島 云浮 江西 丹東 宿州 西藏 貴州 紅河

巨人網絡通訊聲明:本文標題《jsp+jdbc實現連接數據庫的方法》,本文關鍵詞  jsp+jdbc,實現,連接,數據庫,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《jsp+jdbc實現連接數據庫的方法》相關的同類信息!
  • 本頁收集關于jsp+jdbc實現連接數據庫的方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 郎溪县| 宁城县| 凉山| 泾川县| 札达县| 越西县| 云和县| 上犹县| 赣州市| 延安市| 梁平县| 通城县| 随州市| 铜山县| 新化县| 宜宾市| 闸北区| 迁安市| 大丰市| 曲水县| 壶关县| 临清市| 外汇| 台山市| 乌拉特中旗| 新源县| 吉林省| 襄樊市| 屯留县| 仁怀市| 田林县| 封丘县| 漳州市| 喜德县| 永清县| 高尔夫| 泰来县| 大安市| 金华市| 阳新县| 诸城市|