首页 > 编程语言 >JavaWeb课堂测试(一)

JavaWeb课堂测试(一)

时间:2023-02-13 19:45:39浏览次数:39  
标签:pre JavaWeb Util bean 测试 sql 课堂 public con

一、数据库构建

 

 创建一个用户表,存储用户名和密码

 

 创建数据表,存储数据

二、jdbc连接数据库

package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Util {

public static Connection getConnection() throws ClassNotFoundException, SQLException{

Connection con = null;
Statement sta = null;
ResultSet res = null;
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test ?useUnicode=true&characterEncoding=utf8", "root", "123456");
return con;
}

public static void close(Connection con) {
try {
if (con != null) {
con.close();
}

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void close(PreparedStatement pre) {
try {
if (pre != null) {
pre.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void close(ResultSet res) {
try {
if (res != null) {
res.close();
}

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

 

三、创建JavaBean组件

package bean;

public class NewsBean {

private String title; //标题
private String keyword; //关键字
private String author; //作者
private String date; //日期
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}

}

package bean;

public class UserBean {
private String name; //用户名
private String password; //密码
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}

三、创建DAO类接口

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import bean.NewsBean;
import bean.LookBean;
import util.Util;

public class NewsDao {
//添加新闻信息
public int add(NewsBean bean)throws ClassNotFoundException, SQLException
{
Connection con = Util.getConnection();
PreparedStatement pre = null;
try {
String sql = "insert into news1(title,keyword,author,date) values(?,?,?,?)";
pre = con.prepareStatement(sql);
pre.setString(1, bean.getTitle());
pre.setString(2, bean.getKeyword());
pre.setString(3, bean.getAuthor());
pre.setString(4, bean.getDate());
pre.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}finally {
Util.close(con);
Util.close(pre);
}
return 1;
}

//删除新闻信息
public int delete(NewsBean bean) throws ClassNotFoundException, SQLException
{
Connection con = Util.getConnection();
PreparedStatement pre = null;
try {
String sql="delete from news1 where title=?";
pre = con.prepareStatement(sql);
pre.setString(1, bean.getTitle());
pre.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}finally {
Util.close(con);
Util.close(pre);
}
return 1;
}

//修改新闻信息
public int update(NewsBean bean) throws ClassNotFoundException, SQLException
{
Connection con = Util.getConnection();
PreparedStatement pre = null;
try {
String sql = "update news1 set keyword=?,author=?,date=?, where title=?";
pre = con.prepareStatement(sql);
pre.setString(1, bean.getTitle());
pre.setString(2, bean.getKeyword());
pre.setString(3, bean.getAuthor());
pre.setString(4, bean.getDate());
pre.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}finally {
Util.close(con);
Util.close(pre);
}
return 1;
}

//查询新闻信息
public int read(NewsBean bean) throws ClassNotFoundException, SQLException
{
Connection con = Util.getConnection();
PreparedStatement pre = null;
ResultSet resultSet=null;
try {
String sql = "select * from news1 where title=?";
pre = con.prepareStatement(sql);
pre.setString(1, bean.getTitle());
resultSet = pre.executeQuery();
while (resultSet.next()){
System.out.println("title"+resultSet.getString("title")+"keyword"+resultSet.getString("keyword")+"author"+resultSet.getString("author")+"date"+resultSet.getString("date"));
}
pre.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}finally {
Util.close(con);
Util.close(pre);
Util.close(resultSet);
}
return 1;
}
}

 

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import bean.UserBean;
import util.Util;

public class UserDao {
//添加用户信息
public int add(UserBean bean)throws ClassNotFoundException, SQLException
{
Connection con = Util.getConnection();
PreparedStatement pre = null;
try {
String sql = "insert into user(name,password) values(?,?)";
pre = con.prepareStatement(sql);
pre.setString(1, bean.getName());
pre.setString(2, bean.getPassword());
pre.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}finally {
Util.close(con);
Util.close(pre);
}
return 1;
}

//删除新闻信息
public int delete(UserBean bean) throws ClassNotFoundException, SQLException
{
Connection con = Util.getConnection();
PreparedStatement pre = null;
try {
String sql="delete from user where name=?";
pre = con.prepareStatement(sql);
pre.setString(1, bean.getName());
pre.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}finally {
Util.close(con);
Util.close(pre);
}
return 1;
}

//修改新闻信息
public int update(UserBean bean) throws ClassNotFoundException, SQLException
{
Connection con = Util.getConnection();
PreparedStatement pre = null;
try {
String sql = "update user set password=?, where name=?";
pre = con.prepareStatement(sql);
pre.setString(1, bean.getName());
pre.setString(2, bean.getPassword());
pre.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}finally {
Util.close(con);
Util.close(pre);
}
return 1;
}

//查询新闻信息
public int read(UserBean bean) throws ClassNotFoundException, SQLException
{
Connection con = Util.getConnection();
PreparedStatement pre = null;
ResultSet resultSet=null;
try {
String sql = "select * from user where title=?";
pre = con.prepareStatement(sql);
pre.setString(1, bean.getName());
resultSet = pre.executeQuery();
while (resultSet.next()){
System.out.println("name"+resultSet.getString("name")+"password"+resultSet.getString("password"));
}
pre.executeUpdate();
}catch(SQLException e) {
e.printStackTrace();
}finally {
Util.close(con);
Util.close(pre);
Util.close(resultSet);
}
return 1;
}
}

标签:pre,JavaWeb,Util,bean,测试,sql,课堂,public,con
From: https://www.cnblogs.com/lxh-666/p/17117557.html

相关文章

  • 2月13日课堂测试
    增加界面:<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtml><html><head><metacharset="UTF-8"><title>发布新闻<......
  • 大二下学期开学测试总结 2023/2/13
    今天下午,进行了大二下学期java开学测试,在这次的测试中,有了很多的感悟与思考,下面将其总结如下:经过了这次的测试,自己又一次认识到了自己到底学到了多少,自己有多大的本领,这次......
  • 2023年第一次测试
    本次测试从题目来看大部分还是上一学期学的web应用技术与开发,唯一不同的就是对服务器的要求,要求可以连网,并且能在其他主机上运行,其他的就都还是普通的前端页面以及后台数据......
  • 开学测试|河北省环保监测中心网络新闻发布系统
     1、项目需求:河北省环保监测中心网络新闻为搭建公众信息交流平台,决定建立新闻发布平台。新闻发布平台按内容包括中心新闻、企业环保信息发布等若干新闻栏目,新闻撰稿人可......
  • 开学测试
    开学测试经过今天三个小时的练习考试,我熟悉掌握了数据库的链接         链接数据库    }以上输出结果为  根据这次课程的自我学习,通......
  • 开学测试三小时的总结
    三小时没干多少事情,主要是上学期就没怎么学,寒假出了假期考试没碰java,所以生疏了很多很多。简单写了写增删改查,也写的不完整,不过把王老师要求的连接数据库是连接上了。下面......
  • 开学初软件工程第一次课进行课前测试,要求实现一个完整MIs系统的开发流程
    本次测试经历了三个小时,这次测试使沉醉在假期的我惊醒,我意识到我仍是一名学生,我还有许多的东西要去学习。我先去复习上学期的增删改查,连接数据库之类的知识,之后开始写代码,......
  • 028_测试_测试类中启动 web 环境
    修改pom.xml的依赖  WebTest.java ......
  • 027_测试_加载测试专用配置
    MsgConfig.java   ConfigurationTest.java ......
  • 大二下学期开学测试课后总结
    今天下午进行了开学测试,发现了许多的不足。这次的测验是河北省环保监测中心网络新闻发布系统。这个系统主要进行的是多个角色的增删改查,任务量大,需要写的东西非常多,难度不......