java+jsp+sqlserver实现简单的增删改查操作 连接数据库代码
1,网站系统开发需要掌握的技术
(1)网页设计语言,html语言css语言等
(2)Java语言
(3)数据库
(4)等
2,源程序代码
(1) 连接数据库代码
package com.jaovo.msg.Util;
import java.sql.*;
public class DBUtil { public static Connection getConnection() {
try {
//1 加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String user1 = "sa";
String password = "123456";
String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=user";
Connection connection = null;
try {
//2 创建链接对象connection
connection = DriverManager.getConnection(url,user1,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
} //关闭资源的方法
public static void close(Connection connection ) {
try {
if (connection != null) {
connection.close();
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(PreparedStatement preparedStatement ) {
try {
if (preparedStatement != null) {
preparedStatement.close();
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(ResultSet resultSet ) {
try {
if (resultSet != null) {
resultSet.close();
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
(2)实现增删改查的源代码
package com.jaovo.msg.dao; import java.sql.*;
import java.util.ArrayList;
import java.util.List; import com.jaovo.msg.Util.DBUtil;
import com.jaovo.msg.Util.UserException;
import com.jaovo.msg.model.User; import sun.net.www.content.text.plain; public class UserDaoImpl implements IUserDao { public void add(User user) {
Connection connection = DBUtil.getConnection();
try {
String sql="insert into nlc1(id,username,password,nickname)values('"+user.getId()+"','"+user.getUsername()+"','"+user.getPassword()+"','"+user.getNickname()+"')";
Statement stmt=connection.createStatement();
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(connection);
} } @Override
public void delete(User user) {
Connection connection = DBUtil.getConnection();
//System.out.println("执行了");
try {
Statement stmt=connection.createStatement();
String sql = "delete from nlc1 where id = "+user.getId();
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(connection);
} } @Override
public void update(User user) {
Connection connection = DBUtil.getConnection();
try {
//String n=user.getId1();
Statement stmt=connection.createStatement();
String username=user.getUsername();
String id=user.getId();
String password=user.getPassword();
String nickname=user.getNickname();
String id1=user.getId1();
String sql="update nlc1 set id='"+id+"',username='"+username+"',password='"+password+"',nickname='"+nickname+"' where id="+user.getId1();
stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
}
public void check(User user) {
Connection connection = DBUtil.getConnection();
Statement stmt;
try {
stmt = connection.createStatement();
ResultSet rs=stmt.executeQuery("SELECT * FROM nlc1 where id="+user.getId());
//while(rs.next())
//System.out.println(rs.getString("id")+"\t"+rs.getString("username")+"\t"+rs.getString("password")+"\t"+rs.getString("nickname"));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
public List<User> load() {
Connection connection = DBUtil.getConnection();
//准备sql语句
String sql = "select * from nlc1 ";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//集合中只能放入user对象
List<User> users = new ArrayList<User>();
User user = null;
try {
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
user = new User();
user.setId(resultSet.getString("id"));
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
user.setNickname(resultSet.getString("nickname"));
users.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return users;
} @Override
public User load(int id) {
// TODO Auto-generated method stub
return null;
} @Override
public User load(String username) {
// TODO Auto-generated method stub
Connection connection = DBUtil.getConnection();
//准备sql语句
String sql = "select * from nlc1 where username = ?";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
User user = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, username);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
user = new User();
user.setId(username);
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
user.setNickname(resultSet.getString("nickname"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return user;
} }
(3)登录界面代码login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>登录界面</title>
</head>
<body background="D:\图片\动漫\20150422H2607_fKHLB.jpeg">
<%if(request.getAttribute("error1")!=null){ %>
<h4 >用户名或密码不能为空</h4>
<%} %>
<%if(request.getAttribute("error2")!=null){ %>
<h4>账号或密码错误</h4>
<%} %>
<form action="enter.jsp" method="get">
<table align="center" border="4" width="400">
<h4 align="center">登录界面</h4>
<tr>
<td>用户名称 : </td>
<td>
<input type="text" name="username" />
</td>
</tr>
<tr>
<td>用户密码:</td>
<td>
<input type="password" name="password" />
</td>
</tr>
<script>
function a(){window.open("addInput.jsp",'_blank')}
</script>
<script>
function b(){window.open("deleteInput.jsp",'_blank')}
</script>
<tr align="center">
<td colspan="2">
<input type="submit" value="登录" />
<input type="button" value="注册"onclick="a()"/>
<!-- <input type="button" value="修改密码"onclick="b()"/> -->
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</body>
</html>
<%@page import="com.jaovo.msg.Util.UserException"%>
<%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
<%@page import="com.jaovo.msg.model.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User();
UserDaoImpl userDao = new UserDaoImpl();
user.setUsername(username);
user.setPassword(password);
if(username.equals(user.getUsername())&&password.equals(user.getPassword())){
%>
<h4 align="center">登录成功</h4>
<div ><div style="text-align:center;">
<a href="login.jsp">返回登录界面</a>
<a href="list.jsp">查看所有用户</a>
</div>
<%
}
%>
<%
if(username == null || "".equals(username.trim())){
request.setAttribute("error1", "用户名不能为空"); %>
<jsp:forward page="login.jsp"></jsp:forward>
<%
}
%>
<%
user=userDao.load(username);
if(user==null){
request.setAttribute("error2", "用户名不存在或密码错误");
%>
<jsp:forward page="login.jsp"></jsp:forward>
<%
}
%>
</body>
</html>
(4)增删改查界面源代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>用户注销页面</title>
</head>
<body background="D:\图片\动漫\20150422H2607_fKHLB.jpeg">
<%=request.getAttribute("error") %>
<form action="delete.jsp" method="get">
<h4 align="center">删除界面</h4>
<table align="center" border="4" width="400"> <tr>
<td>请输入要删除的用户id:</td>
<td>
<input type="text" name="id" />
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="删除" />
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</body>
</html>
<%@page import="com.jaovo.msg.Util.UserException"%>
<%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
<%@page import="com.jaovo.msg.model.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>删除页面</title>
</head>
<body background="D:\图片\动漫\20150422H2607_fKHLB.jpeg" >
<%
String id=request.getParameter("id");
if(id == null || "".equals(id.trim())){
request.setAttribute("error", "用户名不能为空");
%>
<jsp:forward page="deleteInput.jsp"></jsp:forward>
<% }
User user = new User();
user.setId(id);
UserDaoImpl userDao = new UserDaoImpl();
try{
userDao.delete(user);
%> <h4 align="center">注销成功</h4>
<div style="text-align:center;">
<a href="addInput.jsp">注册用户</a>
<a href="deleteInput.jsp">删除用户</a>
<a href="updataInput.jsp">修改用户</a>
<!-- <a href="checkInput.jsp">查询用户</a> -->
<a href="list.jsp">查看列表信息</a>
<a href=>
</div>
<%
}catch(UserException e){
%>
<h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
<%
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>登录注册</title>
</head>
<body background="D:\图片\动漫\57Z58PICJBv_1024.jpg" >
<%=request.getAttribute("error") %>
<form action="add.jsp" method="get">
<table align="center" border="4" width="400">
<h4 align="center">注册界面</h4>
<tr>
<td>用户id : </td>
<td>
<input type="text" name="id" />
</td>
</tr>
<tr>
<td>用户名称 : </td>
<td>
<input type="text" name="username" />
</td>
</tr>
<tr>
<td>用户密码:</td>
<td>
<input type="password" name="password" />
</td>
</tr>
<tr>
<td>用户昵称:</td>
<td>
<input type="text" name="nickname" />
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="注册" />
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</body>
</html>
<%@page import="com.jaovo.msg.Util.UserException"%>
<%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
<%@page import="com.jaovo.msg.model.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>注册</title>
</head>
<body background="D:\图片\动漫\57Z58PICJBv_1024.jpg" >
<%
//接收客户端传递过来的参数
String id = request.getParameter("id");
String username = request.getParameter("username");
String password = request.getParameter("password");
String nickname = request.getParameter("nickname");
if(username == null || "".equals(username.trim())){
request.setAttribute("error", "用户名不能为空"); %>
<jsp:forward page="addInput.jsp"></jsp:forward>
<%
} User user = new User();
user.setId(id);
user.setUsername(username);
user.setPassword(password);
user.setNickname(nickname); UserDaoImpl userDao = new UserDaoImpl();
try{
userDao.add(user);
%> <h2 align="center">注册成功</h2> <div style="text-align:center;">
<a href="addInput.jsp">注册用户</a>
<a href="deleteInput.jsp">删除用户</a>
<a href="updataInput.jsp">修改用户</a>
<!-- <a href="checkInput.jsp">查询用户</a> -->
<a href="list.jsp">查看列表信息</a>
</div> <%
}catch(UserException e){
%>
<h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
<%
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>用户修改页面</title>
</head>
<body background="D:\图片\动漫\20150422H2607_fKHLB.jpeg">
<%=request.getAttribute("error") %>
<form action="updata.jsp" method="get">
<h4 align="center">修改界面</h4>
<table align="center" border="4" width="400">
<tr>
<td>要修改的用户id : </td>
<td>
<input type="text" name="id1" />
</td>
</tr>
<tr>
<td>修改后的用户id : </td>
<td>
<input type="text" name="id" />
</td>
</tr>
<tr>
<td>修改后的用户name : </td>
<td>
<input type="text" name="username" />
</td>
</tr>
<tr>
<td>修改后的用户password : </td>
<td>
<input type="password" name="password" />
</td>
</tr>
<tr>
<td>修改后的用户nickname : </td>
<td>
<input type="text" name="nickname" />
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="修改" />
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</body>
</html>
<%@page import="com.jaovo.msg.Util.UserException"%>
<%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
<%@page import="com.jaovo.msg.model.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>修改页面</title>
</head>
<body background="D:\图片\动漫\20150422H2607_fKHLB.jpeg">
<%
//接收客户端传递过来的参数
String id1 = request.getParameter("id1");
String id = request.getParameter("id");
String username = request.getParameter("username");
String password = request.getParameter("password");
String nickname = request.getParameter("nickname");
if(id == null || "".equals(id.trim())){
request.setAttribute("error", "id不能为空");
%>
<%-- <jsp:forward page="updataInput.jsp"></jsp:forward> --%>
<% }
User user = new User();
user.setId1(id1);
user.setId(id);
user.setUsername(username);
user.setPassword(password);
user.setNickname(nickname);
UserDaoImpl userDao = new UserDaoImpl();
try{
userDao.update(user);
%> <%
%> <h4 align="center">用户修改成功</h4>
<div style="text-align:center;">
<a href="addInput.jsp">注册用户</a>
<a href="deleteInput.jsp">删除用户</a>
<a href="updataInput.jsp">修改用户</a>
<!-- <a href="checkInput.jsp">查询用户</a> -->
<a href="list.jsp">查看列表信息</a>
</div>
<%
//userDao.add(user); }catch(UserException e){
%>
<h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
<%
}
%>
</body>
</html>
<%@page import="com.jaovo.msg.Util.UserException"%>
<%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
<%@page import="com.jaovo.msg.model.User"%>
<%@page import="java.util.List" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>列表</title>
</head>
<body>
<%
UserDaoImpl userDao = new UserDaoImpl();
List<User> users = userDao.load();
%> <body> <table align="center" border="1" width="500">
<tr>
<td>用户编号</td>
<td>用户名称</td>
<td>用户密码</td>
<td>用户昵称</td>
</tr>
<%
for( User user : users ){
%>
<tr>
<td> <%=user.getId() %></td>
<td> <%=user.getUsername() %></td>
<td> <%=user.getPassword() %></td>
<td> <%=user.getNickname() %></td>
<td> <a href="deleteInput.jsp ">删除</a></td> </tr>
<%
}
%>
</table>
</body>
</body>
</html>
3,运行结果截图
4,这门课的希望和自己的目标,列出计划每周花费在这门课上的时间
希望:希望学习完这门课之后能够熟练写出一个网站系统,尤其是在网站设计上
目标:熟练写出一个网站系统
计划:在完成其它课程作业的其余时间都扑身于这门课上
java+jsp+sqlserver实现简单的增删改查操作 连接数据库代码的更多相关文章
- Java通过JDBC进行简单的增删改查(以MySQL为例)
Java通过JDBC进行简单的增删改查(以MySQL为例) 目录: 前言:什么是JDBC 一.准备工作(一):MySQL安装配置和基础学习 二.准备工作(二):下载数据库对应的jar包并导入 三.JD ...
- python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删改查操作
1.通过 pip 安装 pymysql 进入 cmd 输入 pip install pymysql 回车等待安装完成: 安装完成后出现如图相关信息,表示安装成功. 2.测试连接 import ...
- AD 域服务简介(三)- Java 对 AD 域用户的增删改查操作
博客地址:http://www.moonxy.com 关于AD 域服务器搭建及其使用,请参阅:AD 域服务简介(一) - 基于 LDAP 的 AD 域服务器搭建及其使用 Java 获取 AD 域用户, ...
- C#+Access 员工信息管理--简单的增删改查操作和.ini配置文件的读写操作。
1.本程序的使用的语言是C#,数据库是Access2003.主要是对员工信息进行简单的增删改查操作和对.ini配置文件的读写操作. 2.代码运行效果如下: 功能比较简单.其中在得到查询结果后,在查询结 ...
- 【OF框架】新建库表及对应实体,并实现简单的增删改查操作,封装操作标准WebApi
准备 搭建好项目框架及数据库,了解框架规范. 1.数据库表和实体一一对应,表名实体名名字相同,用小写,下划线连接.字段名用驼峰命名法,首字母大写. 2.实体放在Entities目录下,继承Entity ...
- Java实现简单的增删改查操作
需求分析:通过数组 ,完成 对学生信息的 管理 (增删改查)创建1个 学生类创建1个 CRUD的类 – 学生管理类 并测试 在这个程序中我只运用了两个类进行操作 package com.hopu.de ...
- ADO.NET对SqlServer进行简单的增删改查
对数据库进行增删改查,首先想到的应该就是连接字符串了. 我们的连接字符串是由"Server=地址(本机=local);Database=数据库名称;User Id=登陆用户名;Passwor ...
- 初识Hibernate框架,进行简单的增删改查操作
Hibernate的优势 优秀的Java 持久化层解决方案 (DAO) 主流的对象—关系映射工具产品 简化了JDBC 繁琐的编码 将数据库的连接信息都存放在配置文件 自己的ORM框架 一定要手动实现 ...
- 初识hibernate框架之一:进行简单的增删改查操作
Hibernate的优势 l 优秀的Java 持久化层解决方案 (DAO) l 主流的对象—关系映射工具产品 l 简化了JDBC 繁琐的编码 l 将数据库的连接信息都存放在配置文件 l 自己的ORM ...
随机推荐
- node——简单的服务器启动+乱码问题解决,响应报文头
这个是一个比较简单的代码 // 1.加载hrrp模块 var http=require('http'); // 2.创建一个http服务对象 var server=http.createServer( ...
- IOS - 6\7下UINavigationBar的颜色的方法改变 ——转载http://www.th7.cn/Program/IOS/201310/155057.shtml
IOS7下设置UINavigationBar的颜色的方法已经改变(当然如果是用自定义图片的话请忽略---) 首先是区别iOS7和之前版本的方法如下: //如果是iOS7以前的话if (floor(NS ...
- linux之资料重定向
標準輸入 (stdin) :代碼為 0 ,使用 < 或 << : 標準輸出 (stdout):代碼為 1 ,使用 > 或 >> : 標準錯誤輸出(stderr):代 ...
- 洛谷 P1313 计算系数 (二项式定理)
这道题正好复习了二项式定理 所以答案就是a^n * b^m * c(n, k) 然后注意一些细节 我一开始写组合数只写一行的组合数 即c[0] = 1; c[i] = c[i-1] * (n - i ...
- 5kcrm增加权限管理中的模块(签到统计)
1 首先在model表增加模块名称 2 在controll里增加方法 3 在授权的html增加表单
- ASP.NET-前台view返回model集合
有时操作列表的时候想一次提交一个model集合,这样后台controller直接接受后就可以直接进行操作了,不用使用js,比较方便,也体现了MVC的Binding模式的优势,方法如下: 准备: 1.两 ...
- java语言MySQL批处理
本质来讲就是使用Statement和PreStatement的addBatch()方法 代码 import java.sql.*; public class GetConnection{ public ...
- Eureka Server添加用户认证
Eureka Server添加用户认证 学习了:http://blog.csdn.net/liuchuanhong1/article/details/54729556 注意:1,需要使用 defaul ...
- 代理模式之cglib动态代理
上一篇博客说了实现InvocationHandler接口的jdk动态代理,还有一种实现动态代理的方式则是:通过继承的方式实现的cglib动态代理. 先在程序中导入cglib的包,cglib-nodep ...
- 外网主机如何将数据包发送到共用一个公网IP的局域网某特定主机上的
内网的一台电脑要上因特网对外开放服务或接收数据.都须要port映射.port映射分为动态和静态. 动态port映射:内网中的一台电脑要訪问站点.会向NAT网关发送数据包.包头中包含对方站点IP.por ...