JAVA链接MYSQL8.0的代码以及运行

目的成果:链接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();
    }
}

实现效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值