Java—Web(项目)使用JDBC对医生系统进行增删改查操作

添加:

实现代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Jdbc02 {
    public static void main(String[] args) {
        String url = "jdbc:mysql://127.0.0.1:3306/hospital?useUnicode=true&characterEncoding=UTF-8";
        String username = "root";
        String password = "root";

        //加载数据库访问驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //创建数据库链接
            Connection conn = DriverManager.getConnection(url,username,password);
            System.out.println("数据库已经连接成功了....");
            String sql = "INSERT INTO hospitalstaff(hospitalId,postNum,section,staffName,staffSex,staffAge,birth,staffHome,fixedTelephone,moveTelephone) " +
                    "VALUES(110,7,403,'刘哲','男',49,'1971-11-25','新疆乌鲁木齐','070-154385','13565874258')\n";
            Statement st = conn.createStatement();
            int rows = st.executeUpdate(sql);
            if (rows>0){
                System.out.println("添加成功");
            }else{
                System.out.println("添加失败");
            }
            st.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("数据库链接失败....");
        }
    }
}

实现效果:
在这里插入图片描述

在这里插入图片描述修改:
实现代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Jdbc02 {
    public static void main(String[] args) {
        String url = "jdbc:mysql://127.0.0.1:3306/hospital?useUnicode=true&characterEncoding=UTF-8";
        String username = "root";
        String password = "root";

        //加载数据库访问驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //创建数据库链接
            Connection conn = DriverManager.getConnection(url,username,password);
            System.out.println("数据库已经连接成功了....");
            /*String sql = "INSERT INTO hospitalstaff(hospitalId,postNum,section,staffName,staffSex,staffAge,birth,staffHome,fixedTelephone,moveTelephone) " +
                    "VALUES(110,7,403,'刘哲','男',49,'1971-11-25','新疆乌鲁木齐','070-154385','13565874258')\n";*/
            String sql="update hospitalstaff set postNum=8,section=403,staffName='李源',staffSex='女',staffAge=35,birth='1985-4-11',staffHome='宁夏银川',fixedTelephone='016-123548',moveTelephone='15326982463' where staffNum=8";
            Statement st = conn.createStatement();
            int rows = st.executeUpdate(sql);
            if (rows>0){
                //System.out.println("添加成功");
                System.out.println("修改成功");
            }else{
                //System.out.println("添加失败");
                System.out.println("修改失败");
            }
            st.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("数据库链接失败....");
        }
    }
}

实现效果:
在这里插入图片描述在这里插入图片描述删除:
实现代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Jdbc02 {
    public static void main(String[] args) {
        String url = "jdbc:mysql://127.0.0.1:3306/myschool?useUnicode=true&characterEncoding=UTF-8";
        String username = "root";
        String password = "root";

        //加载数据库访问驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //创建数据库链接
            Connection conn = DriverManager.getConnection(url,username,password);
            System.out.println("数据库已经连接成功了....");
            /*String sql = "INSERT INTO myschool(hospitalId,postNum,section,staffName,staffSex,staffAge,birth,staffHome,fixedTelephone,moveTelephone) " +
                    "VALUES(110,7,403,'刘哲','男',49,'1971-11-25','新疆乌鲁木齐','070-154385','13565874258')\n";*/
            //String sql="update myschool set postNum=8,section=403,staffName='李源',staffSex='女',staffAge=35,birth='1985-4-11',staffHome='宁夏银川',fixedTelephone='016-123548',moveTelephone='15326982463' where staffNum=8";
            String sql ="DELETE FROM student WHERE studentNo=7";
            Statement st = conn.createStatement();
            int rows = st.executeUpdate(sql);
            if (rows>0){
                //System.out.println("添加成功");
                //System.out.println("修改成功");
                System.out.println("删除成功");
            }else{
                //System.out.println("添加失败");
                //System.out.println("修改失败");
                System.out.println("删除失败");
            }
            st.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("数据库链接失败....");
        }
    }
}

实现效果:
在这里插入图片描述在这里插入图片描述查询:
实现代码:

package jdbc01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class TestJdbc1{

    public static void main(String[] args) throws Exception {
        String url="jdbc:mysql://127.0.0.1:3306/myschool?useUnicode=true&characterEncoding=UTF-8";
        String username="root";
        String password="root";

        //加载数据库的访问驱动
        //Class.forName("com.mysql.cj.jdbc.Driver");
        Class.forName("com.mysql.jdbc.Driver");

        //创建数据库链接
        Connection conn= DriverManager.getConnection(url,username,password);
        System.out.println("数据库连接成功~~~");


        //发送Sql语句
        String sql="select * from student";

        Statement st=conn.createStatement();

        ResultSet rs=st.executeQuery(sql);

        //处理结果集
        while(rs.next()){
            String studentName = rs.getString("studentName");
            String loginPwd=rs.getString("loginPwd");
            System.out.println(studentName);
            System.out.println(loginPwd);
        }
        //关闭资源
        rs.close();
        st.close();
        conn.close();

    }

}

实现效果:

在这里插入图片描述以上均为个人所写,如有错误,欢迎指正,谢谢~~

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值