JDBC各个类详解_Statement
3. Statement :执行sql对象
1.执行sql
1.boolean execute(String sql) :可以执行任意的sql 了解
2.int executeUpdate(String sql):执行DML(insert,update,delete)语句、DDL(create,alter,drop)语句
返回值:影响的行数,可以这个通过这个影响的行数判断DML语句执行成功 返回值>0的执行成功,反之,则失效
3.ResultSet executeQuery(String sql) :执行DQL(select)语句
2.练习:
1.accont表 添加一条记录
package CN.XueQiang.JDBC; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** * accont表 添加一条记录 insert 语句 */ public class JDBCDemo2 { public static void main(String[] args) { Connection conn = null; Statement stam = null; try { //1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2.定义sql String sql = "insert into accont value (null,'穆宁雪',4000);"; //3.获取Connection对象 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/win", "root", "********"); //4.获取执行方法sql对象Statement stam = conn.createStatement(); //5.执行sql int count = stam.executeUpdate(sql); //6.处理结果 System.out.println(count); if (count > 0){ System.out.println("添加成功"); }else{ System.out.println("添加失败"); } } catch (ClassNotFoundException e) { throw new RuntimeException(e); } catch (SQLException e) { throw new RuntimeException(e); }finally { // stam.close(); //7.释放资源 //避免空指针 if (stam!=null){ try { stam.close(); } catch (SQLException e) { throw new RuntimeException(e); } } if (conn!=null){ try { conn.close(); } catch (SQLException e) { throw new RuntimeException(e); } } } } }
最后结果
accont:
标签:语句,insert,JDBC,Statement,sql,stam From: https://www.cnblogs.com/x3449/p/16816497.html