package com.jdbc.demo2; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.junit.Test; /** * JDBC的CRUD的操作 * @author lyt * */ public class JDBCDemo2 { @Test /** * 查询单条记录 */ public void demo5() { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { //注册驱动 Class.forName("com.mysql.jdbc.Driver"); //获得连接 conn = DriverManager.getConnection("jdbc:mysql:///test", "root", "oracle"); //创建SQL语句对象 stmt = conn.createStatement(); //编写SQL String sql = "select * from user"; //执行SQL语句 rs = stmt.executeQuery(sql); if(rs.next()) { System.out.println(rs.getInt("id") " " rs.getString("username") " " rs.getString("password")); } }catch(Exception e) { e.printStackTrace(); }finally { if(conn!=null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } conn = null; } if(stmt!=null) { try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } stmt = null; } if(rs!=null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } conn = null; } } } @Test /** * 查询多条记录 */ public void demo4() { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { //注册驱动 Class.forName("com.mysql.jdbc.Driver"); //获得连接 conn = DriverManager.getConnection("jdbc:mysql:///test", "root", "oracle"); //创建SQL语句对象 stmt = conn.createStatement(); //编写SQL String sql = "select * from user"; //执行SQL语句 rs = stmt.executeQuery(sql); while(rs.next()) { System.out.println(rs.getInt("id") " " rs.getString("username") " " rs.getString("password")); } }catch(Exception e) { e.printStackTrace(); }finally { if(conn!=null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } conn = null; } if(stmt!=null) { try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } stmt = null; } if(rs!=null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } conn = null; } } } @Test /** * 删除操作的代码实现 */ public void demo3() { Connection conn = null; Statement stmt = null; try { //注册驱动 Class.forName("com.mysql.jdbc.Driver"); //获得连接 conn = DriverManager.getConnection("jdbc:mysql:///test","root","oracle"); //创建SQL语句对象 stmt = conn.createStatement(); //编写SQL语句 String sql = "delete from user where id=6"; //执行SQL语句 int num = stmt.executeUpdate(sql); if(num>0) { System.out.println("ID为6的用户删除成功"); } }catch(Exception e){ e.printStackTrace(); }finally { if(conn!= null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(stmt!= null) { try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } @Test /** * 修改操作代码实现 */ public void demo2() { Connection conn= null; Statement stmt =null; try { //注册驱动 Class.forName("com.mysql.jdbc.Driver"); //获得连接 conn = DriverManager.getConnection("jdbc:mysql:///test", "root", "oracle"); //执行操作 //创建执行SQL语句的对象 stmt = conn.createStatement(); //编写SQL语句 String sql = "update user set password=‘abc‘ where id=4"; //执行SQL int num = stmt.executeUpdate(sql); if(num>0) { System.out.println("修改密码成功!!"); } }catch(Exception e) { e.printStackTrace(); }finally { if(conn!=null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } conn =null; } if(stmt!=null) { try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } conn =null; } } } @Test /** * 保存操作的代码实现 */ public void demo1() { Connection conn = null; Statement stmt = null; try { //注册驱动; Class.forName("com.mysql.jdbc.Driver"); //获得连接 conn = DriverManager.getConnection("jdbc:mysql:///test", "root", "oracle"); //创建执行SQL语句对象 stmt = conn.createStatement(); //编写SQL语句 String sql = "insert into user values(null,‘eee‘,‘123‘,‘小米‘,21)"; //执行SQL int num = stmt.executeUpdate(sql); if(num>0) { System.out.println("保存用户成功!!"); } }catch(Exception e) { e.printStackTrace(); }finally { //资源释放 if(stmt!=null) { try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } stmt = null; } if(conn!=null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } conn = null; } } } }
配置文件如上图