编写配置文件【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); } } }