JDBC增删改查
/* db.properties的配置 driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/day14 username=root password=seeker */ package cn.itcast.demo; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import org.junit.Test; import cn.itcast.jdbcutils.JdbcUtils; //使用jdbc对数据库进行增删改查
//导包的时候不要导具体的驱动包
public class Demo4 { Connection conn=null; Statement st=null; ResultSet rs=null; @Test public void insert(){ try{ Connection conn = JdbcUtils.getConnection(); st=conn.createStatement(); String sql="insert into users(id,name,password,email,birthday) values(4,'xxx','123','xx@sina.com','1980-09-09')"; int num = st.executeUpdate(sql);//返回一个整型数据,影响了数据几行 if(num>0){ System.out.println("插入成功"); } }catch (Exception e){ }finally{ JdbcUtils.release(conn, st, rs); } } @Test public void delete(){ try{ Connection conn = JdbcUtils.getConnection(); st=conn.createStatement(); String sql="delete from users where id=4"; int num = st.executeUpdate(sql);//返回一个整型数据,影响了数据几行 if(num>0){ System.out.println("删除成功"); } }catch (Exception e){ }finally{ JdbcUtils.release(conn, st, rs); } } @Test public void update(){ try{ Connection conn = JdbcUtils.getConnection(); st=conn.createStatement(); String sql="update users set name='wang',email='wu@.sina.com' where id=3"; int num = st.executeUpdate(sql);//返回一个整型数据,影响了数据几行 if(num>0){ System.out.println("更新成功"); } }catch (Exception e){ }finally{ JdbcUtils.release(conn, st, rs); } } @Test public void find(){ try{ Connection conn = JdbcUtils.getConnection(); st=conn.createStatement(); String sql="select id,name,password,email,birthday from users where id=1"; rs = st.executeQuery(sql);//返回一个整型数据,影响了数据几行 if(rs.next()){ System.out.println(rs.getString("name")); } }catch (Exception e){ }finally{ JdbcUtils.release(conn, st, rs); } } } /*JdbcUtils package cn.itcast.jdbcutils; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; 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 prop = new Properties(); prop.load(in); driver=prop.getProperty("driver"); url=prop.getProperty("url"); username=prop.getProperty("username"); password=prop.getProperty("password"); Class.forName(driver); } catch (IOException e) { throw new ExceptionInInitializerError(e); } catch (ClassNotFoundException e) { // TODO 自动生成的 catch 块 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 (Exception e){ e.printStackTrace();//异常没有抛给java虚拟机,因此不会终止程序的执行, } } if(st!=null){ try{ st.close(); }catch (Exception e){ e.printStackTrace();//异常没有抛给java虚拟机,因此不会终止程序的执行, } } if(conn!=null){ try{ conn.close(); }catch (Exception e){ e.printStackTrace();//异常没有抛给java虚拟机,因此不会终止程序的执行, } } } } */