jdbc

JDBC 搭建

1,导入mysql开发商提供的连接mysql数据库的驱动包

2,加载mysql驱动类

3,建立与数据库的连接通道

4,向数据库发送sql

5,关闭与数据库连接通道

● 注册JDBC驱动程序:

● 这需要初始化驱动程序,这样就可以打开与数据库的通信信道。

Class.forName(“com.mysql.cj.jdbc.Driver”); //反射实现

或者 DriverManager.registerDriver(new Driver());

● 建立与数据库连接:

● 这需要使用DriverManager.getConnection()方法来创建一个

Connection对象,它代表一个物理连接的数据库.

● Connection conn = DriverManager.getConnection(URL,USER,PASS);

URL:jdbc:mysql://ip(127.0.0.1):端口(3306)/数据库名?serverTimezone=Asia/Shanghai

USER:用户名(root)

PASS:密码

●获得Satement执行sql语句

●Statement st = connection.createStatement();

Satement中的方法:

Int executeUpdate(String sql) 用于执行ddl语句和dml(增,删,改)语句 返回操作的行数

用于执行ddl语句返回0 用于执行dml语句返回操作的行数

ResultSet executeQuery(String sql); 用于执行查询语句 返回一个ResultSet 集合

● 关闭与数据库的链接通道

● 每次操作完成后关闭所有与数据库交互的通道

st.close();

rs.close();

conn.close();

ps.close();

public class Demo1 {
​
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
​
        //根据类名加载mysql驱动类
        Class.forName("com.mysql.cj.jdbc.Driver");
        //DriverManager.registerDriver(new Driver());
​
        //建立与数据库的连接,并返回连接对象
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/schooldb?serverTimezone=Asia/Shanghai","root","123");
        System.out.println(connection);
​
        String name = "赵柳";
        String  gender = "男";
        String birthday = "2000-1-2";
        String phone = "15045621789";
​
        //获得Statement,用来发送sql
        Statement st = connection.createStatement();
                  st.executeUpdate(" INSERT INTO student (NAME,gender,birthday,phone)"+
                          " VALUES('"+name+"','"+gender+"','"+birthday+"','"+phone+"')");
​
                  st.close();
                  connection.close();
    }
}
public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/schooldb?serverTimezone=Asia/Shanghai","root","123");
        Statement st =  connection.createStatement();
        String name = "赵六";
        String  gender = "女";
        String birthday = "2001-1-2";
        String phone = "15045621789";
        String num = "24";
​
        //修改
       /* st.executeUpdate("update student set name = '"+name+"',gender = '"+gender+"',birthday = '"+birthday+"'" +
                "where num ="+num);
        */
​
        //删除
        st.executeUpdate("delete from student where num = "+num);
​
        st.close();
        connection.close();
    }
}

● 获得PrepareStatement执行sql语句

● 在sql语句中参数位置使用占位符,使用setXX方法向sql中设置参数

● PrepareStatement ps = connection.prepareStatement(sql);

PrepareStatement中的方法:

Int executeUpdate() 用于执行ddl语句和dml(增,删,改)语句 返回操作的行数

用于执行ddl语句返回0 用于执行dml语句返回操作的行数

ResultSet executeQuery(); 用于执行查询语句 返回一个ResultSet 集合

public class Demo3 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
​
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/schooldb?serverTimezone=Asia/Shanghai","root","123");
​
        String name = "李1";
        String  gender = "男";
        String birthday = "2003-1-2";
        String phone = "15045621787";
        String num = "25";
​
        //预编译方式:预先将sql及参数预编译到PreparedStatement对象中
        //增添
        /*PreparedStatement ps = connection.prepareStatement("insert into student(name,gender,birthday,phone) " +
                                                                "value(?,?,?,?)");
                            ps.setObject(1,name);
                            ps.setObject(2,gender);
                            ps.setObject(3,birthday);
                            ps.setObject(4,phone);
​
                            ps.executeUpdate();//执行*/
​
        //修改
       /* PreparedStatement ps = connection.prepareStatement("update student set name=?,gender=?,birthday=?" +
                                                                "where num=? ");
                            ps.setObject(1,name);
                            ps.setObject(2,gender);
                            ps.setObject(3,birthday);
                            ps.setObject(4,num);
                            ps.executeUpdate();//执行*/
​
        //删除
        PreparedStatement ps = connection.prepareStatement("delete from student where num =?");
                            ps.setObject(1,num);
                            ps.executeUpdate();//执行
​
                        ps.close();
                        connection.close();
​
    }
}

PreparedStatement和Statement的区别

基于以下的原因:

1、代码的可读性和可维护性.

虽然用PreparedStatement来代替Statement会使代码多出几行,但这样的代码无论从可读性还是可维护性上来说.都比直接用Statement的代码高很多档次:

stmt.executeUpdate("insert into tb_name (col1,col2,col2,col4) values 
('"+var1+"','"+var2+"',"+var3+",'"+var4+"')"); 
​
perstmt = con.prepareStatement("insert into tb_name (col1,col2,col2,col4) 
values (?,?,?,?)"); 
perstmt.setString(1,var1); 
perstmt.setString(2,var2); 
perstmt.setString(3,var3); 
perstmt.setString(4,var4); 
perstmt.executeUpdate(); //prestmt是 PreparedStatement 对象实例

2、最重要的一点是极大地提高了安全性.

防止sql注入 Stringsql=“ delete from user where id = ”+num;

如果我们把[or 1=1]作为id传入进来?

delete from tb_name where id = 1 or 1 = 1;

因为‘1’=‘1’肯定成立 而如果你使用预编译语句.你传入的任何内容就不会和原来的语句发生任何匹 配的关系.

预编译模式中每个占位符处,只能插入一个值,而会过滤其他语句

public class Demo4 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
​
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/schooldb?serverTimezone=Asia/Shanghai","root","123");
​
        //PreparedStatement 预先编译sql,向sql中传值是进行检测,一个?是一个占位符,对应一个值
        //是安全的,可以预防sql注入
​
        String num = "10 or 1 = 1";
        /*PreparedStatement ps = connection.prepareStatement("delete  from student where num = ?");
                            ps.setObject(1,num);
                            ps.executeUpdate();
​
                       ps.close();
                       connection.close();*/
​
        //Statement直接将参数拼接到sql中,不能防止sql注入,不安全
        Statement st = connection.createStatement();
                    st.executeUpdate("delete from student where num="+num);
​
                    st.close();
                    connection.close();
    }
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值