使用Statement執行sql語句
阿新 • • 發佈:2018-12-25
3.1 執行DDL語句
/** * 執行DDL語句(建立表) */ @Test public void test1(){ Statement stmt = null; Connection conn = null; try { //1.驅動註冊程式 Class.forName("com.mysql.jdbc.Driver"); //2.獲取連線物件 conn = DriverManager.getConnection(url, user, password); //3.建立Statement stmt = conn.createStatement(); //4.準備sql String sql = "CREATE TABLE student(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(20),gender VARCHAR(2))"; //5.傳送sql語句,執行sql語句,得到返回結果 int count = stmt.executeUpdate(sql); //6.輸出 System.out.println("影響了"+count+"行!"); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally{ //7.關閉連線(順序:後開啟的先關閉) if(stmt!=null) try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } if(conn!=null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } }
3.2 執行DML語句
/** * 使用Statement執行DML語句 * @author APPle * */ public class Demo2 { private String url = "jdbc:mysql://localhost:3306/day17"; private String user = "root"; private String password = "root"; /** * 增加 */ @Test public void testInsert(){ Connection conn = null; Statement stmt = null; try { //通過工具類獲取連線物件 conn = JdbcUtil.getConnection(); //3.建立Statement物件 stmt = conn.createStatement(); //4.sql語句 String sql = "INSERT INTO student(NAME,gender) VALUES('李四','女')"; //5.執行sql int count = stmt.executeUpdate(sql); System.out.println("影響了"+count+"行"); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally{ //關閉資源 /*if(stmt!=null) try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } if(conn!=null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); }*/ JdbcUtil.close(conn, stmt); } } /** * 修改 */ @Test public void testUpdate(){ Connection conn = null; Statement stmt = null; //模擬使用者輸入 String name = "陳六"; int id = 3; try { /*//1.註冊驅動 Class.forName("com.mysql.jdbc.Driver"); //2.獲取連線物件 conn = DriverManager.getConnection(url, user, password);*/ //通過工具類獲取連線物件 conn = JdbcUtil.getConnection(); //3.建立Statement物件 stmt = conn.createStatement(); //4.sql語句 String sql = "UPDATE student SET NAME='"+name+"' WHERE id="+id+""; System.out.println(sql); //5.執行sql int count = stmt.executeUpdate(sql); System.out.println("影響了"+count+"行"); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally{ //關閉資源 /*if(stmt!=null) try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } if(conn!=null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); }*/ JdbcUtil.close(conn, stmt); } } /** * 刪除 */ @Test public void testDelete(){ Connection conn = null; Statement stmt = null; //模擬使用者輸入 int id = 3; try { /*//1.註冊驅動 Class.forName("com.mysql.jdbc.Driver"); //2.獲取連線物件 conn = DriverManager.getConnection(url, user, password);*/ //通過工具類獲取連線物件 conn = JdbcUtil.getConnection(); //3.建立Statement物件 stmt = conn.createStatement(); //4.sql語句 String sql = "DELETE FROM student WHERE id="+id+""; System.out.println(sql); //5.執行sql int count = stmt.executeUpdate(sql); System.out.println("影響了"+count+"行"); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally{ //關閉資源 /*if(stmt!=null) try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } if(conn!=null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); }*/ JdbcUtil.close(conn, stmt); } } }
3.3 執行DQL語句
/** * 使用Statement執行DQL語句(查詢操作) * @author APPle */ public class Demo3 { @Test public void test1(){ Connection conn = null; Statement stmt = null; try{ //獲取連線 conn = JdbcUtil.getConnection(); //建立Statement stmt = conn.createStatement(); //準備sql String sql = "SELECT * FROM student"; //執行sql ResultSet rs = stmt.executeQuery(sql); //移動游標 /*boolean flag = rs.next(); flag = rs.next(); flag = rs.next(); if(flag){ //取出列值 //索引 int id = rs.getInt(1); String name = rs.getString(2); String gender = rs.getString(3); System.out.println(id+","+name+","+gender); //列名稱 int id = rs.getInt("id"); String name = rs.getString("name"); String gender = rs.getString("gender"); System.out.println(id+","+name+","+gender); }*/ //遍歷結果 while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); String gender = rs.getString("gender"); System.out.println(id+","+name+","+gender); } }catch(Exception e){ e.printStackTrace(); throw new RuntimeException(e); }finally{ JdbcUtil.close(conn, stmt); } } }