package jdbchomework;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class JdbcWork2
{
static Connection connection = null;
static PreparedStatement preparedStatement = null;
static ResultSet set = null;
// 设计users表:
// uid int 自增 主键
// username varchar
// pwd varchar
// age int
// gender enum('','')
// phoneNumber varchar
// 实现如下要求:
// 一.在现有操作方法下,完成添加/删除/修改 三种类型的操作:
// 1. 向Users表中添加一条记录
// 2. 删除users表中编号为1的记录
// 3. 修改Users表中编号为2的用户姓名为 张三丰
/**
* @param args
*/
public static void ui()
{
System.out.println("========================");
System.out.println("请选择你的操作..");
System.out.println("1:添加数据\t\t,2:删除数据\t\t,3:修改数据");
System.out.println("========================");
}
//插入数据
public static void add()
{
String sql = "insert into users values(?,?,?,?,?,?)";
try
{
preparedStatement = connection.prepareStatement(sql);
boolean flag = true;
while (flag)
{
preparedStatement.setString(1, null);
System.out.println("请输入用户姓名");
String username = new Scanner(System.in).next();
preparedStatement.setString(2, username);
System.out.println("请输入用户密码");
String pwd = new Scanner(System.in).next();
preparedStatement.setString(3, pwd);
System.out.println("请输入用户年龄");
String age = new Scanner(System.in).next();
preparedStatement.setString(4, age);
System.out.println("请输入用户性别");
String gender = new Scanner(System.in).next();
preparedStatement.setString(5, gender);
System.out.println("请输入用户电话号码");
String phoneNubmer = new Scanner(System.in).next();
preparedStatement.setString(6, phoneNubmer);
int lines = preparedStatement.executeUpdate();
if (lines != 0)
{
System.out.println("插入成功");
}
System.out.println("你要继续插入数据吗?是(输入1)/否(输入0)");
int num = new Scanner(System.in).nextInt();
if (num == 0)
{
flag = false;
}
if (num == 1)
{
flag = true;
}
// System.out.print(flag);
}
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void delete()
{
// 删除数据
System.out.println("删除数据...");
boolean flag2 = true;
while (flag2)
{
System.out.println("请输入你要删除那一条数据");
int num2 = new Scanner(System.in).nextInt();
String sql2 = "delete from users where uid=" + num2 + "";
// System.out.println(num2);
int lines2 = 0;
try
{
// System.out.println(lines2);
preparedStatement = connection.prepareStatement(sql2);
lines2 = preparedStatement.executeUpdate();
if (lines2 != 0)
{
System.out.println("删除成功.....");
} else
{
System.out.println("删除失败");
}
System.out.println("请问你是否继续删除:是(输入1)/否(输入0)");
int num3 = new Scanner(System.in).nextInt();
if (num3 == 1)
{
flag2 = true;
}
if (num3 == 0)
{
flag2 = false;
}
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void update()
{
// 修改数据
System.out.println("开始修改数据..");
System.out.println("请输入要修改数据的编号");
int num4 = new Scanner(System.in).nextInt();
try
{
String sql3 = "update users set username=?,pwd=?,age=?,gender=?,phoneNumber=? where uid=" + num4 + "";
preparedStatement = connection.prepareStatement(sql3);
System.out.println("输入修改姓名");
String username = new Scanner(System.in).next();
preparedStatement.setString(1, username);
System.out.println("输入修改密码");
String pwd = new Scanner(System.in).next();
preparedStatement.setString(2, pwd);
System.out.println("输入修改年龄");
String age = new Scanner(System.in).next();
preparedStatement.setString(3, age);
System.out.println("输入修改性别");
String sex = new Scanner(System.in).next();
System.out.println(sex);
preparedStatement.setString(4, sex);
System.out.println("输入修改电话");
String phone = new Scanner(System.in).next();
preparedStatement.setString(5, phone);
int lines4 = preparedStatement.executeUpdate();
if (lines4 != 0)
{
System.out.println("修改成功");
} else
{
System.out.println("修改失败");
}
} catch (Exception e)
{
// TODO: handle exception
}
// System.out.println("连接成功");
}
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
String url = "jdbc:mysql://127.0.0.1:3306/scor?characterEncoding=utf-8&useSSL=false";
String user = "root";
String password = "12345";
try
{
connection = DriverManager.getConnection(url, user, password);
ui();
System.out.println("请输入你的选择");
int i = new Scanner(System.in).nextInt();
boolean j = true;
while (j)
{
switch (i)
{
case 1:
add();
break;
case 2:
delete();
break;
case 3:
update();
break;
}
System.out.println("你是否继续操作:是(输入1)/否(输入0)");
int num0 = new Scanner(System.in).nextInt();
if (num0 == 1)
{
j = true;
} else
{
j = false;
}
if (num0 == 1)
{
ui();
System.out.println("请输入你的选择");
i = new Scanner(System.in).nextInt();
}
}
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}