admin 管理员组文章数量: 887149
2024年1月16日发(作者:javascript入门教程实例)
创建两个触发器:创建序列表users_xh用来id递增Create table users_xh (xh_id number)create or replace trigger users_increase beforeinsert on users for each rowbegin select xh_id+1 into : from users_xh;end;和创建类DatabaseTest,内容如下:public class DatabaseTest {private static Connection getConn() {String driver = "Driver";String url = "jdbc:oracle:thin:@localhost:1521:orcl";String username = "scott";//数据库用户名String password = "tiger";//数据库用户密码create or replace trigger users_increase_after after
insert on users for each rowdeclare -- local variables herebegin update users_xh set xh_id =xh_id+1;end users_increase_after;说明:我在数据库创建了触发器来自动添加id,所以没有出现设定id值。连接oracle数据库需要和两个包,放入lib在数据库创建users表:create table users (id number not null,username varchar2(20),password varchar2(30))1Struts2连接oracle数据库增删改查实例的实现Connection conn = null;try {e(driver);// new Driver();conn = nection(url, username, password);n("connect success");} catch (ClassNotFoundException e) {
所需的jar包:tackTrace();} catch (SQLException e) {tackTrace();}return conn;这样数据库环境搭建无错,可以进行下一步。创建web project结构如下}public static void main(String[] args) {getConn();}}Run as java application运行结果如下,表示数据库连接成功!
xmlns="/xml/ns/javaee" xmlns:web="/xml/ns/javaee/web- }}return id; = id;return password;return username;}}}}}private int id;public int getId() {private String password;private String username;public void setId(int id) {public class UserBean{package ;package tion;public String getPassword() {public String getUsername() {rd = password;me = username;public void setPassword(String password) {public void setUsername(String username) {import tion;import Manager;import edStatement;import Set;import eption;import ist;1.3 Beans源码:1.4 Action源码: import ;import ;public int getId() {return id;}public void setId(int id) { = id;}import an;import rvletRequest;public class UserAction extends ActionSupport {import ;import tory;import Context;import Support;public String getPassword() {public String getUsername() {return username;}private int id;private String username;private String password;private List }}/**/**return conn;return password; * 用户名与密码匹配检查 * public List * @return */private static Connection getConn() {String driver = "Driver";String url = "jdbc:oracle:thin:@localhost:1521:orcl";String username = "scott";String password = "tiger";Connection conn = null;try {e(driver);// new Driver();conn = nection(url, username, password);// ("connect success");} catch (ClassNotFoundException e) {tackTrace();} catch (SQLException e) {tackTrace();} * @return 结果标识 字符串 success 表示匹配成功 UserNotExist表示用户不存在 }else}return "error";();();();} catch (SQLException e) {tackTrace();}if (name == null)// 验证用户名是否存在return "UserNotExist";return "success";error 表示匹配失败 */private String userCheck() {("");String name = null;String pass = null;Connection conn = getConn();String sql = "select * from users where username='" + username + "'";PreparedStatement pstmt;try {pstmt = eStatement(sql);ResultSet rs = eQuery();while (()) {name = ing("username");pass = ing("password");else if ((username) && (password))// 验证登录用户名与密码是否匹配private UserBean query(int id) {("");Connection conn = getConn();String sql = "select * from users where id='" + id + "'";PreparedStatement pstmt;UserBean user = new UserBean();try {pstmt = eStatement(sql);ResultSet rs = eQuery(); }();();();} catch (SQLException e) {tackTrace();}return user;/** * 查看所有用户 * * @return List }return i;}/** * 将注册信息添加到数据库表 users * * @param username * @param password * @return */private int insert(String username, String password) {Connection conn = getConn();int i = 0;String sql = "insert into users (username,password) values(?,?)";PreparedStatement pstmt;try {pstmt = eStatement(sql);// Statement stat = Statement();ing(1, username);ing(2, password);i = eUpdate();n("insert resutl: " + i);();();} catch (SQLException e) {tackTrace();}private static int myUpdate(int id, String newPass) {Connection conn = getConn();int i = 0;String sql = "update users set password='" + newPass + "' where id='"+ id + "'";PreparedStatement pstmt;try {pstmt = eStatement(sql);();i = eUpdate();n("update resutl: " + i); }return i;}/** * * * @return * @throws Exception *//** * * * @return * @throws Exception */();} catch (SQLException e) {tackTrace();}public String register() throws Exception {String name = getUsername();String pass = getPassword();int i = insert(name, pass);if (i != 0)return "success";elsereturn "error";}public String login() throws Exception {("...");if (userCheck().equals("success")) {text().getSession().put("username", getUsername());return SUCCESS;} else if (userCheck().equals("UserNotExist"))return "UserNotExist";elsereturn ERROR; public String show() throws Exception {("...");userlist = findAllUser();if (() > 0)return "success";elsereturn "error";}public String update() throws Exception {("id="+id);UserBean user=query(id);if (null!=user){setUsername(rname());("username="+username);myUpdate(id,password);return "success";}else{return "error";}}public String delete() throws Exception {Connection conn = getConn();int i = 0;n("id=" + id);String sql = "delete users where id ='" + id + "'";PreparedStatement pstmt;try {pstmt = eStatement(sql);();();} catch (SQLException e) {tackTrace();}if (i > 0)return "success";elsereturn "error";i = eUpdate();n("delete resutl: " + i); }%><%1.5 %><% <%@taglib uri="/struts-tags" prefix="s"%>
密 码: 用户注册