03.JDBC数据库编程之处理DML语句
2015-04-27 23:41
344 查看
转载请标明出处:http://blog.csdn.net/u012637501
一、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.创建Connection对象conn,表示连接到MySQL数据库
Connection conn=null;
Statement stmt=null;
int i;
try{
conn=DriverManager.getConnection(url, DBusername, DBpassword);
//3.获取能够实现执行SQL语句的Statement对象
stmt=conn.createStatement();
//4.执行SQL语句,根据返回的int类型数据判断是否插入成功
i=stmt.executeUpdate("insert into test (name,age,score) values ('dongguo',18,97)");
if(i!=1)
{
System.out.println("更新数据库失败!");
}
}catch(SQLException se)
{
System.out.println("连接数据库失败");
se.printStackTrace();
}
//6.关闭所有使用的JDBC对象,释放JDBC资源
if(stmt!=null) //关闭声明
{
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(conn!=null) //关闭数据库连接
{
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
运行结果
(1)插入一条记录
stmt.executeUpdate("insert into test (name,age,score) values ('dongguo',18,97)");
(2)删除一条记录
stmt.executeUpdate("delete from test where name='JJ'");
2.JDBC应用向数据库动态插入数据
源码:通过命令行的参数作为插入的数据源,即动态更新数据库数据
import java.sql.*;
/*MySQL数据库编程
* 实例(3):JDBC处理DML语句.通过命令行输入需要插入到数据库中的数据*/
public class JDBC_DML2 {
public static void main(String[] args) {
if(args.length!=3) //输入不正确,非正常退出
{
System.out.println( "Parament Error,Please Input Again!");
System.exit(-1);
}
String nameParam=args[0]; //获取命令行第一个参数
int ageParam=Integer.parseInt(args[1]); //获取命令行第二个参数,并转换为整型
int scoreParam=Integer.parseInt(args[2]);//获取命令行第三个参数,并转换为整型
//0.连接数据库相关参数
String url="jdbc:mysql://localhost:3306/jdbc_test_db"; //数据库URL(资源定位唯一标识符)
String DBusername="root"; //数据库用户名
String DBpasswd="111111"; //数据库密码
//1.加载数据库驱动,将Driver注册到DriverManager中
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
//2.通过数据库URL连接到数据库
Connection conn=null;
Statement stmt=null;
try{
conn=DriverManager.getConnection(url, DBusername, DBpasswd);
//3.获取Statement对象
stmt=conn.createStatement();
//4.获取命令参数,并调用Statement对象的executeUpdate更新数据库
String sql="insert into test(name,age,score) values('"+nameParam+"',"+ageParam+","+scoreParam+")";
System.out.println(sql);//调试
stmt.executeUpdate(sql);
}catch(SQLException e){
e.printStackTrace();
}
//5.释放JDBC资源
if(stmt!=null) //关闭声明
{
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(conn!=null) //关闭连接
{
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
(1)设置命令行参数
右击工程->Run as->Open Run Dialog->Main Class选择"JDBC_DML2"
(2)运行结果
(3)遇到的错误与调试
输入参数运行Java应用后,出现SQLException异常。我们可以通过打印输入的插入sql语句,发现程序没有获取到命令行传入的参数,另外,vaules拼写错误,找到原因。
修改:sql="insert into test(name,age,score) values('"+nameParam+"',"+ageParam+","+scoreParam+")";即可
一、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())
void | addBatch(String sql) Adds the given SQL command to the current list of commands for this Statementobject. |
void | clearBatch() Empties this Statementobject's current list of SQL commands. |
void | close() Releases this Statementobject's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. |
boolean | execute(String sql) Executes the given SQL statement, which may return multiple results. |
int[] | executeBatch() Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts. |
ResultSet | executeQuery(String sql) Executes the given SQL statement, which returns a single ResultSetobject. |
int | executeUpdate(String sql) Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETEstatement or an SQL statement that returns nothing, such as an SQL DDL statement. |
int | 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 retrieval. |
boolean | isClosed() Retrieves whether this Statementobject has been closed. |
boolean | isCloseOnCompletion() Returns a value indicating whether this Statementwill be closed when all its dependent result sets are closed. |
void | setMaxRows(int max) Sets the limit for the maximum number of rows that any ResultSetobject generated by this Statementobject can contain to the given number. |
void | setQueryTimeout(int seconds) Sets the number of seconds the driver will wait for a Statementobject to execute to the given number of seconds. |
二、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.创建Connection对象conn,表示连接到MySQL数据库
Connection conn=null;
Statement stmt=null;
int i;
try{
conn=DriverManager.getConnection(url, DBusername, DBpassword);
//3.获取能够实现执行SQL语句的Statement对象
stmt=conn.createStatement();
//4.执行SQL语句,根据返回的int类型数据判断是否插入成功
i=stmt.executeUpdate("insert into test (name,age,score) values ('dongguo',18,97)");
if(i!=1)
{
System.out.println("更新数据库失败!");
}
}catch(SQLException se)
{
System.out.println("连接数据库失败");
se.printStackTrace();
}
//6.关闭所有使用的JDBC对象,释放JDBC资源
if(stmt!=null) //关闭声明
{
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(conn!=null) //关闭数据库连接
{
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
运行结果
(1)插入一条记录
stmt.executeUpdate("insert into test (name,age,score) values ('dongguo',18,97)");
(2)删除一条记录
stmt.executeUpdate("delete from test where name='JJ'");
2.JDBC应用向数据库动态插入数据
源码:通过命令行的参数作为插入的数据源,即动态更新数据库数据
import java.sql.*;
/*MySQL数据库编程
* 实例(3):JDBC处理DML语句.通过命令行输入需要插入到数据库中的数据*/
public class JDBC_DML2 {
public static void main(String[] args) {
if(args.length!=3) //输入不正确,非正常退出
{
System.out.println( "Parament Error,Please Input Again!");
System.exit(-1);
}
String nameParam=args[0]; //获取命令行第一个参数
int ageParam=Integer.parseInt(args[1]); //获取命令行第二个参数,并转换为整型
int scoreParam=Integer.parseInt(args[2]);//获取命令行第三个参数,并转换为整型
//0.连接数据库相关参数
String url="jdbc:mysql://localhost:3306/jdbc_test_db"; //数据库URL(资源定位唯一标识符)
String DBusername="root"; //数据库用户名
String DBpasswd="111111"; //数据库密码
//1.加载数据库驱动,将Driver注册到DriverManager中
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
//2.通过数据库URL连接到数据库
Connection conn=null;
Statement stmt=null;
try{
conn=DriverManager.getConnection(url, DBusername, DBpasswd);
//3.获取Statement对象
stmt=conn.createStatement();
//4.获取命令参数,并调用Statement对象的executeUpdate更新数据库
String sql="insert into test(name,age,score) values('"+nameParam+"',"+ageParam+","+scoreParam+")";
System.out.println(sql);//调试
stmt.executeUpdate(sql);
}catch(SQLException e){
e.printStackTrace();
}
//5.释放JDBC资源
if(stmt!=null) //关闭声明
{
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(conn!=null) //关闭连接
{
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
(1)设置命令行参数
右击工程->Run as->Open Run Dialog->Main Class选择"JDBC_DML2"
(2)运行结果
(3)遇到的错误与调试
输入参数运行Java应用后,出现SQLException异常。我们可以通过打印输入的插入sql语句,发现程序没有获取到命令行传入的参数,另外,vaules拼写错误,找到原因。
修改:sql="insert into test(name,age,score) values('"+nameParam+"',"+ageParam+","+scoreParam+")";即可
相关文章推荐
- JDBC学习之二: JDBC处理DML语句3(PreparedStatement的使用 重点)
- JDBC学习之二: JDBC处理DML语句2(Statement的使用 重点)
- JDBC学习之二:JDBC处理DML语句1(重点)
- JAVA利用JDBC对数据库的操作和JDBC编程之事务处理
- c# 数据库编程(通过SqlCommand 执行DML语句)
- JDBC与JAVA数据库编程
- JDBC:数据库分页语句
- 运用JDBC进行数据库编程
- [Spring学习笔记 7 ] Spring中的数据库支持 RowMapper,JdbcDaoSupport 和 事务处理Transaction
- [Java]JDBC数据库编程
- JDBC-数据库的更新操作编程(三)
- 关于Oracle处理DDL和DML语句的事务管理
- 【笔记】Oracle 数据库编程 - 基本语句总结
- 批量处理JDBC语句提高处理速度
- JAVAWEB开发之数据库简介、MySQL的安装与卸载(处理中文乱码)、以及MySQL语言(DDL、DML、DCL、DQL)的详解和使用
- Mysql数据库学习(一):数据库基本概念、关系型数据库、Mysql数据库安装配置、DDL/DCL/DML语句
- JDBC与JAVA数据库编程
- JDBC数据库编程
- JDBC与JAVA数据库编程
- 批量处理JDBC语句提高处理速度