⑴ jsp做一个最简单的,连接数据库,实现增删改查人员姓名的功能。一定要非常简单的那种。
(1)把mysql的驱动放到tomcat的lib中
(2)建一个很简单的表person就两个字段username和password,数据库名和数据库密码换成你的就是了
create database ibatis;--创建数据库
use ibatis;--使用数据库,以下表在该数据库中
create table person(username varchar(20),password varchar(20));--创建person表
(3)创建index.jsp和regist.jsp
1:
index.jsp 提交表单页面
<%@ page pageEncoding="GBK"%>
<html>
<head>
</head>
<body>
<form action="regist.jsp" method="post">
username :<input type = "text" name="name"/>
password :<input type = "password" name="password"/>
<input type = "submit" value="提交"/>
</form>
</body>
</html>
2:regist.jsp //用户注册同时显示所有用户
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*"%>
<body>
<center>
<%
request.setCharacterEncoding("GBK");
String uname=request.getParameter("name"); //从表单获得
String pwd=request.getParameter("password"); //从表单获得
String driver="com.mysql.jdbc.Driver"; //我用的是mysql官方驱动你自己换一下就是了 在这里有
String url="jdbc:mysql://localhost:3306/ibatis?user=root&password=yanghao"; //这是数据库连接地址Ibatis是数据库名称,user是用户.password就是你的用户名,根据实际情况你修改
String sql="INSERT INTO person (username,password) VALUES('"+uname+"','"+pwd+"')"; //把index.jsp提交的两个数据插进数据库的数据库语句
Connection conn=null; //数据库连接
Statement stmt=null;
ResultSet rs = null; //查询结果
%>
<%
Class.forName(driver); //加载驱动
conn=DriverManager.getConnection(url); //获得连接
stmt=conn.createStatement();
stmt.execute(sql);//存入数据库
rs=stmt.executeQuery("select * from person"); //查询所有person语句
%>
<%
if(rs!=null){ //判断以下
while(rs.next()){
String username=rs.getString(1);
String password=rs.getString(2);
%>
<table>
<tr>
<td><%=username %></td>
<td><%=password %></td>
</tr>
</table>
<%
//关闭数据库连接,和开始的顺序是反的
rs.close();//关闭结果集
stmt.close();//关闭Statement
conn.close();//关闭数据库连接
//ok完成了插入和查询操作
}
}
%>
</center>
</body>
这也是我从网上找了一个例子,大概流程就是这样,慢慢来。
⑵ jsp增删改查怎么写
packagetest.;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.Statement;
importjava.util.ArrayList;
importjava.util.List;
importtest.entity.Emp;
importtest.util.BaseDao;
publicclassEmpDao{
//查找数据库Emp表所有人信息
publicList<Emp>findAll()throwsException{
Connectionconn=BaseDao.getCon();
Statementstat=conn.createStatement();
Stringsql="select*fromtblemp,tbldeptwheretbldept.deptid=tblemp.deptid";
ResultSetrs=stat.executeQuery(sql);
List<Emp>list=newArrayList<Emp>();
while(rs.next()){
Empe=newEmp();
e.setEmpid(rs.getInt("empid"));
e.setEname(rs.getString("ename"));
e.setDname(rs.getString("dname"));
list.add(e);
}
BaseDao.close(conn);
returnlist;
}
//往Emp表添加新员工
publicvoidsave(Empe)throwsException{
Connectionconn=BaseDao.getCon();
Stringsql="insertintotblemp(ename,egendar,deptid)values(?,?,?)";
PreparedStatementprep=conn.prepareStatement(sql);
prep.setString(1,e.getEname());
prep.setDouble(2,e.getEgendar());
prep.setInt(3,e.getDeptid());
prep.executeUpdate();
BaseDao.close(conn);
}
//根据id删除该员工
publicvoiddelete(intid)throwsException{
Connectionconn=BaseDao.getCon();
Stringsql="deletefromtblempwhereempid=?";
PreparedStatementprep=conn.prepareStatement(sql);
prep.setLong(1,id);
prep.executeUpdate();
BaseDao.close(conn);
}
//根据id查找该员工信息
publicEmpfindById(intid)throwsException{
Connectionconn=BaseDao.getCon();
Empe=null;
Stringsql="select*fromtblemp,tbldeptwhereempid=?andtbldept.deptid=tblemp.deptid";
PreparedStatementprep=conn.prepareStatement(sql);
prep.setLong(1,id);
ResultSetrs=prep.executeQuery();
if(rs.next()){
e=newEmp();
e.setEmpid(id);
e.setEname(rs.getString("ename"));
e.setEgendar(rs.getInt("egendar"));
e.setDname(rs.getString("dname"));
e.setDeptid(rs.getInt("deptid"));
}
returne;
}
//根据id修改该员工
publicvoipdate(Empe)throwsException{
Connectionconn=BaseDao.getCon();
Stringsql="updatetblempsetename=?,egendar=?,deptid=?whereempid=?";
PreparedStatementprep=conn.prepareStatement(sql);
prep.setString(1,e.getEname());
prep.setInt(2,e.getEgendar());
prep.setInt(3,e.getDeptid());
prep.setLong(4,e.getEmpid());
prep.executeUpdate();
BaseDao.close(conn);
}
}
//我把我写过的给你参考,你只需要修改成自己的字段就能用了。
需要用到哪个方法,就调用它。
EmpDao=newEmpDao();
//增加就.save(数据);
//删除就.delete(id);
//查找就.findAll();
//修改就.update(内容);
希望帮到你
⑶ 如何在JSP页面中实现对数据库的增删查改
首先我觉得你的问题不太明确,做增删改查,的话一般不用ajax,除非其中要用到单独的验证字段的时候采用,比如在注册时验证用户名,这里用到ajax查询用户名是否存在,返回给页面直接用流打回页面就行(比如:此用户名可用之类的)而其他的查询比如显示所有或者查询的结果为对象的话那看你用什么框架(controller),struts直接封装到值栈中,在页面用标签显示就行,不知道能不能帮到你
⑷ 谁能够用JSP编写简单的增删查改方法,用PreparedStatement方法
我给你一个连接SQL数据库和增加、浏览的。其余的要学会自己写。都很容易。 连接数据库代码: package com.accp.model.; import java.sql.*; //数据库连接 public class ConnectionManager { private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver"; private static final String URL="jdbc:sqlserver://localhost:1433;DatabaseName=JspStudent"; private static final String USER="sa"; private static final String PASSWORD="sa"; public static Connection getConnection(){ Connection conn = null; try{ Class.forName(DRIVER); conn=DriverManager.getConnection(URL,USER,PASSWORD); }catch(Exception e){ e.printStackTrace(); } return conn; } public static void closeConnection(Connection conn){ try{ if(conn!=null&&(!conn.isClosed())){ conn.close(); } }catch(Exception e){ e.printStackTrace(); } } } 插入代码: package com.accp.model.bean; import com.accp.model..ConnectionManager; import java.sql.*; public class InsertThing { public static void main(String[] args){ Connection conn = null; PreparedStatement pStatement=null; try{ conn=ConnectionManager.getConnection(); String sql="insert into dbo.Student values(?,'?)"; pStatement=conn.prepareStatement(sql); pStatement.setInt(1, 1); pStatement.setString(2, "张三"); pStatement.executeUpdate(); }catch(Exception e){ e.printStackTrace(); }finally{ ConnectionManager.closeConnection(conn); } } } 查询代码: package com.accp.model.bean; import com.accp.model..ConnectionManager; import java.sql.*; public class SelectThing { public static void main(String[] args) { Connection conn = null; PreparedStatement pStatement = null; ResultSet rSet = null; try { conn = ConnectionManager.getConnection(); // 查询数据SQL语句 String sql = "select * from Student"; // 查询操作 pStatement = conn.prepareStatement(sql); rSet = pStatement.executeQuery(); while (rSet.next()) { System.out.print("Id:" + rSet.getInt(1)+"\t"); System.out.println("Name:" + rSet.getString(2)); } } catch (Exception e) { e.printStackTrace(); } finally { ConnectionManager.closeConnection(conn); } } } 数据自己建好,表明叫Student。 删除和修改就和插入差不多。把SQL语句变一下就可以。 希望你继续加油。
采纳哦
⑸ jsp怎么写增删改查代码
下面的代码即可实现(对数据库的操作):
<%@page
language="java"
contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"
%>
<%@pageimport="java.sql.*"%>
<center>
<H1><fontcolor="blue"size="12">管理中心</font></H1>
<HR/>
<tablewidth="80%"border="1">
<tr>
<th>ID</th>
<th>书名</th>
<th>作者</th>
<th>价格</th>
<th>删除</th>
</tr>
<%
//数据库的名字
StringdbName="zap";
//登录数据库的用户名
Stringusername="sa";
//登录数据库的密码
Stringpassword="123";
//数据库的IP地址,本机可以用localhost或者127.0.0.1
Stringhost="127.0.0.1";
//数据库的端口,一般不会修改,默认为1433
intport=1433;
StringconnectionUrl="jdbc:sqlserver://"+host+":"+port+";databaseName="+dbName+";user="+username
+";password="+password;
//
//声明需要使用的资源
//数据库连接,记得用完了一定要关闭
Connectioncon=null;
//Statement记得用完了一定要关闭
Statementstmt=null;
//结果集,记得用完了一定要关闭
ResultSetrs=null;
try{
//注册驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//获得一个数据库连接
con=DriverManager.getConnection(connectionUrl);
StringSQL="SELECT*fromnote";
//创建查询
stmt=con.createStatement();
//执行查询,拿到结果集
rs=stmt.executeQuery(SQL);
while(rs.next()){
%>
<tr>
<td>
<%=rs.getInt(1)%>
</td>
<td>
<ahref="prepareupdate?ID=<%=rs.getInt("ID")%>"target="_blank"><%=rs.getString(2)%></a>
</td>
<td>
<%=rs.getString(3)%>
</td>
<td>
<%=rs.getString(4)%>
</td>
<td>
<ahref="delete?ID=<%=rs.getInt("ID")%>"target="_blank">删除</a>
</td>
</tr>
<%
}
}catch(Exceptione){
//捕获并显示异常
e.printStackTrace();
}finally{
//关闭我们使用过的资源
if(rs!=null)
try{
rs.close();
}catch(Exceptione){}
if(stmt!=null)
try{
stmt.close();
}catch(Exceptione){}
if(con!=null)
try{
con.close();
}catch(Exceptione){}
}
%>
</table>
<ahref="insert.jsp">添加新纪录</a>
</center>
⑹ jsp增删改查怎么实现的
java代码走起啊,
⑺ jsp和数据库(sqlserver)连接后,系统进行增删改查,这些操作是怎么实现的,详细具体点拜托
建议使用MVC模式做,JSP页面提交相应的操作后,提交给Servlet,Servlet中调用Model中定义的增删改查方法,方法调用后返回结果,然后通过Servlet返回给JSP页面。对于前台的增删改查跟数据库中中新建查询的操作是一样的,只是JSP页面增删改查是调用数据库查询语句封装的函数方法而已!
⑻ 如何使用jsp+servlet实现增删改查代码功能
第一步: 连接数据库
第二部: 查询出数据(可以加入条件) 并通过( jstl \ el )展现到页面
第三部 : 在你展现数据的最后添加一列(操作列:有删除、修改)
第四部:进行相关操作