import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* 数据库连接
* @author wu.meilan
*
*/
public class DbConnection {
private Connection conn=null;
public Connection getConn(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:scott/tiger@localhost:1521:inspur");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/**
* 用户表
* @author wu.meilan
*
*/
public class Users {
private String uname;
private String password;
public Users(String uname, String password) {
this.uname = uname;
this.password = password;
}
public Users(String uname){
this.uname=uname;
}
public Users(){
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class UserDao {
private Connection conn=null;
private PreparedStatement pstmt=null;
private Statement st=null;
private ResultSet rs =null;
private ResultSetMetaData meta=null;
/**
* 用户验证
*
*/
public void checkUser(){
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String name="",password="";
try {
System.out.println("请输入用户名和密码:");
name=(br.readLine()).trim();
password=(br.readLine()).trim();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn=(new DbConnection()).getConn();
try {
st=conn.createStatement();
String sql="select * from users";
rs=st.executeQuery(sql);
while(rs.next()){
if(name.equals(rs.getString(1))){
if(password.equals(rs.getString(2))){
System.out.println("登陆成功!!");
}
else{
System.out.println("密码不正确!!");
}
break;
}
}
if(rs.isAfterLast())
System.out.println("用户不存在!!!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(rs!=null)
rs.close();
if(st!=null)
st.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 插入用户信息
*
*/
public void insertUser(){
conn=(new DbConnection()).getConn();
String sql="Insert into users(uname,password) values(?,?)";
String name="",password="";
name=JOptionPane.showInputDialog("请输入用户名!!!");
password=JOptionPane.showInputDialog("请输入密码!!!");
Users user=new Users(name,password);
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, user.getUname());
pstmt.setString(2, user.getPassword());
int count=pstmt.executeUpdate();
System.out.println("插入成功!!!");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}finally{
try {
if(pstmt!=null)
pstmt.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 删除用户信息
* @param user 要删除的用户
*/
public void deleteUser(Users user){
conn=(new DbConnection()).getConn();
String sql="delete from users where uname=?";
try {
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, user.getUname());
int count=pstmt.executeUpdate();
if(count!=0)
System.out.println("删除成功!!!");
else
System.out.println("不存在该用户!!!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(pstmt!=null)
pstmt.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 修改用户信息
* @param user 要修改的用户
*/
public void updateUsers(Users user){
conn=(new DbConnection()).getConn();
String sql="update users set uname=?,password=? where uname=?";
try {
pstmt=conn.prepareStatement(sql);
String name="",password="";
name=JOptionPane.showInputDialog("请输入用户名!!!");
password=JOptionPane.showInputDialog("请输入密码!!!");
pstmt.setString(1, name);
pstmt.setString(2, password);
pstmt.setString(3, user.getUname());
int count=pstmt.executeUpdate();
if(count!=0)
System.out.println("修改成功!!!");
else
System.out.println("不存在该用户!!!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(pstmt!=null)
pstmt.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JOptionPane;
/**
* 测试
* @author wu.meilan
*
*/
public class ConnTest {
public static void main(String[] args) {
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("************");
int choice=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try {
while(true){
choice=Integer.parseInt(br.readLine());
switch(choice){
case 1:
new UserDao().checkUser();
break;
case 2:
new UserDao().insertUser();
break;
case 3:
String name1="";
name1=JOptionPane.showInputDialog("请输入要删除的用户:");
Users user1=new Users(name1);
new UserDao().deleteUser(user1);
break;
case 4:
String name2="";
name2=JOptionPane.showInputDialog("请输入要修改的用户:");
Users user2=new Users(name2);
new UserDao().updateUsers(user2);
break;
case 5:
System.exit(0);
}
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}