目的成果:链接MySQL实现数据增删改查
程序代码:
package bookuser;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class UIdata {
//图书馆用户的数据单独增删改查完成,用户交互swtch以及Scanner处理。
public static void update() {//数据更新
try {
Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8", "root", "123456");
Statement stmt=con.createStatement();
String sql="UPDATE user SET pwd='999' WHERE name='wangwu'";
stmt.executeUpdate(sql);
System.out.println("updata ok");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void delect() {//数据删除
try {
Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8", "root", "123456");
//Scanner sc1 = new Scanner(System.in);
Statement stmt=con.createStatement();
String sql="DELECT *FROM user WHERE name='wangwu'";
stmt.executeUpdate(sql);
System.out.println("delect ok");
//sc1.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void insert() {//数据插入
try {
Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8", "root", "123456");
//Scanner sc1 = new Scanner(System.in);
//String str1=sc1.next();
//String str2=sc1.next();
Statement stmt=con.createStatement();
String sql="INSERT INTO user(name,pwd) values('aaaa','1235456')";
stmt.executeUpdate(sql);
System.out.println("updata ok");
//sc1.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void select() {//数据查询
try {
Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8", "root", "123456");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT * FROM USER");
while(rs.next()) {
int id=rs.getInt(1);
String name=rs.getString("name");
String pwd=rs.getString(3);
System.out.println(id+" "+name+" "+pwd);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("增加数据:1");
System.out.println("删除数据:2");
System.out.println("修改数据:3");
System.out.println("查询数据:4");
System.out.println("退出:0");
Scanner sc = new Scanner(System.in);
int str =sc.nextInt();
switch(str){
case 0 :
break; //可选
case 1 :
insert();
System.out.println("成功增加数据");
break; //可选
case 2:
delect();
System.out.println("成功删除数据");
break;
case 3:
update();
System.out.println("成功修改数据");
case 4:
select();
System.out.println("成功查询数据");
break;
default :
break;
}
sc.close();
}
}
实现效果