004、Statement接口---用于执行sql的对象

该对象是执行Connection对象的方法createStatement()获取

一、JDBC访问数据库的步骤

1)注册和加载驱动(可以省略)
2)获取连接
3)Connection获取Statement对象
4)使用Statement对象执行SQL语句
5)返回结果集
6)释放资源

二、tatement作用:
代表一条语句对象,用于发送SQL语句给服务器,用于执行静态 SQL 语句并返回它所生成结果的对象。

三、Statement中的方法:

Statement中的方法
Statement接口中的方法
 
描述
 
int executeUpdate(String sql)
 
用于发送DML语句,增删改的操作,insert、update、delete
参数:SQL语句
返回值:返回对数据库影响的行数
 
ResultSet executeQuery(String sql)
 
用于发送DQL语句,执行查询的操作。select
参数:SQL语句
返回值:查询的结果集
boolean execute(String sql) 可以执行任意的sql 只做了解

四、案例

案例一、更新一条记录

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/*
    添加一条记录
 */
public class Demo02JDBC {
    public static void main(String[] args)   {
        Connection conn = null;
        Statement st = null;
        try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sql_sel",
                        "root", "tt1314521");

                st = conn.createStatement();
                //
                String strSQL = "INSERT INTO account VALUES(NULL,\"wangwu\",2500)";
                int count = st.executeUpdate(strSQL);
                System.out.println(count);
                if(count > 0){
                    System.out.println("添加成功");
                }else{
                    System.out.println("添加失败");
                }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (st == null){//避免空指针,防止Connection执行失败
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

案例二、更新一条记录

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/*
    更新一条记录
 */
public class Demo03JDBC {
    public static void main(String[] args) {
        Connection conn = null;
        Statement statement = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sql_sel",
                    "root", "tt1314521");
            statement = conn.createStatement();
            int count = statement.executeUpdate("update account set balance= 1000 where id=3");
            if(count > 0){
                System.out.println("更新成功");
            }
            else{
                System.out.println("更新失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(statement == null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
发布了287 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/l0510402015/article/details/104711358
今日推荐