jsp数据库连接

jsp数据库连接,第1张

跟着我做就是了,这个可是我现场测试和编写的哦!!没有任何copy
(1)把mysql的驱动放到tomcat的lib中 驱动是这个
http://ftpupacza/pub/windows/MySQL/Downloads/Connector-J/mysql-connector-java-516zip
解压后在lib中有mysql-connector-java-516jar把这个文件放到tomcat的lib中5X的在tomcat/common/lib 60在tomcat/lib
(2)建一个很简单的表person就两个字段username和password,数据库名和数据库密码换成你的就是了
create database ibatis;--创建数据库
use ibatis;--使用数据库,以下表在该数据库中
create table person(username varchar(20),password varchar(20));--创建person表
(3)创建indexjsp和registjsp
1:
indexjsp 提交表单页面
<%@ page pageEncoding="GBK"%>
<html>
<head>
</head>
<body>
<form action="registjsp" method="post">
username :<input type = "text" name="name"/>
password :<input type = "password" name="password"/>
<input type = "submit" value="提交"/>
</form>
</body>
</html>
2:registjsp //用户注册同时显示所有用户
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="javasql"%>
<body>
<center>
<%
requestsetCharacterEncoding("GBK");
String uname=requestgetParameter("name"); //从表单获得
String pwd=requestgetParameter("password"); //从表单获得
String driver="commysqljdbcDriver"; //我用的是mysql官方驱动你自己换一下就是了 在这里有
String url="jdbc:mysql://localhost:3306/ibatisuser=root&password=yanghao"; //这是数据库连接地址Ibatis是数据库名称,user是用户password就是你的用户名,根据实际情况你修改
String sql="INSERT INTO person (username,password) VALUES('"+uname+"','"+pwd+"')"; //把indexjsp提交的两个数据插进数据库的数据库语句
Connection conn=null; //数据库连接
Statement stmt=null;
ResultSet rs = null; //查询结果
%>
<%
ClassforName(driver); //加载驱动
conn=DriverManagergetConnection(url); //获得连接
stmt=conncreateStatement();
stmtexecute(sql);//存入数据库
rs=stmtexecuteQuery("select from person"); //查询所有person语句
%>
<%
if(rs!=null){ //判断以下
while(rsnext()){
String username=rsgetString(1);
String password=rsgetString(2);
%>
<table>
<tr>
<td><%=username %></td>
<td><%=password %></td>
</tr>
</table>
<%
//关闭数据库连接,和开始的顺序是反的
rsclose();//关闭结果集
stmtclose();//关闭Statement
connclose();//关闭数据库连接
//ok完成了插入和查询 *** 作
}
}
%>
</center>
</body>

以上就是关于jsp数据库连接全部的内容,包括:jsp数据库连接、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10190352.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-06
下一篇2023-05-06

发表评论

登录后才能评论

评论列表(0条)

    保存