接口 | 作用 |
---|---|
Statement接口 | 用于执行静态的sql语句 |
PreparedStatement接口 | 用于执行预编译sql语句 |
CallableStatement接口 | 用于执行存储过程的sql语句(call xxx) |
package com.rk.db.b_statement; import java.sql.DriverManager; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; /** * 使用Statement执行DDL语句(创建表) * @author RK * */ public class Demo01 { static//静态代码段 { try { //1.驱动注册程序 Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); throw new RuntimeException(e); } } public static void main(String[] args) { Connection conn = null; Statement stmt = null; String url = "jdbc:mysql://localhost:3306/testdb"; String user = "root"; String password = "root"; try { //2.获取连接对象 conn = DriverManager.getConnection(url, user, password); //3.创建Statement stmt = conn.createStatement(); //4.准备sql String sql = "CREATE TABLE T_Persons(Id INT PRIMARY KEY AUTO_INCREMENT, UserName VARCHAR(20), Pwd VARCHAR(20))"; //5.发送sql语句,执行sql语句,得到返回结果 int count = stmt.executeUpdate(sql); //6.输出 System.out.println("影响了"+count+"行!");//影响了0行! } catch (SQLException e) { e.printStackTrace(); } finally { //7.关闭连接(顺序:后打开的先关闭) closeQuietly(stmt); closeQuietly(conn); } } /** * 安静的关闭 */ private static void closeQuietly(AutoCloseable ac) { if(ac != null) { try { ac.close(); } catch (Exception e) { e.printStackTrace(); } } } }
package com.rk.db.b_statement; import java.sql.DriverManager; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; /** * 使用Statement执行DML语句 * @author RK * */ public class Demo02 { static { try { //1.驱动注册程序 Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } public static void main(String[] args) { Connection conn = null; Statement stmt = null; String url = "jdbc:mysql://localhost:3306/testdb"; String user = "root"; String password = "root"; try { //2.获取连接对象 conn = DriverManager.getConnection(url, user, password); //3.创建Statement stmt = conn.createStatement(); //4.准备sql String sql = "INSERT INTO T_Persons(UserName,Pwd) VALUES(‘地球人‘,‘123456‘)";//增加 //String sql = "UPDATE T_Persons SET UserName=‘火星人‘ WHERE Id=1";//修改 //String sql = "DELETE FROM T_Persons WHERE Id=1";//删除 //5.发送sql语句,执行sql语句,得到返回结果 int count = stmt.executeUpdate(sql); //6.输出 System.out.println("影响了"+count+"行!");//影响了1行! } catch (SQLException e) { e.printStackTrace(); } finally { closeQuietly(stmt); closeQuietly(conn); } } /** * 安静的关闭 */ private static void closeQuietly(AutoCloseable ac) { if(ac != null) { try { ac.close(); } catch (Exception e) { e.printStackTrace(); } } } }
package com.rk.db.b_statement; import java.sql.DriverManager; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; /** * 使用Statement执行DQL语句(查询操作) * @author RK * */ public class Demo03 { static { try { //1.驱动注册程序 Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; String url = "jdbc:mysql://localhost:3306/testdb"; String user = "root"; String password = "root"; try { //2.获取连接对象 conn = DriverManager.getConnection(url, user, password); //3.创建Statement stmt = conn.createStatement(); //4.准备sql String sql = "SELECT * FROM T_Persons"; //5.发送sql语句,执行sql语句,得到返回结果 rs = stmt.executeQuery(sql); //6.输出 while(rs.next()) { int id = rs.getInt("Id"); String userName = rs.getString("UserName"); String pwd = rs.getString("Pwd"); System.out.println(id + "\t" + userName + "\t" + pwd); } } catch (SQLException e) { e.printStackTrace(); } finally { closeQuietly(rs); closeQuietly(stmt); closeQuietly(conn); } } /** * 安静的关闭 */ private static void closeQuietly(AutoCloseable ac) { if(ac != null) { try { ac.close(); } catch (Exception e) { e.printStackTrace(); } } } }
原文地址:http://lsieun.blog.51cto.com/9210464/1772371