03.JDBC数据库编程之处理DML语句(一)

2015-07-24 07:20:26 · 作者: · 浏览: 6
  1. void
  1. addBatch(String sql) Adds the given SQL command to the current list of commands for this Statement object.
  1. void
  1. clearBatch() Empties this Statement object's current list of SQL commands.
  1. void
  1. close() Releases this Statement object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.
  1. boolean
  1. execute(String sql) Executes the given SQL statement, which may return multiple results.
  1. int[]
  1. executeBatch() Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts.
  1. ResultSet
  1. executeQuery(String sql) Executes the given SQL statement, which returns a single ResultSet object.
  1. int
  1. executeUpdate(String sql) Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.
  1. int
  1. executeUpdate(String sql, String[] columnNames) Executes the given SQL statement and signals the driver that the auto-generated keys indicated in the given array should be made available for retrieva l.
  1. boolean
  1. isClosed() Retrieves whether this Statement object has been closed.
  1. boolean
  1. isCloseOnCompletion() Returns a value indicating whether this Statement will be closed when all its dependent result sets are closed.
  1. void
  1. setMaxRows(int max) Sets the limit for the maximum number of rows that any ResultSet object generated by this Statementobject can contain to the given number.
  1. void
  1. setQueryTimeout(int seconds) Sets the number of seconds the driver will wait for a Statement object to execute to the given number of seconds.
一、DML与Statement接口
1.DML(Data manipulation language)
数据库操作语句,用于添加、删除、更新和查询数据库纪录,并检查数据库的完整性。常用的语句关键字主要包括insert、delete、update等。
(1)添加/修改/删除表数据
增加一行数据:(思想:往哪张表添加?给哪几行添加值?分别是什么值?)
? ?insert ?into ?表名 ?(列1,列2,...,列n) ? values ?(值1,值2...,值n); ?
修改某行的列数据:(思想:改哪张表?你需要改几列的值?分别改为什么值?在哪些行生效?)
update 表名 set 列1=新值1, 列2=新值2,where expr;
删除行:(思想:你要删除哪张表的数据?你要删掉哪些行?)
delete from 表名 where expr;
查看表数据:
select * from [表名];


(2)修改字段及属性
修改表(表中的字段类型、字段名、添加字段、删除字段、修改表名)
①修改表中字段属性(不能修改字段名)
alter table [表名] modify [字段名] [字段类型] [约束条件] [fisrt|after 列名];
②修改表中字段名及属性
alter table [表名] change [源字段名] [修改后的字段名] [字段类型] [约束条件] [fisrt|after 列名];
③增加表字段
?alter table [表名] add [字段名] [字段类型] [约束条件] [first|after 列名];
④删除表字段
alter table [表名] drop [字段名];
注意:[first|after 列名],用于修改字段的排序,其中,after将新增的字段添加在某一字段后;first表示将新建的字段放在该表第一列。
修改表名
命令:alter table [表名] rename to [新表名];


2.Statement接口
用于执行静态的SQL语句并获得返回结果的接口,通过Connection中的createStatement方法得到的
即,Statement stmt=conn.createStatement())
参考:http://docs.oracle.com/javase/8/docs/api/index.html
二、JDBC数据库应用源码实战 1.源码实现 任务:向连接的数据库中静态插入一条数据('dongguo',18,97).
import java.sql.*;


/*MySQL数据库编程
* 实例(2):JDBC处理DML语句*/


public class JDBC_DML {
public static void main(String[] args) {
//0.数据库URL、数据库账户名称与密码
String url = "jdbc:mysql://localhost/jdbc_test_db";
String DBusername="root";
String DBpassword="896013";


//1.加载数据库驱动程序到Java虚拟机


try{
Class.forName("com.mysql.jdbc.Driver"); //Driver为MySQL驱动类
}catch(ClassNotFoundException e)
{
System.out.println("找不到数据库驱动程序类,加载驱动失败!");
e.printStackTrace(); //将异常保存到log日志中
}


//2.