【无标题】Statement对象详解
代码实现
1.提取工具类
package com.yang.yang.lesson02.utils; import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; public class jdbcUtils { private static String driver =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);properties.getProperty("driver");properties.getProperty("url");properties.getProperty("username");properties.getProperty("password");//1.驱动只用加载一次Class.forName(driver); } catch (IOException | ClassNotFoundException e) {throw new RuntimeException(e);}}//获取连接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 e) {throw new RuntimeException(e);} }if(st!=null){try {st.close();} catch (SQLException e) {throw new RuntimeException(e);}}if(conn!=null){try {conn.close();} catch (SQLException e) {throw new RuntimeException(e);}} } }
2.编写增删改的方法,excuteUpdate
增
package com.yang.yang.lesson02.utils; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestInsert {public static void main(String[] args) {Connection conn = null;Statement st = null;ResultSet rs = null;try {conn = jdbcUtils.getConnection();//获取数据库连接st= conn.createStatement();//获得sql的执行对象String sql = "insert into users(id,'NAME','PASSWORD','email','birthday')"+"VALUES (4,'yangyang','123456','223525365@qq.com','1020-2-2')" ;int i = st.executeUpdate(sql);if (i > 0){System.out.println("插入成功");}} catch (SQLException e) {throw new RuntimeException(e);}finally {jdbcUtils.release(conn,st,rs); }} }
删
package com.yang.yang.lesson02.utils; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestDelete {public static void main(String[] args) {Connection conn = null;Statement st = null;ResultSet rs = null;try {conn = jdbcUtils.getConnection();//获取数据库连接st= conn.createStatement();//获得sql的执行对象String sql = "DELETE FROM users WHERE id = 4" ;int i = st.executeUpdate(sql);if (i > 0){System.out.println("删除成功");}} catch (SQLException e) {throw new RuntimeException(e);}finally {jdbcUtils.release(conn,st,rs); }}} }
改
package com.yang.yang.lesson02.utils; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestUpdate {public static void main(String[] args) {Connection conn = null;Statement st = null;ResultSet rs = null;try {conn = jdbcUtils.getConnection();//获取数据库连接st= conn.createStatement();//获得sql的执行对象String sql = "UPDATE users SET NAME = 'yangyang' ,'email'='2345778@qq.com' WHERE id =1" ;int i = st.executeUpdate(sql);if (i > 0){System.out.println("更新成功");}} catch (SQLException e) {throw new RuntimeException(e);}finally {jdbcUtils.release(conn,st,rs); }}} }
3.查询
package com.yang.yang.lesson02.utils; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestSelect {public static void main(String[] args) {Connection conn = null;Statement st = null;ResultSet rs = null;try {conn = jdbcUtils.getConnection();conn.createStatement();//SQl String sql = "select * from users where id =1";rs= st.executeQuery(sql);//查询完毕会返回一个结果集while (rs.next()){System.out.println(rs.getString("NAME"));}} catch (SQLException e) {throw new RuntimeException(e);}finally {jdbcUtils.release(conn,st,rs);}} }