欢迎光临
感谢一路有你

javaweb mysql的增删改查

如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
 

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

引入mysql jar


<code class="">&lt;dependency>
      &lt;groupId>mysql&lt;/groupId>
      &lt;artifactId>mysql-connector-java&lt;/artifactId>
      &lt;version>5.1.47&lt;/version>
&lt;/dependency>
</code>


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

        }
    }
}

</code>


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

        }
    }
}


</code>


<code class="">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(&quot;com.mysql.jdbc.Driver&quot;);
            //2.创建连接
            connection = DriverManager.getConnection
                    (&quot;jdbc:mysql://120.78.193.246:3306/jdd?useSSL=true&amp;&quot; +
                            &quot;characterEncoding=utf-8&amp;user=&quot; +
                            &quot;jdd&amp;password=fEh4mJFPRG87z3k4&quot;);
            System.out.println(&quot;创建连接成功&quot;);
            //3.写sql
            String sql = &quot;update bookinfo set book_name=?,store=?  where book_id=?&quot;;

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

        }
    }
}


</code>


<code class="">package net.jdbc.test;

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

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

            while(resultSet.next()){ //遍历结果集,取出数据
                int book_id = resultSet.getInt(&quot;book_id&quot;);
                String book_name = resultSet.getString(&quot;book_name&quot;);
                BigDecimal price = resultSet.getBigDecimal(&quot;price&quot;);
                Date public_date = resultSet.getDate(&quot;public_date&quot;);
                String store = resultSet.getString(&quot;store&quot;);
                //输出数据
                System.out.print(&quot;图书编号:&quot;+book_id);
                System.out.print(&quot;,图书名称:&quot;+book_name);
                System.out.print(&quot;,价格&quot;+price);
                System.out.print(&quot;,出版日期&quot;+public_date);
                System.out.print(&quot;,库存&quot;+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();
        }
    }
}

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

相关推荐

  • 暂无文章

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

支付宝扫一扫打赏

微信扫一扫打赏

×
订阅图标按钮