java中的JDBC

时间:2025-03-05 19:49:55
Statement s = ();
        //现在要向数据库的表中插入一条数据
                                                    //这里对应的是数据库中的字段
        /*int a = ("INSERT INTO suser(sname,passworde,brithday,timet)" +

                 //这里是java中自己定义的变量,用于向数据库中存入想要存入的数据                     "VALUE('"+name+"','"+password+"','"+brithdays+"',now())");*/


        //对指定条件的行进行数据修改
        /*int a = ("update suser set sname='"+name+"',passworde='"+password+"'," +
                "brithday='"+brithdays+"'where num="+num);*/


        //删除
        int a = ("delete from suser where num="+num);
        (a);

(2)获得PrepareStatement执行sql语句

sql 语句中参数位置使用占位符 , 使用 setXX 方法向 sql 中设置参数
PrepareStatement ps = (sql);
PrepareStatement 中的方法 :
Int executeUpdate() 用于执行 ddl 语句和 dml( , , ) 语句 返回操作的行数
用于执行 ddl 语句返回 0
用于执行 dml 语句返回操作的行数
 PreparedStatement ps  =  //将sql预编译到preparedstatement对象中,并没有执行
                ("insert into suser(sname,passworde,brithday,timet) value(?,?,?,?)");
        //接下来设置值
        (1,username);
        (2,userpassword);
        (3,brithday);
        (4,new . Date());
        //执行
        ();

4.关闭与数据库的链接通道

每次操作完成后关闭所有与数据库交互的通道,将statement于connection全部关闭
();
();
();
();
5. PreparedStatement Statement
1、代码的可读性和可维护性.
虽然用PreparedStatement来代替Statement会使代码多出几行,但这样的代码无论从可读性还是可维护性上来说.都比直接用Statement的代码高很多档次:
("insert into tb_name (col1,col2,col2,col4) values
('"+var1+"','"+var2+"',"+var3+",'"+var4+"')");
perstmt = ("insert into tb_name (col1,col2,col2,col4) values (?,?,?,?)");
(1,var1);
(2,var2);
(3,var3);
(4,var4);
(); //prestmt是 PreparedStatement 对象实例
可以看出来PreparedStatement要看起来更为简单直观
2.提高了安全性
(1)以 Statement为例
 String num = "1000 or 1=1";//sql注入  sql攻击
        ("");

        Connection connection = ("jdbc:mysql://127.0.0.1:3306/charroom?serverTimezone=Asia/Shanghai","root","121212");

        //执行
        Statement statement = ();
                  int a = ("delete from suser where num="+num);


//这种操作会直接把数据库表中的所有数据删除

(2)以PreparedStatement为例

String num = "1000 or 1=1";//sql注入  sql攻击
        ("");

        Connection connection = ("jdbc:mysql://127.0.0.1:3306/charroom?serverTimezone=Asia/Shanghai","root","121212");

        //执行
        PreparedStatement pt = ("delete from suser where num=?");
        (1,num);//set方法中对传入的值进行检测,一个?对应一个值,不能有其他的关键字
        ();

//这种方法在运行程序时它会检测到错误从而报错阻止操作继续进行

6.结果集处理(也就是将数据库表中的数据提取出来)

PreparedStatement Statement 中的 executeQuery() 方法中会返回一个ResultSet 对象 , 查询结果就封装在此对象中 .
1.使用 ResultSet 中的 next() 方法获得下一行数据
2.使用 getXXX(String name) 方法获得值
(1)将一行数据取出来
首先自己定义一个类
public class User {
    private  String name;
    private  String password;
    private  Date brithday;
    private  Date time;
    private  int num;
    public int getNum() {
        return num;
    }

    public void setNum(int num) {
         = num;
    }

    public void setName(String name) {
         = name;
    }

    public void setPassword(String password) {
         = password;
    }

    public void setBrithday(Date brithday) {
         = brithday;
    }

    public void setTime(Date time) {
         = time;
    }



    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", brithday=" + brithday +
                ", time=" + time +
                ", num=" + num +
                '}';
    }
}

public class Demo7 {
    public static void main(String[] args) {
        String num = "6";

        User user = null;
        try {
            user = new Demo7().dd(num);
        } catch (ClassNotFoundException | SQLException e) {
            ();
        }

        (user);
    }
    public  User dd(String num) throws ClassNotFoundException, SQLException {
        PreparedStatement pt=null;
        Connection connection = null;
        User user=null;
        try{
            ("");
             connection = ("jdbc:mysql://127.0.0.1:3306/charroom?serverTimezone=Asia/Shanghai","root","121212");
            pt = ("select * from suser where num=?");
            (1,num);
            ResultSet st = ();//执行查询操作
            //从ResultSet中将数据传入到我们自己定义的对象中
            while (()){
                 user = new User();
                (("num"));
                (("sname"));
                (("passworde"));
                (("brithday"));
                (("timet"));
            }

        }finally {
                ();
                ();

        }


        return user;
    }
}

(2)将表中的所有数据取出来,用到了集合的思想

public class Demo8 {


    public static void main(String[] args) {

        ArrayList<User> users = null;
        try {
            users = new Demo8().dd();
        } catch (ClassNotFoundException | SQLException e) {
            ();
        }


        (users);
    }
    public  ArrayList<User> dd() throws ClassNotFoundException, SQLException {
        PreparedStatement pt=null;
        Connection connection = null;
        ArrayList<User> users = new ArrayList<>();
        User user=null;
        try{
            ("");
             connection = ("jdbc:mysql://127.0.0.1:3306/charroom?serverTimezone=Asia/Shanghai","root","121212");
            pt = ("select * from suser ");

            ResultSet st = ();//执行查询操作
            //从ResultSet中将数据传入到我们自己定义的对象中
            while (()){
                 user = new User();
                (("num"));
                (("sname"));
                (("passworde"));
                (("brithday"));
                (("timet"));
                (user);
            }

        }finally {
                ();
                ();

        }


       return users;
    }
}