mysql之PreparedStatement的增删改
2021/8/29 19:36:12
本文主要是介绍mysql之PreparedStatement的增删改,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
编写配置文件【db.properties】:
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/jdbcStudy?useUnicode=true&characterEncoding=utf8&useSSL=true username=root password=admin#1234
编写连接数据库的工具类【JdbcUtils】
package com.yeyue.lesson02; import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; public class JdbcUtils { private static String dirver = null; private static String url = null; private static String username = null; private static String password = null; static { try{ //读取到配置文件 InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties"); Properties properties = new Properties(); properties.load(in); //获取到配置文件中的有用参数 dirver = properties.getProperty("driver"); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); //加载驱动 Class.forName(dirver); } catch (Exception e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { //连接数据库 return DriverManager.getConnection(url,username,password); } public static void release(Connection conn, Statement st, ResultSet rs){ //关闭连接 if(rs!=null){ try { rs.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if(st!=null){ try { st.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
删除【delete】
package com.yeyue.lesson03; import com.yeyue.lesson02.JdbcUtils; import java.sql.*; public class TestDelete { public static void main(String[] args) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = JdbcUtils.getConnection(); String sql = "DELETE FROM users WHERE id = ?"; st = conn.prepareStatement(sql); st.setInt(1,6); int i = st.executeUpdate(); if(i>0){ System.out.println("删除成功"); } } catch (SQLException throwables) { throwables.printStackTrace(); }finally { JdbcUtils.release(conn,st,rs); } } }
新增【insert】
package com.yeyue.lesson03; import com.yeyue.lesson02.JdbcUtils; import java.util.Date; import java.sql.*; public class TestInsert { public static void main(String[] args) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = JdbcUtils.getConnection(); //INSERT INTO `users` (id,NAME,PASSWORD,email) VALUES (6,'wewwqw','2323','323wwqd') String sql = "INSERT INTO `users` (id,NAME,PASSWORD,email,birthday) VALUES (?,?,?,?,?)"; st = conn.prepareStatement(sql); //预编译sql 不执行 st.setInt(1,7); st.setString(2,"wewwqw"); st.setString(3,"2323"); st.setString(4,"323wwqd"); //java.sql.Date 数据库 java.util.Date java new Date().getTime() 获取时间戳 st.setDate(5,new java.sql.Date(new Date().getTime())); int i = st.executeUpdate(); if(i>0){ System.out.println("插入成功"); } } catch (SQLException throwables) { throwables.printStackTrace(); }finally { JdbcUtils.release(conn,st,rs); } } }
改【update】
package com.yeyue.lesson03; import com.yeyue.lesson02.JdbcUtils; import java.sql.*; public class TestUpdate { public static void main(String[] args) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; try { conn = JdbcUtils.getConnection(); String sql = "UPDATE users SET NAME = 'yeyue1111' WHERE id = ?"; st = conn.prepareStatement(sql); st.setInt(1,4); int i = st.executeUpdate(); if(i>0){ System.out.println("更新成功"); } } catch (SQLException throwables) { throwables.printStackTrace(); }finally { JdbcUtils.release(conn,st,rs); } } }
这篇关于mysql之PreparedStatement的增删改的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-25如何部署MySQL集群资料:新手入门教程
- 2024-12-24MySQL集群部署资料:新手入门教程
- 2024-12-24MySQL集群资料详解:新手入门教程
- 2024-12-24MySQL集群部署入门教程
- 2024-12-24部署MySQL集群学习:新手入门教程
- 2024-12-24部署MySQL集群入门:一步一步搭建指南
- 2024-12-07MySQL读写分离入门:轻松掌握数据库读写分离技术
- 2024-12-07MySQL读写分离入门教程
- 2024-12-07MySQL分库分表入门详解
- 2024-12-07MySQL分库分表入门指南