public class DButil {
private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
//数据库连接地址
private static final String URL = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
//用户名
private static final String USER_NAME = "root";
//密码
private static final String PASSWORD = "980526";
public static Connection getConnection(){
Connection connection=null;
try{
Class.forName(DRIVER_NAME);
connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
}catch(Exception e){
e.printStackTrace();
}
return connection;
}
public sta
tic void close(ResultSet resultSet) {
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(PreparedStatement statement) {
if(statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Connection connection) {
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public class Demo {
public Demo(String username, String password){
this.username=username;
this.password=password;
}
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Demo{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
public void insertUser(Demo newUser) {
Connection connection = DButil.getConnection();
PreparedStatement pstm = null;
try {
String sql = "insert into demo(id,username,password) values(?,?,?)"; //sql语言
pstm = connection.prepareStatement(sql);
//填充sql语句中的?
pstm.setInt(1, newUser.getId());
pstm.setString(2, newUser.getUsername());
pstm.setString(3, newUser.getPassword());
//使用executeUpdate函数执行sql语句
pstm.executeUpdate();
System.out.println("新增用户成功");
}catch(Exception e){
e.printStackTrace();
}finally {
DButil.close(pstm);
DButil.close(connection);
}
}
public void deleteUser(int id) {
Connection connection = DButil.getConnection();
PreparedStatement pstm = null;
try {
String sql = "delete from demo where id=?";
pstm = connection.prepareStatement(sql);
pstm.setInt(1,id);
pstm.executeUpdate();
System.out.println("删除用户成功");
}catch(Exception e){
e.printStackTrace();
}finally {
DButil.close(pstm);
DButil.close(connection);
}
}
public void updateUser(int modId, Demo modUser) {
Connection connection = DButil.getConnection();
PreparedStatement pstm = null;
try{
String sql = "update demo set username=?,password=? where id=?";
pstm = connection.prepareStatement(sql);
pstm.setString(1, modUser.getUsername());
pstm.setString(2, modUser.getPassword());
pstm.setInt(3, modId);
pstm.executeUpdate();
System.out.println("修改用户成功");
}catch(Exception e){
e.printStackTrace();
}finally {
DButil.close(pstm);
DButil.close(connection);
}
}
public static List findAll() {
List list=new ArrayList<>();
ResultSet resultSet=null;
PreparedStatement statement=null;
Connection connection=null;
try {
connection= DButil.getConnection();
//3.写sql
String sql="select * from demo";
//4.得到statement对象
statement = connection.prepareStatement(sql);
//5.执行sql
resultSet = statement.executeQuery();
//6处理结果集
while (resultSet.next()){
int id=resultSet.getInt(1);
String username=resultSet.getString(2);
String password=resultSet.getString(3);
Demo userInfo=new Demo(username,password);
userInfo.setId(id);
list.add(userInfo);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DButil.close(resultSet);
DButil.close(connection);
DButil.close(statement);
}
return list;
}
public static void main(String[] args) {
System.out.println("请选择操作:"+"\t"+"1、增加"+"\t"+"2、删除"+"\t"+"3、修改"+"\t"+"4、查询");
while(true){
Scanner sc=new Scanner(System.in);
int k=sc.nextByte();
switch (k){
case 1:
System.out.println("请输入:"+"\t");
DemoDao test1=new DemoDao();
Scanner sc1 = new Scanner(System.in);
int id1=sc1.nextByte();
Scanner sc2 = new Scanner(System.in);
String newNmae=sc2.next();
Scanner sc3 = new Scanner(System.in);
String newPassword=sc3.next();
Demo newUser=new Demo(newNmae,newPassword);
newUser.setId(id1);
test1.insertUser(newUser);
break;
case 2:
System.out.println("请输入:"+"\t");
DemoDao test2=new DemoDao();
Scanner sc4 = new Scanner(System.in);
int id2=sc4.nextByte();
test2.deleteUser(id2);
break;
case 3:
System.out.println("请输入:"+"\t");
DemoDao test3=new DemoDao();
Scanner sc5 = new Scanner(System.in);
int modId=sc5.nextByte();
Scanner sc6 = new Scanner(System.in);
String newNmae1=sc6.next();
Scanner sc7 = new Scanner(System.in);
String newPassword1=sc7.next();
Demo modUser=new Demo(newNmae1,newPassword1);
test3.updateUser(modId,modUser);
break;
case 4:
DemoDao test4=new DemoDao();
List list=new ArrayList<>();
list=test4.findAll();
System.out.println(list.toString());
break;
}}
}
至此,JDBC的连接以及基本操作都已完成