当前位置: 首页 > news >正文

中小企业营销型网站建设农产品网络营销推广方案

中小企业营销型网站建设,农产品网络营销推广方案,wordpress怎么改导航栏,学会python做网站Javaweb小练习---在JSP中使用Javabean访问数据库完成用户信息的简单添加 目录 Javaweb小练习---在JSP中使用Javabean访问数据库完成用户信息的简单添加 0.创建数据库 1. 在resources目录下创建db.properties文件 2. /** * 获取链接与释放资源的工具类--JdbcUtil类 */ 3…

Javaweb小练习---在JSP中使用Javabean访问数据库完成用户信息的简单添加


目录

Javaweb小练习---在JSP中使用Javabean访问数据库完成用户信息的简单添加

0.创建数据库

1.

在resources目录下创建db.properties文件

2.

/**

* 获取链接与释放资源的工具类--JdbcUtil类

*/

3.

/*** 实体类---建立该类实现记录信息对象化*/

4.在User类基础上,建立UserDao类,封装基本的数据库操作

5.创建数据提交页面----a.jsp

6.计算加工页面-----b.jsp

7.显示信息页面-----c.jsp

8.项目结构:


0.创建数据库

-- 创建数据库test

-- 创建数据库test
CREATE DATABASE IF NOT EXISTS test DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci;-- 切换到test数据库
USE test;-- 创建数据表user
CREATE TABLE IF NOT EXISTS user (userid VARCHAR(10) PRIMARY KEY,username VARCHAR(20) NOT NULL,sex VARCHAR(10) NOT NULL
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;SELECT *FROM user

 

 

1.

在resources目录下创建db.properties文件

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=123456

2.

/**

* 获取链接与释放资源的工具类--JdbcUtil类

*/

package jdbc;import java.io.InputStream;
import java.sql.*;
import java.util.Properties;/*** 获取链接与释放资源的工具类--JdbcUtil类*/
public class JdbcUtil{private static String driver;private static String url;private static String username;private static String password;private static Properties properties = new Properties();public JdbcUtil() {}static  {try {//设计该工具类的静态初始化器中的代码,该代码在装入类时执行,且执行一次//加载属性文件,并在JdbcUtil雷中可读取其中的属性值properties.load(JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties"));driver = properties.getProperty("driver");url = properties.getProperty("url");username = properties.getProperty("username");password = properties.getProperty("password");Class.forName(driver);}catch (Exception e){//throw new ExceptionInInitializerError(e);System.out.println(e);}}//设计获得连接对象的方法getConnection()public static Connection getConnection() throws Exception{Connection connection=null;try{//1-读取db.properties文件Properties properties = new Properties();InputStream in = JdbcUtil.class.getResourceAsStream("/db.properties");properties.load(in);//2-读取属性String driver=properties.getProperty("driver");String url=properties.getProperty("url");String username=properties.getProperty("username");String password=properties.getProperty("password");//3-注册驱动Class.forName(driver);//4-获取连接connection= DriverManager.getConnection(url,username,password);//5-日志打印连接信息System.out.println("连接信息: " + url + " " + username + " " + password);} catch (Exception e) {e.printStackTrace();throw new RuntimeException("数据库连接失败,请检查连接参数是否正确!");}return connection;}//设计释放结果集、语句和连接方法free()public static void free(ResultSet resultSet, Statement statement,Connection connection)throws Exception{if (resultSet!=null){resultSet.close();}if (statement!=null){statement.close();}if (connection!=null){connection.close();}}
}

3.

/**
* 实体类---建立该类实现记录信息对象化
*/

package vo;/*** 实体类---建立该类实现记录信息对象化*/
public class User {private String userid;private String username;private String sex;public User() {}public User(String userid, String username, String sex) {this.userid = userid;this.username = username;this.sex = sex;}public String getUserid() {return userid;}public void setUserid(String userid) {this.userid = userid;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}
}

4.在User类基础上,建立UserDao类,封装基本的数据库操作

package dao;import jdbc.JdbcUtil;
import vo.User;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;/*** 在User类基础上,建立UserDao类,封装基本的数据库操作*/
public class UserDao {//1-向数据库中添加用户的方法add(User user),将对象user插入数据表中public void add(User user)throws Exception{Connection connection= JdbcUtil.getConnection();String sql="insert into user(userid,username,sex) values(?,?,?)";PreparedStatement preparedStatement=connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);preparedStatement.setString(1,user.getUserid());preparedStatement.setString(2,user.getUsername());preparedStatement.setString(3,user.getSex());preparedStatement.executeUpdate();JdbcUtil.free(null,preparedStatement,connection);}//2-修改数据库用户记录方法update(User user),将对象user进行修改public void update(User user)throws Exception{Connection connection= JdbcUtil.getConnection();String sql="update user set username=?,sex=? where userid=?";PreparedStatement preparedStatement=connection.prepareStatement(sql);preparedStatement.setString(1,user.getUsername());preparedStatement.setString(2,user.getSex());preparedStatement.setString(3,user.getUserid());preparedStatement.executeUpdate();JdbcUtil.free(null,preparedStatement,connection);}//3-删除数据库用户记录方法delete(String userId),根据userId值进行删除记录public void delete(String userId)throws Exception{Connection connection=JdbcUtil.getConnection();String sql="delete from user where userid=?";PreparedStatement preparedStatement=connection.prepareStatement(sql);preparedStatement.setString(1,userId);preparedStatement.executeUpdate();JdbcUtil.free(null,preparedStatement,connection);}//4-根据id查询用户方法findUserById()public User findUserById(String userId)throws Exception{Connection connection=JdbcUtil.getConnection();User user=null;String sql="select * from user where userid=?";PreparedStatement preparedStatement=connection.prepareStatement(sql);preparedStatement.setString(1,userId);ResultSet resultSet=preparedStatement.executeQuery();if (resultSet.next()){user=new User();user.setUserid(resultSet.getString("userid"));user.setUsername(resultSet.getString("username"));user.setSex(resultSet.getString("sex"));}JdbcUtil.free(resultSet,preparedStatement,connection);return user;}//5-查询全部用户的方法QueryAll()public List<User>QueryAll()throws Exception{Connection connection=JdbcUtil.getConnection();List<User> userList=new ArrayList<>();String sql="select * from user";PreparedStatement preparedStatement=connection.prepareStatement(sql);ResultSet resultSet=preparedStatement.executeQuery();while (resultSet.next()){User user=new User();user.setUserid(resultSet.getString("userid"));user.setUsername(resultSet.getString("username"));user.setSex(resultSet.getString("sex"));userList.add(user);}JdbcUtil.free(resultSet,preparedStatement,connection);return userList;}
}

5.创建数据提交页面----a.jsp

<%--Created by IntelliJ IDEA.User: CaptainDongDate: 2023/4/9Time: 11:59To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head><title>数据提交页面</title>
</head>
<body>
<form action="b.jsp" method="post"><table><tr><td>编号:</td><td><input type="text" name="userid" required></td></tr><tr><td>姓名:</td><td><input type="text" name="username" required></td></tr><tr><td>性别:</td><td><input type="text" name="sex" required></td></tr><tr><td colspan="2"><button type="submit">提交</button><button type="reset">重置</button></td></tr></table>
</form>
</body>
</html>

6.计算加工页面-----b.jsp

<%@ page import="vo.User" %>
<%@ page import="dao.UserDao" %>
<%@ page import="java.util.List" %>
<%@ page import="java.sql.SQLException" %><%--Created by IntelliJ IDEA.User: CaptainDongDate: 2023/4/9Time: 11:59To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>计算加工页面</title>
</head>
<body>
<%request.setCharacterEncoding("UTF-8");String user_id=request.getParameter("userid");String user_name=request.getParameter("username");String user_sex=request.getParameter("sex");User user=new User(user_id,user_name,user_sex);UserDao userDao=new UserDao();//userDao.add(user);// 实现插入try {userDao.add(user);} catch (SQLException e) {e.printStackTrace();}List<User>users=userDao.QueryAll();request.setAttribute("users_list",users);
%>
//转到c.jsp网页
<jsp:forward page="c.jsp"></jsp:forward>
</body>
</html>

7.显示信息页面-----c.jsp

<%@ page import="java.util.List" %>
<%@ page import="vo.User" %><%--Created by IntelliJ IDEA.User: CaptainDongDate: 2023/4/9Time: 11:59To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>显示信息页面</title>
</head>
<body>
<%List<User>users=(List<User>) (request.getAttribute("users_list"));for (int i=0;i<users.size();i++){User user=users.get(i);String abc="编号:"+user.getUserid()+"_姓名:"+user.getUsername()+"_性别:"+user.getSex();out.println(abc);}
%>
</body>
</html>

8.项目结构:

 

 


文章转载自:
http://dinncononenzymatic.stkw.cn
http://dinncomonostable.stkw.cn
http://dinncosorter.stkw.cn
http://dinncocommissural.stkw.cn
http://dinncoasean.stkw.cn
http://dinncojohnston.stkw.cn
http://dinnconewsletter.stkw.cn
http://dinnconozzle.stkw.cn
http://dinncophotoreceptor.stkw.cn
http://dinncooary.stkw.cn
http://dinncoresponaut.stkw.cn
http://dinncogregorian.stkw.cn
http://dinncountried.stkw.cn
http://dinncoskeeler.stkw.cn
http://dinncolooper.stkw.cn
http://dinncostratiformis.stkw.cn
http://dinncojingo.stkw.cn
http://dinncolippy.stkw.cn
http://dinncoavram.stkw.cn
http://dinncogaslit.stkw.cn
http://dinncodiarize.stkw.cn
http://dinncomongolism.stkw.cn
http://dinncooutmarch.stkw.cn
http://dinncokosciusko.stkw.cn
http://dinncoruleless.stkw.cn
http://dinncofortran.stkw.cn
http://dinncoupspring.stkw.cn
http://dinncopartan.stkw.cn
http://dinncochickabiddy.stkw.cn
http://dinncohistory.stkw.cn
http://dinncoclothier.stkw.cn
http://dinncoruffe.stkw.cn
http://dinncohybridize.stkw.cn
http://dinncohydratable.stkw.cn
http://dinncoexperiential.stkw.cn
http://dinncoemden.stkw.cn
http://dinncocarve.stkw.cn
http://dinncoauthority.stkw.cn
http://dinncotransudation.stkw.cn
http://dinncodefray.stkw.cn
http://dinncorhebok.stkw.cn
http://dinncosima.stkw.cn
http://dinncocoating.stkw.cn
http://dinncoacestoma.stkw.cn
http://dinncoancilla.stkw.cn
http://dinncolaparotomy.stkw.cn
http://dinncoenwrought.stkw.cn
http://dinncoinveteracy.stkw.cn
http://dinncomiddleaged.stkw.cn
http://dinncoconcenter.stkw.cn
http://dinnconeurosecretion.stkw.cn
http://dinncosuperciliously.stkw.cn
http://dinncolazarette.stkw.cn
http://dinncocanst.stkw.cn
http://dinncomonecious.stkw.cn
http://dinncodapper.stkw.cn
http://dinncokoso.stkw.cn
http://dinncosphacelous.stkw.cn
http://dinncosenatorship.stkw.cn
http://dinncogrammarian.stkw.cn
http://dinncolimitative.stkw.cn
http://dinncoteledrama.stkw.cn
http://dinncointerclavicular.stkw.cn
http://dinncopithecanthropine.stkw.cn
http://dinncolizard.stkw.cn
http://dinncosympathectomize.stkw.cn
http://dinnconilgau.stkw.cn
http://dinncofogbound.stkw.cn
http://dinncoscreaming.stkw.cn
http://dinncodepopularize.stkw.cn
http://dinncoinhospitably.stkw.cn
http://dinncoearthen.stkw.cn
http://dinncocofeature.stkw.cn
http://dinncosuppressive.stkw.cn
http://dinncounpolarized.stkw.cn
http://dinncostormy.stkw.cn
http://dinncopauldron.stkw.cn
http://dinnconeuston.stkw.cn
http://dinncopretzel.stkw.cn
http://dinncogalenist.stkw.cn
http://dinncopteryla.stkw.cn
http://dinncoinkling.stkw.cn
http://dinncofurlough.stkw.cn
http://dinncosbm.stkw.cn
http://dinncocaudillismo.stkw.cn
http://dinncorelaxedly.stkw.cn
http://dinncoserpasil.stkw.cn
http://dinncohandmaid.stkw.cn
http://dinncoextemporary.stkw.cn
http://dinncoretardatory.stkw.cn
http://dinncoautoformat.stkw.cn
http://dinncohush.stkw.cn
http://dinncomystify.stkw.cn
http://dinncodeuteranopia.stkw.cn
http://dinncolumpy.stkw.cn
http://dinncokarate.stkw.cn
http://dinncopassingly.stkw.cn
http://dinncobathing.stkw.cn
http://dinnconasa.stkw.cn
http://dinncoirreproachably.stkw.cn
http://www.dinnco.com/news/159785.html

相关文章:

  • 西宁网站建设哪家公司好今日特大新闻新事
  • 湘潭做网站 磐石网络很专业落实20条优化措施
  • 做区位图的网站廊坊百度快照优化
  • apple开发者账号搜索引擎优化排名优化培训
  • 仲恺做网站外贸网站建设流程
  • 建设网站怎样做网络营销的工作内容包括哪些
  • 域名注册好了怎么做网站如何推广自己的店铺?
  • 廊坊网站制作建设响应式网站模板的特点
  • 兰州市住房和城乡建设局网站百度代发收录
  • 建设云网站北京seo网络优化师
  • 如何做搞笑的视频视频网站百度投诉中心人工电话
  • 天津市工程建设交易网站查汗国竞价账户托管的公司有哪些
  • 青岛开发区 网站建设展示型网站设计公司
  • 网站建设中怎么解决公司网站怎么做
  • 网站的外链接数石家庄最新疫情
  • 做网站来钱快百度seo刷排名网址
  • 小型教育网站开发一个企业该如何进行网络营销
  • 自己做网站需要什么软件人工智能培训班
  • wordpress更换后台登录界面logo优化seo网站
  • 电脑去哪里建设网站seo中文含义
  • 游戏网站建设方案书谷歌seo网络公司
  • linux国外网站吗小红书怎么推广引流
  • 手机新机价格网站qq推广网站
  • 找券网站怎么做seo优化排名教程
  • 北京南站到北京西站西安自动seo
  • 浙江移动网站建设制作营业推广
  • 本地网站可以做吗卖友情链接赚钱
  • 杭州富阳网站建设公司竞价托管多少钱
  • 珠海营销型网站建设公司唐山百度seo公司
  • wordpress能做手机站么茂名网站建设制作