今天又是新的摸鱼一天

主要任务–了解JDBC并且学会使用

早上的任务

跟着老师学习了JDBC的基本内容,了解到了JDBC是用来可以进行一次java编程而可以在其他的平台上使用的一个工具,他的基本语法基于java,全称叫做Java Database connection,然后我们跟着老师一起,检查了java的版本号,然后MySQL的版本,然后检查有没有文件缺失,然后跟着老师进行了Sqlyog的使用,即是图形化界面的使用,然后我们新建了一个数据库叫做jdbc,然后新建了一个表userinfo,然后对表进行了修改,有一些基本的信息,然后我们进行jdbc的学习,然后老师给出了一个很基本的例子:

package com.zhongruan.wode;

import java.sql.*;

public class Test {
    public static void main(String[] args) {

        ResultSet rs = null;
        PreparedStatement statement = null;
        Connection connection = null;
        try {
            //1.加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.创建连接
            connection = DriverManager.getConnection
                    ("jdbc:mysql://127.0.0.1:3306/user?useSSL=true&" +
                            "characterEncoding=utf-8&user=" +
                            "root&password=root");
            System.out.println("创建连接成功");
            //3.写sql
            String sql = "select * from userinfo";
            //4.得到statement对象
            statement = connection.prepareStatement(sql);
            //5.执行sql得到结果集
            rs = statement.executeQuery();
            //6.处理结果集
            while (rs.next()) {
                System.out.println(rs.getInt(1));
                System.out.println(rs.getString(2));
                System.out.println(rs.getString(3));
            }
            //7.关闭资源
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

然后到了下午,老师带着我们进行了一些jdbc的基于数据库的一些改动以及对上面代码的封装和优化。从以下几个方面展开,我们定义了一个实体类Bean,是专门用来对应数据库的,然后对其进行了封装,使其使用更加安全,可以直接调用实体类来进行对数据库的数据进行处理。然后新建了一个工具类DBUtil是封装了对于我们数据库的连接以及断开的处理。
以下是代码部分:


import com.zhongruan.wode.Bean.Userinfo;
import com.zhongruan.wode.util.DButil;
import java.sql.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

public class Insert {
    public static void main(String[] args) {
        PreparedStatement statement = null;
        Connection connection = null;
        try{
        connection= DButil.getConnection();
        String sql="insert into userinfo(username ,password)values(?,?)";
        statement=connection.prepareStatement(sql);
        statement.setString(1,"xiangcai");
        statement.setString(2,"jianglaoshi");
        statement.executeUpdate();

        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            DButil.close(null,statement,connection);
        }
    }
}
package com.zhongruan.wode.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;

public class DButil {
    //加载驱动
    public static Connection connection=null;
    public static PreparedStatement statement=null;
    public static ResultSet resultSet=null;
    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
            }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }
    //创建连接
   public  static Connection getConnection(){
        try{
       connection = DriverManager.getConnection
               ("jdbc:mysql://127.0.0.1:3306/user?useSSL=true&" +
                       "characterEncoding=utf-8&user=" +
                       "root&password=123456");}
                       catch(SQLException e){
            e.printStackTrace();
                       }
             return connection;

   }
   public static void close(ResultSet rs,PreparedStatement statement,Connection connection){
       if (rs != null) {
           try {
               rs.close();
           } catch (SQLException e) {
               e.printStackTrace();
           }
       }
       if (statement != null) {
           try {
               statement.close();
           } catch (SQLException e) {
               e.printStackTrace();
           }
       }
       if (connection != null) {
           try {
               connection.close();
           } catch (SQLException e) {
               e.printStackTrace();
           }
       }
   }
}
package com.zhongruan.wode.Bean;

public class Userinfo {
    private int id;
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "Userinfo{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
    ```
   至此,今天一天的学习结束。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值