In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
1. Add database tables
Open the database using sqlplus
Sqlplus scott/tiger
Create an emps data table
Create table emps (id varchar (32) not null, username varchar (20) not null, salary number (6d2), hiredate date)
2. Add jar package
The jar packages required by the project are spring-core, spring-web, spring-webmvc, oracle database driver, c3p0 database connection pool, and dbutils.
Jar packet classification specific jar package spring-core
Commons-logging-1.2.jar
Spring-beans-3.2.5.RELEASE.jar
Spring-context-3.2.5.RELEASE.jar
Spring-core-3.2.5.RELEASE.jar
Spring-expression-3.2.5.RELEASE.jar
Spring-webspring-web-3.2.5.RELEASE.jarspring-webmvcspring-webmvc-3.2.5.RELEASE.jaroracle database driver
Ojdbc5.jar
Located at: OracleDB\ product\ 11.2.0\ dbhome_1\ jdbc\ lib\ ojdbc5.jar
C3p0 database connection pool
C3p0-0.9.1.2.jardbutilscommons-dbutils-1.6.jar
3. Configuration
After adding the jar package, configure:
(1) to add springmvc to the web project, you need to configure web.xml and springmvc.xml files
(2) to use c3p0, you need to configure the c3p0-config.xml file
Web.xml
Emp index.jsp springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc.xml springmvc * .action CharacterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 CharacterEncodingFilter / *
Springmvc.xml
C3p0-config.xml
Jdbc:oracle:thin:@127.0.0.1:1521:orcl oracle.jdbc.driver.OracleDriver scott tiger 2 5 15 1000
4. Tool class writing
SecurityUtils is used to provide UUID, while JDBCUtils is used to get DataSource.
SecurityUtils.java
Package com.rk.utils;import java.util.UUID;public class SecurityUtils {public static String getUUID () {return UUID.randomUUID () .toString () .replaceAll ("-", ");}}
JDBCUtils.java
Package com.rk.utils;import com.mchange.v2.c3p0.ComboPooledDataSource;public class JDBCUtils {/ * load the c3p0-config.xml configuration file * / private static ComboPooledDataSource dataSource = new ComboPooledDataSource () in the src directory; / * get the data source * / public static ComboPooledDataSource getDataSource () {return dataSource;}}
5. From entity to action
Employee.java
Package com.rk.entity;import java.util.Date;public class Employee {private String id; private String username; private Double salary; private Date hiredate; public Employee () {} public Employee (String id, String username, Double salary, Date hiredate) {this.id = id; this.username = username; this.salary = salary This.hiredate = hiredate;} public String getId () {return id;} public void setId (String id) {this.id = id;} public String getUsername () {return username;} public void setUsername (String username) {this.username = username } public Double getSalary () {return salary;} public void setSalary (Double salary) {this.salary = salary;} public Date getHiredate () {return hiredate;} public void setHiredate (Date hiredate) {this.hiredate = hiredate;}}
EmpDao.java
Package com.rk.dao;import java.sql.Timestimport java.util.Date;import org.apache.commons.dbutils.QueryRunner;import org.junit.Test;import com.rk.entity.Employee;import com.rk.utils.JDBCUtils;import com.rk.utils.SecurityUtils;public class EmpDao {public void add (Employee emp) throws Exception {QueryRunner queryRunner = new QueryRunner (JDBCUtils.getDataSource ()) String sql = "insert into emps (id,username,salary,hiredate) values (?,)"; Object [] params = {SecurityUtils.getUUID (), emp.getUsername (), emp.getSalary (), new Timestamp (emp.getHiredate (). GetTime ())}; queryRunner.update (sql,params) @ Test public void run () throws Exception {Employee emp = new Employee (); emp.setUsername ("Xiaoming"); emp.setSalary (88.88); emp.setHiredate (new Date ()); add (emp);}}
EmpService.java
Package com.rk.service;import com.rk.dao.EmpDao;import com.rk.entity.Employee;public class EmpService {private EmpDao empDao; public void setEmpDao (EmpDao empDao) {this.empDao = empDao;} public void register (Employee emp) throws Exception {empDao.add (emp);}}
EmpAction.java
Package com.rk.action;import java.text.SimpleDateFormat;import java.util.Date;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.propertyeditors.CustomDateEditor;import org.springframework.validation.BindException;import org.springframework.web.bind.ServletRequestDataBinder;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.AbstractCommandController;import com.rk.entity.Employee;import com.rk.service.EmpService @ SuppressWarnings ("deprecation") public class EmpAction extends AbstractCommandController {/ / Business layer private EmpService empService; public void setEmpService (EmpService empService) {this.empService = empService;} / / encapsulate the form parameters into the Employee entity public EmpAction () {this.setCommandClass (Employee.class) } / / Custom String- > Date converter @ Override protected void initBinder (HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {binder.registerCustomEditor (Date.class, new CustomDateEditor (new SimpleDateFormat ("yyyy-MM-dd"), true)) } @ Override protected ModelAndView handle (HttpServletRequest request, HttpServletResponse response, Object obj, BindException bindException) throws Exception {ModelAndView modelAndView = new ModelAndView (); Employee emp = (Employee) obj; empService.register (emp); modelAndView.addObject ("message", "operation successful") ModelAndView.setViewName ("success"); return modelAndView;}}
6. Configuration of dao/service/action
Spring-emp.xml
7. JSP page
WebRoot/jsp/index.jsp
Add employee name: employee salary: Entry time:
WebRoot/jsp/success.jsp
Added successfully ${message}
Demo
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.