创建一个类,内部有两个方法,一个连接数据库,一个关闭资源。
import java.sql.*;
public class Connect {
static Connection con = null;
// 连接数据库(将获取数据库连接,包装成一个方法)
public static Connection getConnect() {
try {
// 加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库
String url = "jdbc:mysql://localhost:3306/students?useUnicode=true&characterEncoding=utf-8";
String name = "root";
String password = "root";
con = DriverManager.getConnection(url, name, password);
if (con != null) {
System.out.println("connect success1");
} else {
System.out.println("connect failed1");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
// 关闭资源
public static void close(PreparedStatement ps, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
创建数据库的类(将所有的信息封装起来)(抽血toString方法,获取所有的get,set方法,以及创建一个带参和一个不带参的构造函数):
public class Info {
private int id;
private String name;
private String sex;
private int age;
private String address;
private int tel;
public Info(int id, String name, String sex, int age, String address,
int tel) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.address = address;
this.tel = tel;
}
@Override
public String toString() {
return "Info [id=" + id + ", name=" + name + ", sex=" + sex + ", age="
+ age + ", address=" + address + ", tel=" + tel + "]";
}
public Info() {
super();
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getTel() {
return tel;
}
public void setTel(int tel) {
this.tel = tel;
}
}
创建测试类,去调用数据库内部的数据,及增删改查:
import java.sql.*;
import java.util.Scanner;
public class Test {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
public void insert(String name, String sex, int age, String address, int tel)
throws Exception {
con = Connect.getConnect();
String sql = "INSERT INTO info(name,sex,age,address,tel) VALUES(?,?,?,?,?)";
ps = con.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, sex);
ps.setInt(3, age);
ps.setString(4, address);
ps.setInt(5, tel);
int i = ps.executeUpdate();
if (i == 1) {
System.out.println("insert success");
} else {
System.out.println("insert failed2");
}
}
public void updata(String name, String sex, int age, String address,
int tel, int id) throws SQLException {
con = Connect.getConnect();
String sql = "update info set name=?,sex=?,age=?,address=?,tel=? where id=?";
ps = con.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, sex);
ps.setInt(3, age);
ps.setString(4, address);
ps.setInt(5, tel);
ps.setInt(6, id);
int i = ps.executeUpdate();
if (i == 1) {
System.out.println("updata success");
} else {
System.out.println("updata failed2");
}
}
public void delete(int id) throws SQLException {
con = Connect.getConnect();
String sql = "delete from info where id=?";
ps = con.prepareStatement(sql);
ps.setInt(1, id);
int i = ps.executeUpdate();//在增删改当中获取是否操作成功,成功返回1,失败返回0(只有增删改有这个方法)
if (i == 1) {
System.out.println("delete success");
} else {
System.out.println("delete failed2");
}
}
public void select(int id) throws SQLException {
con = Connect.getConnect();
String sql = "select name,sex,age from info where id=?";
ps = con.prepareStatement(sql);
ps.setInt(1, id);
ResultSet i = ps.executeQuery();//获取信息集
while (i.next()) {
String name = i.getString("name");//取出相对应的信息,通过字段取出
String sex = i.getString("sex");
int age = i.getInt("age");
System.out.println(name + "\t" + sex + "\t" + age);
}
}
public void menu() {
Scanner input = new Scanner(System.in);
System.out.println("******欢迎来到数据库的增删改查********");
System.out.println("1.增\t2.删\t3.改\t4.查");
System.out.println("请选择相对应的操作编号:");
int num = 0;
while (true) {
if (input.hasNext()) {
num = input.nextInt();
break;
} else {
System.out.println("请重新输入:");
}
}
try {
switch (num) {
case 1://增加
System.out.println("请输入增加后的名字:");
String name = input.next();
System.out.println("请输入增加后的性别:");
String sex = input.next();
System.out.println("请输入增加后的年龄");
int age = input.nextInt();
System.out.println("请输入增加后的地址:");
String adress = input.next();
System.out.println("请输入增加后的电话号码:");
int tel = input.nextInt();
new Test().insert(name, sex, age, adress, tel);
break;
case 2://删除
System.out.println("请输入要进行删除的id号:");
int id = input.nextInt();
new Test().delete(id);
break;
case 3://修改
System.out.println("请输入修改后的名字:");
String name1 = input.next();
System.out.println("请输入修改后的性别:");
String sex1 = input.next();
System.out.println("请输入修改后的年龄");
int age1 = input.nextInt();
System.out.println("请输入修改后的地址:");
String adress1 = input.next();
System.out.println("请输入修改后的电话号码:");
int tel1 = input.nextInt();
System.out.println("请输入要进行修改的id号:");
int id1 = input.nextInt();
new Test().updata(name1, sex1, age1, adress1, tel1,id1);
break;
case 4:
System.out.println("请输入要进行查看信息的id号:");
int id2 = input.nextInt();
new Test().select(id2);
break;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("是否要返回主界面:(y/n)");
String a = input.next();
if (a.equals("y")) {
menu();
}else{
Connect.close(ps, rs);
System.out.println("欢迎退出!");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test a = new Test();
a.menu();
// try {
// // a.insert("zdc","男",22,"河南省",123456789);
// // a.updata("帅哥", "女", 23, "长葛", 123456789, 103);
// a.delete(1);
a.select(103);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
}
登录检测:
import java.sql.*;
public class Login {
static Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
public void loginTest(String name) throws SQLException{
boolean f = cheakLogin(name);
if (f) {
System.out.println("login success");
}else{
System.out.println("login false");
}
}
public boolean cheakLogin(String name) throws SQLException{
boolean f = false;
String sql = "select id,name from info where name=?";
con = Connect.getConnect();
ps = con.prepareStatement(sql);
ps.setString(1, name);
rs = ps.executeQuery();
if (rs.next()) {
f=true;
}
return f;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
new Login().loginTest("zdc");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}