JDBC(四):Statement
Statement作用执行sql1. 执行dml、ddlint excuteUpdate(sql)1dml输出受影响行数为正执行成功为负执行失败2ddl可能输出0无异常执行成功有异常执行失败2. 执行dqlResultSet excuteQuery(sql)public class jdbc04Statement { Test public void testDml() throws SQLException { //0. 导入驱动mysql的驱动jar //1. 注册驱动指定要用哪个jar包 //Class.forName(com.mysql.jdbc.Driver); //2. 获取连接 String url jdbc:mysql:///数据库名?useSSLfalse; String username root; String password 数据库密码; Connection conn DriverManager.getConnection(url, username, password); //3. 定义sql语句 String sql1 update account set money 1000 where id 1; //4. 获取执行sql对象 Statement stmt conn.createStatement(); //5. 执行sql语句 int count1 stmt.executeUpdate(sql1); //6. 处理执行结果 //判断是否执行成功count1为正执行成功count1为负执行失败 if (count1 0) { System.out.println(执行成功受影响行数为count1); }else { System.out.println(执行失败); } //7. 释放 stmt.close(); conn.close(); } Test public void testDdl() throws SQLException { //0. 导入驱动mysql的驱动jar //1. 注册驱动指定要用哪个jar包 //Class.forName(com.mysql.jdbc.Driver); //2. 获取连接 String url jdbc:mysql:///数据库名?useSSLfalse; String username root; String password 数据库密码; Connection conn DriverManager.getConnection(url, username, password); //3. 定义sql语句 String sql1 drop database db02; //4. 获取执行sql对象 Statement stmt conn.createStatement(); //5. 执行sql语句 int count1 stmt.executeUpdate(sql1); //6. 处理执行结果 //可能输出0无异常执行成功有异常执行失败 System.out.println(count1); //7. 释放 stmt.close(); conn.close(); } }
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2623798.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!