JDBC的简单使用

package com.fgy.jdbc;

import java.sql.*;

public class Demo1Jdbc {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 1.导入驱动jar包

        // 2.注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        // 3.获取连接
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1", "root", "root");

        // 4.定义SQL语句
        String sql = "select * from stu";

        // 5.获取statement对象
        Statement statement = con.createStatement();

        // 6.执行SQL语句
        ResultSet rs = statement.executeQuery(sql);

        // 7.处理结果
        while (rs.next()) {
            System.out.println(rs.getString("name"));
        }

        // 8.释放资源
        statement.close();
        con.close();
    }
}
package com.fgy.jdbc;

import java.sql.*;

public class Demo2Jdbc {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement statement = null;
        try {
            // 1.导入驱动jar包

            // 2.注册驱动
            Class.forName("com.mysql.jdbc.Driver");

            // 3.获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1", "root", "root");

            // 4.定义SQL语句
            String sql = "insert into stu(name) values ('筱筱')";

            // 5.获取statement对象
            statement = conn.prepareStatement(sql);

            // 6.执行SQL语句
            // statement.execute();
            int i = statement.executeUpdate();
            System.out.println("受影响的行数:" + i);

            // 7.处理结果
            // ...
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 8.释放资源
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
package com.fgy.jdbc;

import java.sql.*;
import java.util.Scanner;

/**
 * 登录小案例
 */
public class login {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入账号:");
        String name = sc.next();
        System.out.println("请输入密码:");
        String password = sc.next();
        boolean bool = login(name, password);
        if (bool) {
            System.out.println("登陆成功!");
        } else {
            System.out.println("登录失败!");
        }
    }

    private static boolean login(String name, String password) {
        if (name == null || password  == null) {
            return false;
        }

        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;

        try {
            // 1.导入驱动jar包
            // 2.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 3.获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1", "root", "root");
            // 4.定义SQL语句
            // 为了防止SQL注入,不采用字符串拼接的方式,而是采用占位符的方式
            String sql = "select * from user where name = ? and password = ?";
            // 5.获取statement对象
            pstm = conn.prepareStatement(sql);
            pstm.setString(1, name); // 给占位符赋值,参数位置从1开始
            pstm.setString(2, password);
            // 6.执行SQL语句
            rs = pstm.executeQuery();
            // 7.处理结果
            // 结果只有一条数据,移动游标时有数据返回true,没有数据返回false
            return rs.next();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 8.释放资源
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (pstm != null) {
                try {
                    pstm.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return false;
    }
}
posted @ 2020-01-13 21:36  糖不甜,盐不咸  阅读(132)  评论(0编辑  收藏  举报