JDBC操作数据库(增删改查,注册登录)
2015-10-11 17:34
405 查看
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
/**
* @author 清澈见底的疯子
*
*/
public class UserServer {
static Scanner sc = new Scanner(System.in);
static Connection conn = null;
//conn = DBUtil.getConnection();
public static void cd() {
System.out.println(" 欢迎登录学生信息管理系统");
System.out.println("-------1 注册----");
System.out.println("-------2 登录----");
System.out.println("-------3 查询----");
System.out.println("-------4 修改----");
System.out.println("-------5 删除----");
System.out.println("-------0 退出----");
}
public static void main(String[] args) {
while(true){
cd();
System.out.println("开始,输入0退出");
int command;
do {
System.out.println("请输入选择的功能:1-5");
command = sc.nextInt();
} while (command!=0 &&command != 1 && command != 2 && command != 3 && command!=4&&command!=5);
switch (command) {
case 1:
register();
break;
case 2:
login();
break;
case 3:
query();
break;
case 4:
modify();
break;
case 5:
delete();
break;
case 0:
System.exit(0);
}
}
}
private static void delete() {
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
System.out.println("请输入要删除的用户名");
String username = scan.nextLine();
System.out.println("请输入此用户ID");
String id = scan.nextLine();
try{
conn = DBUtil.getConnection();
String sql = "delete from StudentInfo where username=? and id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, id);
int n = ps.executeUpdate();
if(n!=0){
System.out.println("信息删除完毕");
}else{
System.out.println("删除失败!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
private static void modify() {
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = scan.nextLine();
System.out.println("请输入新密码:");
String password = scan.nextLine();
System.out.println("请输入性别:");
String sex = scan.nextLine();
System.out.println("请输入班级:");
String grade = scan.nextLine();
System.out.println("请输入邮箱:");
String email = scan.nextLine();
String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
System.out.println(email.matches(check));
while (!email.matches(check)) {
System.out.println("邮箱格式错误,请重新输入:");
email = scan.nextLine();
System.out.println(email.matches(check));
}
try{
conn = DBUtil.getConnection();
String sql = "Update StudentInfo SET password=?,email=?,sex=?,grade=? WHERE username=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, password);
ps.setString(2, email);
ps.setString(3, sex);
ps.setString(4, grade);
ps.setString(5, username);
int n = ps.executeUpdate();
if(n!=0){
System.out.println("信息修改完毕");
}else{
System.out.println("修改失败!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
private static void query() {
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
System.out.println("请输入您要查找的学生姓名:");
String username = scan.nextLine();
try{
conn = DBUtil.getConnection();
Statement state = conn.createStatement();
String sql = "SELECT username ,id,email,sex,grade from StudentInfo where username="+"'"+username+"'";
ResultSet rs = state.executeQuery(sql);
if(rs.next()){
System.out.println("信息如下:"+"\n"+"姓名:"+username+","+"班级:"+rs.getString("grade")+"ID:"+rs.getInt("id")+","+"性别:"+rs.getString("sex")+"邮箱:"+rs.getString("email"));
}else{
System.out.println("查无此人!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
private static void login() {
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
System.out.println("欢迎登录!");
System.out.println("请输入用户名:");
String username = scan.nextLine();
System.out.println("请输入密码");
int password = Integer.parseInt(scan.nextLine());
try{
conn = DBUtil.getConnection();
Statement state = conn.createStatement();
String sql = "SELECT username ,password,account from userinfo_fancq where username="+"'"+username+"'"+ " and password="+password;
ResultSet rs = state.executeQuery(sql);
if(rs.next()){
System.out.println("您好"+username+","+"欢迎登录学生管理系统!");
}else{
System.out.println("用户名或密码不正确!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
@SuppressWarnings("resource")
private static void register() {
Scanner scan = new Scanner(System.in);
System.out.println("欢迎注册!");
System.out.println("请输入用户名:");
String username = scan.nextLine();
System.out.println("请输入密码");
String password = scan.nextLine();
System.out.println("请输入性别:");
String sex = scan.nextLine();
System.out.println("请输入班级:");
String grade = scan.nextLine();
System.out.println("请输入邮箱");
String email = scan.nextLine();
String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
System.out.println(email.matches(check));
while (!email.matches(check)) {
System.out.println("邮箱格式错误,请重新输入:");
email = scan.nextLine();
System.out.println(email.matches(check));
}
try{
conn = DBUtil.getConnection();
Statement state = conn.createStatement();
String sql = "INSERT INTO StudentInfo ( id,username,password,email,sex,grade ) VALUES"+
" ( seq_id_S.NEXTVAL"+",'"+username+"','"+password+"','"+email+"',"+"'"+sex+"',"+"'"+grade+" )";
int rs = state.executeUpdate(sql);
if(rs==1 ){
System.out.println("恭喜"+username+","+"注册成功");
}else{
System.out.println("请重新注册");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
}
DBUtil代码:
import java.io.FileInputStream;
import java.sql.Connection;
//import java.sql.DriverManager;
import java.util.Properties;
import org.apache.commons.dbcp.BasicDataSource;
public class DBUtil {
//DBCP 连接池
private static BasicDataSource ds;
static{
try {
//读取配置文件
//java.util.Properties用来读取配置文件
Properties prop = new Properties();
//通过文件流读取并解析配置文件内容
prop.load(
new FileInputStream("config.properties")
);
String driverName = prop.getProperty("driverName");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
//最大连接数
int maxActive = Integer.parseInt(
prop.getProperty("maxActive")
);
//最大等待时间
int maxWait = Integer.parseInt(
prop.getProperty("maxWait")
);
ds = new BasicDataSource();
ds.setDriverClassName(driverName);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
//连接池中的最大连接数
ds.setMaxActive(maxActive);
//最大等待时间 getConnection时有效
ds.setMaxWait(maxWait);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection()throws Exception{
try {
return ds.getConnection();
}catch(Exception e){
System.out.println("连接数据库异常");
throw e;
}
}
public static void close(Connection conn){
if(conn != null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void rollback(Connection conn){
if(conn != null){
try{
conn.rollback();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
/**
* @author 清澈见底的疯子
*
*/
public class UserServer {
static Scanner sc = new Scanner(System.in);
static Connection conn = null;
//conn = DBUtil.getConnection();
public static void cd() {
System.out.println(" 欢迎登录学生信息管理系统");
System.out.println("-------1 注册----");
System.out.println("-------2 登录----");
System.out.println("-------3 查询----");
System.out.println("-------4 修改----");
System.out.println("-------5 删除----");
System.out.println("-------0 退出----");
}
public static void main(String[] args) {
while(true){
cd();
System.out.println("开始,输入0退出");
int command;
do {
System.out.println("请输入选择的功能:1-5");
command = sc.nextInt();
} while (command!=0 &&command != 1 && command != 2 && command != 3 && command!=4&&command!=5);
switch (command) {
case 1:
register();
break;
case 2:
login();
break;
case 3:
query();
break;
case 4:
modify();
break;
case 5:
delete();
break;
case 0:
System.exit(0);
}
}
}
private static void delete() {
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
System.out.println("请输入要删除的用户名");
String username = scan.nextLine();
System.out.println("请输入此用户ID");
String id = scan.nextLine();
try{
conn = DBUtil.getConnection();
String sql = "delete from StudentInfo where username=? and id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, id);
int n = ps.executeUpdate();
if(n!=0){
System.out.println("信息删除完毕");
}else{
System.out.println("删除失败!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
private static void modify() {
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = scan.nextLine();
System.out.println("请输入新密码:");
String password = scan.nextLine();
System.out.println("请输入性别:");
String sex = scan.nextLine();
System.out.println("请输入班级:");
String grade = scan.nextLine();
System.out.println("请输入邮箱:");
String email = scan.nextLine();
String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
System.out.println(email.matches(check));
while (!email.matches(check)) {
System.out.println("邮箱格式错误,请重新输入:");
email = scan.nextLine();
System.out.println(email.matches(check));
}
try{
conn = DBUtil.getConnection();
String sql = "Update StudentInfo SET password=?,email=?,sex=?,grade=? WHERE username=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, password);
ps.setString(2, email);
ps.setString(3, sex);
ps.setString(4, grade);
ps.setString(5, username);
int n = ps.executeUpdate();
if(n!=0){
System.out.println("信息修改完毕");
}else{
System.out.println("修改失败!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
private static void query() {
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
System.out.println("请输入您要查找的学生姓名:");
String username = scan.nextLine();
try{
conn = DBUtil.getConnection();
Statement state = conn.createStatement();
String sql = "SELECT username ,id,email,sex,grade from StudentInfo where username="+"'"+username+"'";
ResultSet rs = state.executeQuery(sql);
if(rs.next()){
System.out.println("信息如下:"+"\n"+"姓名:"+username+","+"班级:"+rs.getString("grade")+"ID:"+rs.getInt("id")+","+"性别:"+rs.getString("sex")+"邮箱:"+rs.getString("email"));
}else{
System.out.println("查无此人!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
private static void login() {
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
System.out.println("欢迎登录!");
System.out.println("请输入用户名:");
String username = scan.nextLine();
System.out.println("请输入密码");
int password = Integer.parseInt(scan.nextLine());
try{
conn = DBUtil.getConnection();
Statement state = conn.createStatement();
String sql = "SELECT username ,password,account from userinfo_fancq where username="+"'"+username+"'"+ " and password="+password;
ResultSet rs = state.executeQuery(sql);
if(rs.next()){
System.out.println("您好"+username+","+"欢迎登录学生管理系统!");
}else{
System.out.println("用户名或密码不正确!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
@SuppressWarnings("resource")
private static void register() {
Scanner scan = new Scanner(System.in);
System.out.println("欢迎注册!");
System.out.println("请输入用户名:");
String username = scan.nextLine();
System.out.println("请输入密码");
String password = scan.nextLine();
System.out.println("请输入性别:");
String sex = scan.nextLine();
System.out.println("请输入班级:");
String grade = scan.nextLine();
System.out.println("请输入邮箱");
String email = scan.nextLine();
String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
System.out.println(email.matches(check));
while (!email.matches(check)) {
System.out.println("邮箱格式错误,请重新输入:");
email = scan.nextLine();
System.out.println(email.matches(check));
}
try{
conn = DBUtil.getConnection();
Statement state = conn.createStatement();
String sql = "INSERT INTO StudentInfo ( id,username,password,email,sex,grade ) VALUES"+
" ( seq_id_S.NEXTVAL"+",'"+username+"','"+password+"','"+email+"',"+"'"+sex+"',"+"'"+grade+" )";
int rs = state.executeUpdate(sql);
if(rs==1 ){
System.out.println("恭喜"+username+","+"注册成功");
}else{
System.out.println("请重新注册");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
}
DBUtil代码:
import java.io.FileInputStream;
import java.sql.Connection;
//import java.sql.DriverManager;
import java.util.Properties;
import org.apache.commons.dbcp.BasicDataSource;
public class DBUtil {
//DBCP 连接池
private static BasicDataSource ds;
static{
try {
//读取配置文件
//java.util.Properties用来读取配置文件
Properties prop = new Properties();
//通过文件流读取并解析配置文件内容
prop.load(
new FileInputStream("config.properties")
);
String driverName = prop.getProperty("driverName");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
//最大连接数
int maxActive = Integer.parseInt(
prop.getProperty("maxActive")
);
//最大等待时间
int maxWait = Integer.parseInt(
prop.getProperty("maxWait")
);
ds = new BasicDataSource();
ds.setDriverClassName(driverName);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
//连接池中的最大连接数
ds.setMaxActive(maxActive);
//最大等待时间 getConnection时有效
ds.setMaxWait(maxWait);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection()throws Exception{
try {
return ds.getConnection();
}catch(Exception e){
System.out.println("连接数据库异常");
throw e;
}
}
public static void close(Connection conn){
if(conn != null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void rollback(Connection conn){
if(conn != null){
try{
conn.rollback();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
相关文章推荐
- sqlserver字段类型详解
- centos6 PHP安装memcache和memcached
- 查看已经安装的mysql编译参数
- sql server 2000里ldf损坏,只有mdf的恢复
- Mysql、MariaDB 传统主从集群配置 推荐
- 个人学习笔记——数据库:范式
- MySQL 5.0-触发器(参考)
- 用批处理命令自动安装SQL 2000
- mysqldump命令备份与还原mysql数据库方法详解
- Oracle数据库常用Sql语句大全
- mybatis11 sqlMapConfig.xml文件说明
- SQLServer2012导出表数据为SQL脚本
- 关于Mongodb的全面总结,学习mongodb的人,可以从这里开始!
- MySql常用命令总结 mysql命令大全
- phpredis 安装
- 【Redis基础】SSH 中Spring-data-redis使用体验
- ORACLE数据库查询语句、触发器积累
- 数据库的锁机制
- MySQL SQLSTATE[42000]: Syntax error or access violation: 1286 Unknown table engine 'InnoDB' 错误的原因及解决
- [转]sql语句优化原则