1.sqlite建立表user
CREATE TABLE user(name text,
pwd text);
2.插入记录(省去...)
3.实现类:
package day7_2.d06;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class manageMent {
public static void loginUser(){
System.out.println("请输入您的登录信息:");
System.out.println("-------------------------------------");
Connection conn=null;
try {
conn = DriverManager.getConnection("jdbc:sqlite:/c:/sqlite/db/my.db");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(true){
Scanner sc=new Scanner(System.in);
System.out.println("请输入用户名:");
String name=sc.nextLine();
System.out.println("请输入密码:");
String pwd=sc.nextLine();
String sql="select * from user where name=? and pwd=?";
try {
Class.forName("org.sqlite.JDBC");
PreparedStatement stmt=conn.prepareStatement(sql);
stmt.setString(1,name);
stmt.setString(2,pwd);
ResultSet rs=stmt.executeQuery();
if(rs.next()){
System.out.println("登录成功!");
break;
}else{
System.out.println("登录失败!");
}
} catch (Exception e) {
e.printStackTrace();
}
}//while
try {
conn.close();
load();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void findAllUser() {
String sql="select * from User";
try {
Class.forName("org.sqlite.JDBC");
Connection conn=DriverManager.getConnection("jdbc:sqlite:/c:/sqlite/db/my.db");
PreparedStatement stmt=conn.prepareStatement(sql);
ResultSet rs=stmt.executeQuery();
while(rs.next()){
System.out.println("用户名:"+rs.getString(1)+"-----"+"密码:"+rs.getString(2));
}
rs.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void addUser(){
Scanner sc=new Scanner(System.in);
System.out.println("请输入添加的用户名:");
String name=sc.nextLine();
System.out.println("请输入添加的密码:");
String pwd=sc.nextLine();
//String sql="insert into user values("+name+","+pwd+")";
//String sql="insert into user values('ch','ch')";
String sql="insert into user values('"+name+"'"+","+"'"+pwd+"'"+")";
try {
Class.forName("org.sqlite.JDBC");
Connection conn=DriverManager.getConnection("jdbc:sqlite:/c:/sqlite/db/my.db");
Statement stmt=conn.createStatement();
int rs=stmt.executeUpdate(sql);
if(rs>0){
System.out.println("添加成功!");
}else{
System.out.println("添加失败!");
}
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void deleteUser() {
Scanner sc=new Scanner(System.in);
System.out.println("请输入删除的用户名:");
String name=sc.nextLine();
String sql="delete from User where name="+"'"+name+"'";
try {
Class.forName("org.sqlite.JDBC");
Connection conn=DriverManager.getConnection("jdbc:sqlite:/c:/sqlite/db/my.db");
Statement stmt=conn.createStatement();
int rs=stmt.executeUpdate(sql);
if(rs>0){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void modifyUser() {
Scanner sc=new Scanner(System.in);
System.out.println("请输入您修改的用户名:");
String name=sc.nextLine();
System.out.println("请输入新的密码:");
String pwd=sc.nextLine();
String sql="update user set pwd="+"'"+pwd+"'"+"where name="+"'"+name+"'";
try {
Class.forName("org.sqlite.JDBC");
Connection conn=DriverManager.getConnection("jdbc:sqlite:/c:/sqlite/db/my.db");
Statement stmt=conn.createStatement();
int rs=stmt.executeUpdate(sql);
if(rs>0){
System.out.println("修改成功!");
}else{
System.out.println("修改失败 !");
}
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void load(){
while(true){
System.out.println("欢迎来到账户信息管理系统:");
System.out.println("--------------------------");
System.out.println("1:增加用户信息");
System.out.println("2:删除用户信息");
System.out.println("3:修改用户信息");
System.out.println("4:查看用户信息");
System.out.println("----------------------------");
System.out.println("请选择:");
Scanner sc=new Scanner(System.in);
int choose=0;
while(true){
try {
choose = sc.nextInt();
break;
} catch (Exception e) {
sc.next();//清除上次的输入信息
System.out.println("请输入合法信息:");
}
}
switch (choose) {
case 1:
addUser();
break;
case 2:
deleteUser();
break;
case 3:
modifyUser();
break;
case 4:
findAllUser();
break;
}
System.out.println("按任意键回到首页:");
String name=sc.next();
}//while
}
public static void main(String[] args) {
loginUser();
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章