JDBC的3种Statment

JDBC提供了3种向数据库发送SQL语句的类:

  • Statment
  • PrepareStatment
  • CallableStatment

同时也提供了3种方法来操作数据库:

  • executeUpdate(String sql)
    返回受影响的行的数据
  • executeQuery(String sql)
    返回ResultSet
  • execute(String sql)(不常用)
    可以返回多个结果。如果返回受影响的行的数据,则返回false。如果返回ResultSet则返回true。

演示

  • Statment - createStatement() - stmt.execute()
public class StatementDemo {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       String url = "jdbc:mysql://localhost:3306/school";
       String username = "root";
       String password = "";

       try
       {
           Class.forName("com.mysql.jdbc.Driver");
           Connection conn = DriverManager.getConnection(url, username, password);
           //String sql = "insert into Students values (8,'admin',12,'m','[email protected]','abc');";
           String sql = "select * from Students;";
           Statement stmt = conn.createStatement();

           if(stmt.execute(sql))
           {
               System.out.println("查询成功!");
           }
           else
           {
               System.out.println("更新成功!");
           }
           stmt.close();
           conn.close();
       }
       catch(Exception ex)
       {
           ex.printStackTrace();
       }
    }
}
  • Statment - createStatement() - stmt.executeQuery()
public class ResultSetDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String url = "jdbc:mysql://localhost:3306/school";
        String username = "root";
        String password = "";

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, username, password);
            // String sql =
            // "insert into Students values (8,'admin',12,'m','[email protected]','abc');";
            String sql = "select * from Students;";
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            while(rs.next())
            {
                /*
                System.out.println(rs.getInt("sid") + ","
                        + rs.getString("sname") + "," + rs.getInt("age"));*/

                System.out.println(rs.getInt(1) + ","
                        + rs.getString(2) + "," + rs.getInt(3));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

            try {
                stmt.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}
  • prepareStatement - stmt.setInt(index, value) | stmt.setString(index, value) - stmt.executeUpdate()
public class PreparedStatementDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //以下URL写法,会出现中文乱码
        //String url = "jdbc:mysql://localhost:3306/school";
        //解决中文乱码问题
        String url = "jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=UTF-8";

        String username = "root";
        String password = "";

        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, username, password);
            // String sql =
            // "insert into Students values (8,'admin',12,'m','[email protected]','abc');";
            String sql = "insert into Students values (?,?,?,?,?,?);";
            stmt = conn.prepareStatement(sql);
            //注意:下标从1开始。
            stmt.setInt(1, 11);
            stmt.setString(2, "王老五");
            stmt.setInt(3, 24);
            stmt.setString(4, "男");
            stmt.setString(5, "[email protected]");
            stmt.setString(6, "上海市浦东新区");

            int result = stmt.executeUpdate();
            if(result>0)
            {
                System.out.println("插入成功!");
            }
            else
            {
                System.out.println("插入失败!");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

            try {
                stmt.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

你可能感兴趣的:(JavaSE基础视频教程)