欢迎光临
感谢一路有你

javaweb mysql的增删改查

参考文章:https://blog.csdn.net/qq_40718312/article/details/95489257
maven jar包查询地址:https://search.maven.org/

引入mysql jar

<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.47</version>
</dependency>

package net.jdbc.test;
import java.sql.*;

public class Add {
    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://120.78.193.246:3306/jdd?useSSL=true&" +
                            "characterEncoding=utf-8&user=" +
                            "jdd&password=fEh4mJFPRG87z3k4");
            System.out.println("创建连接成功");
            //3.写sql
            String sql = "insert into bookinfo(book_name,price,public_date,store) values(?,?,?,?)";
            //4.得到statement对象
            statement = connection.prepareStatement(sql);
            //5.执行sql得到结果集
            statement =  connection.prepareStatement(sql);
            //6.处理结果集,插入数据
            statement.setString(1, "Rose");
            statement.setString(2, "123");
            statement.setString(3, "2019-02-01");
            statement.setString(4, "1");
            statement.executeUpdate();
            System.out.println("插入成功!");
            //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();
                }
            }

        }
    }
}

package net.jdbc.test;
import java.sql.*;

public class Del {
    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://120.78.193.246:3306/jdd?useSSL=true&" +
                            "characterEncoding=utf-8&user=" +
                            "jdd&password=fEh4mJFPRG87z3k4");
            System.out.println("创建连接成功");
            //3.写sql
            String sql = "delete from bookinfo where book_id=?";
            //4.得到statement对象
            statement = connection.prepareStatement(sql);
            //5.执行sql得到结果集
            statement.setInt(1,2);
            statement.executeUpdate();
            System.out.println("删除成功!");
            //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();
                }
            }

        }
    }
}


package net.jdbc.test;
import java.sql.*;

public class Update {
    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://120.78.193.246:3306/jdd?useSSL=true&" +
                            "characterEncoding=utf-8&user=" +
                            "jdd&password=fEh4mJFPRG87z3k4");
            System.out.println("创建连接成功");
            //3.写sql
            String sql = "update bookinfo set book_name=?,store=?  where book_id=?";

            //4.得到statement对象
            statement = connection.prepareStatement(sql);
            //5.执行sql得到结果集
            statement =  connection.prepareStatement(sql);
            //6.处理结果集,插入数据
            statement.setString(1,"abc");
            statement.setString(2,"789");
            statement.setInt(3,1);
            statement.executeUpdate();
            System.out.println("修改成功!");
            //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();
                }
            }

        }
    }
}


package net.jdbc.test;

import java.math.BigDecimal;
import java.sql.*;

public class Select {
    //数据库url、用户名和密码
    static final String DB_URL="jdbc:mysql://120.78.193.246:3306/jdd?";
    static final String USER="jdd";
    static final String PASS="fEh4mJFPRG87z3k4";
    public static void main(String[] args) {
        try {
            //1、注册JDBC驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2、获取数据库连接
            Connection connection = DriverManager.getConnection(DB_URL, USER, PASS);
            //3、操作数据库
            Statement statement = connection.createStatement();//获取操作数据库的对象
            String sql="select * from bookinfo";
            ResultSet resultSet = statement.executeQuery(sql);//执行sql,获取结果集

            while(resultSet.next()){ //遍历结果集,取出数据
                int book_id = resultSet.getInt("book_id");
                String book_name = resultSet.getString("book_name");
                BigDecimal price = resultSet.getBigDecimal("price");
                Date public_date = resultSet.getDate("public_date");
                String store = resultSet.getString("store");
                //输出数据
                System.out.print("图书编号:"+book_id);
                System.out.print(",图书名称:"+book_name);
                System.out.print(",价格"+price);
                System.out.print(",出版日期"+public_date);
                System.out.print(",库存"+store);
                System.out.println();
            }
            //4、关闭结果集、数据库操作对象、数据库连接
            resultSet.close();
            statement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch(SQLException e){
            e.printStackTrace();
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

赞(1) 打赏
未经允许不得转载:王明昌博客 » javaweb mysql的增删改查
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

隐藏
变装