//创建工具类
package com.yan;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectionManager {
static {
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:EDUASK", "yanjun", "123456");
}
public static void closeConnection(ResultSet rs, Statement ps,
Connection conn) throws SQLException {
try {
if (rs != null)
rs.close();
} finally {
try {
if (ps != null)
ps.close();
} finally {
if (conn != null)
conn.close();
}
}
}
}
--创建UserBean
import java.io.Serializable;
public class UserBean implements Serializable {
private static final long serialVersionUID = -5224532459029766280L;
private Long id;
private String username;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(System.in);
aa: while (true) {
System.out.println("注册新用户----A");
System.out.println("登录系统----L");
System.out.println("显示所有用户信息----S");
System.out.println("修改数据-----------U");
System.out.println("删除数据-----------D");
System.out.println("退出系统------E");
String str = s.nextLine();
if (str != null && str.trim().length() > 0) {
char c = str.charAt(0);
System.out.println(c);
UserBean user = null;
switch (c) {
case 'A':
case 'a':
user = readInput(s);
createUser(user);
break;
case 'L':
case 'l':
while (true) {
user = readLoginInput(s);
if (user != null) {
boolean bb = loginUser(user);
if (bb) {
System.out.println("登录成功!");
break;
} else {
System.out.println("登录失败!请重新登录");
}
}
}
break;
case 'S':
case 's':
List<UserBean> ulist = showAllUser();
if (ulist != null && ulist.size() > 0) {
System.out.println("用户编号 用户名称 用户口令");
System.out.println("==============================");
for (UserBean temp : ulist) {
System.out.println(temp.getId() + " "
+ temp.getUsername() + " "
+ temp.getPassword());
}
System.out.println("==============================");
} else {
System.out.println("没有对应的用户数据");
}
break;
case 'D':
case 'd':
deleteUser(s);
break;
case 'U':
case 'u':
updateUser(s);
break;
default:
System.out.println("感谢使用本系统.....");
break aa;
}
}
}
}
private static void deleteUser(Scanner s) throws Exception {
System.out.println("请输入删除的用户编号");
long id = s.nextLong();
s.nextLine();
UserBean user = loadById(id);
if (user != null) {
Connection conn = null;
Statement stmt = null;
try {
conn = ConnectionManager.getConnection();
String sql = "delete from t_users where id=" + id;
stmt=conn.createStatement();
int len=stmt.executeUpdate(sql);
if(len>0)
System.out.println("删除成功!");
} finally {
ConnectionManager.closeConnection(null, stmt, conn);
}
}
}
private static void updateUser(Scanner s) throws Exception {
System.out.println("请输入修改的用户编号");
long id = s.nextLong();
s.nextLine();
UserBean user = loadById(id);
if (user != null) {
System.out.println("用户名称为" + user.getUsername() + ",请输入新名称:");
String newName = s.nextLine();
if (newName == null || newName.trim().length() < 1)
newName = user.getUsername();
System.out.println("用户口令为" + user.getPassword() + ",请输入新口令:");
String newPassword = s.nextLine();
if (newPassword == null || newPassword.trim().length() < 1)
newPassword = user.getPassword();
user.setUsername(newName);
user.setPassword(newPassword);
Connection conn = null;
PreparedStatement ps = null;
try {
conn = ConnectionManager.getConnection();
String sql = "update t_users set username=?,password=? where id=?";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
ps.setLong(3, user.getId());
int len = ps.executeUpdate();
if (len > 0)
System.out.println("修改成功!");
} finally {
ConnectionManager.closeConnection(null, ps, conn);
}
}
}
private static UserBean loadById(long id) throws Exception {
UserBean res = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = ConnectionManager.getConnection();
String sql = "select * from t_users where id=?";
ps = conn.prepareStatement(sql);
ps.setLong(1, id);
rs = ps.executeQuery();
if (rs.next()) {
res = new UserBean();
res.setId(rs.getLong("id"));
res.setUsername(rs.getString("username"));
res.setPassword(rs.getString("password"));
}
} finally {
ConnectionManager.closeConnection(rs, ps, conn);
}
return res;
}
private static List<UserBean> showAllUser() throws Exception {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<UserBean> res = new ArrayList<UserBean>();
try {
conn = ConnectionManager.getConnection();
String sql = "select * from t_users";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
UserBean temp = new UserBean();
temp.setId(rs.getLong("id"));
temp.setUsername(rs.getString("username"));
temp.setPassword(rs.getString("password"));
res.add(temp);
}
} finally {
ConnectionManager.closeConnection(rs, ps, conn);
}
return res;
}
private static boolean loginUser(UserBean user) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = ConnectionManager.getConnection();
String sql = "select * from t_users where username=? and password=?";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
rs = ps.executeQuery();
return rs.next();
} finally {
try {
if (rs != null)
rs.close();
} finally {
try {
if (ps != null)
ps.close();
} finally {
if (conn != null)
conn.close();
}
}
}
}
private static void createUser(UserBean user) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
try {
Class.forName("oracle.jdbc.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:EDUASK", "yanjun",
"123456");
String sql = "insert into t_users values(seq_users.nextval,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
int len = ps.executeUpdate();
if (len > 0)
System.out.println("添加数据成功!");
} finally {
try {
if (ps != null)
ps.close();
} finally {
if (conn != null)
conn.close();
}
}
}
private static UserBean readLoginInput(Scanner s) {
System.out.println("输入用户名:");
String username = s.nextLine();
while (username == null || username.trim().length() < 1)
username = s.nextLine();
System.out.println("输入口令:");
String password = s.nextLine();
while (password == null || password.trim().length() < 1)
password = s.nextLine();
UserBean user = new UserBean();
user.setUsername(username);
user.setPassword(password);
return user;
}
private static UserBean readInput(Scanner s) {
System.out.println("输入用户名:");
String username = s.nextLine();
while (username == null || username.trim().length() < 1)
username = s.nextLine();
System.out.println("输入口令:");
String password = s.nextLine();
while (password == null || password.trim().length() < 1)
password = s.nextLine();
System.out.println("输入确认口令:");
String repassword = s.nextLine();
while (repassword == null || repassword.trim().length() < 1)
repassword = s.nextLine();
if (!password.equals(repassword)) {
System.out.println("两次输入口令不一致!");
return readInput(s);
} else {
UserBean user = new UserBean();
user.setUsername(username);
user.setPassword(repassword);
return user;
}
}
}