JDBC

一、JDBC是什么

1、JDBC的概念

在这里插入图片描述

2、JDBC访问数据库步骤

在这里插入图片描述

3、新建JDBC项目

先建一个web工程
在这里插入图片描述
导入jdbc的依赖jar包
在这里插入图片描述
修复导入之后的问题
在这里插入图片描述

4、JDBC代码示例

####################初始代码##############################################
public class TestJDBC {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
    //1、加载驱动
        Class.forName("com.mysql.jdbc.Driver");
    //2、获取连接、数据库地址url、用户名、密码
        String url="jdbc:mysql://192.168.186.100/test";
        Connection conn = DriverManager.getConnection(url,"root","ok");
     //3、执行增删改查等操作
        Statement stat = conn.createStatement();
        String sql="select * from grade";
        ResultSet rs = stat.executeQuery(sql);
        while(rs.next()) {
            int gradeId = rs.getInt(1);
            String gradeName=rs.getString(2);
            System.out.println(gradeId+"\t"+gradeName);
        }
      //4、关闭释放资源
        rs.close();
        stat.close();
        conn.close();
    }
}
############################运行结果########################################
1	大一
3	大三
2	大二
4	大四
5	研一
#########################################提取方法后的jdbc写法#######################
public class TestJDBC {
    private static Connection conn;
    private static Statement stat;
    private static ResultSet rs;
    public static void getConn(){
        //1、加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //2、获取连接、数据库地址url,用户名、密码
        String url="jdbc:mysql://192.168.186.100:3306/test";
        try {
            conn = DriverManager.getConnection(url, "root", "ok");
            if(conn!=null)
                stat=conn.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void query(String sql,String param){
        try {
            if(stat!=null) {
                rs = stat.executeQuery(sql+param);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
//        finally {
//            try {
//                stat.close();
//                conn.close();
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//        }
    }
    public static boolean update(String sql){
        int num = -1;
        try {
            if(stat!=null)
                num = stat.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num>0;
    }
    public static void close(){
        if(null!=rs){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(null!=stat){
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(null!=conn){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws Exception{
        getConn();
//        String sql="insert into grade(gradeId,gradeName) value(5,'研一')";
//        boolean  isCorrect= update(sql);
//        System.out.println(isCorrect?"插入成功":"插入失败");
        String sql = "select * from grade where gradeId=";
        query(sql,"4 or 1=1"); //sql注入 prepareStatement(防止sql注入)
        while (rs.next()) {
            System.out.println(rs.getString(2));
        }
//        String sql = "desc grade";
//        ResultSet set = query(sql);
//        while (set.next()) {
//            int i=1;
//            while (set.getMetaData().getColumnCount() >= i) {
//                System.out.print(set.getObject(i) + "\t|");
//                i++;
//            }
//            System.out.println();
//        }
//        set.close();
        close();
    }
}

二、prepareStatement

public class TestPrepared {
    private static Connection conn;
    private static PreparedStatement pst;
    private static ResultSet rs;
    private static final String URL="jdbc:mysql://192.168.186.100/test";
    public static void getConn(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(URL,"root","ok");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void query(String sql){
        try {
            pst=conn.prepareStatement(sql);
            rs=pst.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static boolean update(String sql){
        int num=0;
        try {
            pst=conn.prepareStatement(sql);
            num=pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num>0;
    }
    public static void close(){
        try {
            if (rs!=null) rs.close();
            if(pst!=null) pst.close();
            if(conn!=null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws SQLException {
        getConn();
        String sql="select * from subject";
        query(sql);
        query(sql);
        while (rs.next()) {
            int i=1;
            while (rs.getMetaData().getColumnCount()>=i) {
                System.out.print(rs.getObject(i)+"\t");
                i++;
            }
            System.out.println();
        }
        close();
    }
}
###############################运行结果############################################
1	高等数学-1	120	1	
2	高等数学-2	110	2	
3	高等数学-3	100	3	
4	高等数学-4	130	4	
5	C语言-1	100	2	
6	C语言-2	110	2	
7	数据结构	110	2	
8	java	150	3	
9	python	120	4	

######################################################################

package cn.kgc.kb11;

import java.sql.*;

/**
 * @Author Wanglei
 * @Date 2021/3/9
 * @Description
 */
public class TestPrepared {
    private static Connection conn;
    private static PreparedStatement pst;
    private static ResultSet rs;
    private static final String URL="jdbc:mysql://192.168.186.100/test";
    public static void getConn(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(URL,"root","ok");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void query(String sql,String...params){
        try {
            pst=conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pst.setObject(i+1,params[i]);
            }
            rs=pst.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static boolean update(String sql,String...params){
        int num=0;
        try {
            pst=conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pst.setObject(i+1,params[i]);
            }
            num=pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num>0;
    }
    public static void close(){
        try {
            if (rs!=null) rs.close();
            if(pst!=null) pst.close();
            if(conn!=null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws SQLException {
        getConn();
        String sql="select * from subject where subjectNo=?";
        query(sql,"3");
        while (rs.next()) {
            int i=1;
            while (rs.getMetaData().getColumnCount()>=i) {
                System.out.print(rs.getObject(i)+"\t");
                i++;
            }
            System.out.println();
        }
        close();
    }
}
##############################运行结果#############################################
3	高等数学-3	100	3	

在这里插入图片描述

三、JDBC的工作原理

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值