JDBC之CRUD练习

1、jdbc连接步骤

package cn.jdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.Statement;

public class Demo1 {
    public static void main(String[] args) throws Exception {
    	//注册驱动(可省略)
        Class.forName("com.mysql.jdbc.Driver");
        //定义sql语句
        String sql = "update stu set salary = 500 where id = 2";
        //获取Connection对象
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1", "root", "");
        //获取执行sql的对象Statement
        Statement stmt = conn.createStatement();
        //执行sql,返回的是影响的行数
        int count = stmt.executeUpdate(sql);
        System.out.println(count);
        stmt.close();
        conn.close();

    }
}

2、添加数据库表中数据

package cn.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Demo2 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String sql = "insert into stu values(null, '狗狗', 3000)";
            conn = DriverManager.getConnection("jdbc:mysql:///db1", "root", "");
            stmt = conn.createStatement();
            int count = stmt.executeUpdate(sql);
            System.out.println(count);
            if (count > 0) System.out.println("添加成功");
            else System.out.println("添加失败");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

3、修改表中信息

package cn.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Demo3 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接对象
            conn = DriverManager.getConnection("jdbc:mysql:///db1", "root", "");
            //3.定义sql
            String sql = "update stu set salary = 1500 where id = 4";
            //4.获取执行sql对象
            stmt = conn.createStatement();
            //5.执行sql
            int count = stmt.executeUpdate(sql);
            //6.处理结果
            System.out.println(count);
            if (count > 0) System.out.println("修改成功");
            else System.out.println("修改失败");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //7.释放资源,防止空指针异常
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

上一篇:2022年JAVA短信加语音组合方案API


下一篇:Denial of Service