执行数据库更新操作
2015-12-02 22:22
459 查看
数据库连接之后,要进行数据库操作,则要使用Statement接口完成,此接口可以使用Connection接口中提供的createStatement(方法实例化)
例如 向 mytable 表中增加 数据 并执行数据库更新操作
数据库表中数据更新
例如 向 mytable 表中增加 数据 并执行数据库更新操作
import java.sql.Connection ; import java.sql.DriverManager ; import java.sql.SQLException ; import java.sql.Statement ; public class ConnectionDemo01 { //定义MySql数据库驱动程序 public static final String driver = "org.gjt.mm.mysql.Driver" ; //定义Mysql数据库的连接地址 public static final String url = "jdbc:mysql://localhost:3306/spiderman" ; //定义数据库名称 public static final String user = "root" ; //定义数据库连接密码 public static final String pass = "" ; public static void main(String[] args) { //加载数据库驱动程序 try { Class.forName(driver) ; } catch (ClassNotFoundException e) { e.printStackTrace() ; return ; } // 加载数据库连接 Connection conn = null ; try { conn = DriverManager.getConnection(url,user,pass) ; } catch (SQLException e) { e.printStackTrace() ; return ; } //执行数据库操作 Statement sta = null ; int age = 1000 ; //声明年龄属性 String name = "Wangbadan" ; String password = "Elex" ; String sex = "男" ; String birthday = "1992-01-01" ; String sql = "Insert into mytable(name,password,age,sex,birthday)values"+ "('"+name+"','"+password+"',"+age+",'"+sex+"','"+birthday+"')" ; try { sta = conn.createStatement() ; //实例化Statement对象 sta.executeUpdate(sql) ; //执行数据库更新 } catch (SQLException e) { e.printStackTrace() ; return ; } finally { if(sta!=null) { try { sta.close() ; //数据库操作 关闭 } catch (SQLException e) { e.printStackTrace() ; } } if(conn!=null) { 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 ConnectionDemo01 { //定义MySql数据库驱动程序 public static final String driver = "org.gjt.mm.mysql.Driver" ; //定义Mysql数据库的连接地址 public static final String url = "jdbc:mysql://localhost:3306/spiderman" ; //定义数据库名称 public static final String user = "root" ; //定义数据库连接密码 public static final String pass = "" ; public static void main(String[] args) { //加载数据库驱动程序 try { Class.forName(driver) ; } catch (ClassNotFoundException e) { e.printStackTrace() ; return ; } // 加载数据库连接 Connection conn = null ; try { conn = DriverManager.getConnection(url,user,pass) ; } catch (SQLException e) { e.printStackTrace() ; return ; } //执行数据库操作 Statement sta = null ; int age = 10000 ; //声明年龄属性 String name = "传奇" ; String password = "Elex" ; String sex = "女" ; String birthday = "1999-01-01" ; int id = 9 ; String sql = "update mytable set name = '"+name+"',password = '"+password+"',"+ "age = "+age+",sex = '"+sex+"',birthday = '"+birthday+"'where id ="+id ; try { sta = conn.createStatement() ; //实例化Statement对象 sta.executeUpdate(sql) ; //执行数据库更新 } catch (SQLException e) { e.printStackTrace() ; return ; } finally { if(sta!=null) { try { sta.close() ; //数据库操作 关闭 } catch (SQLException e) { e.printStackTrace() ; } } if(conn!=null) { try { conn.close() ;//数据库连接 关闭 } catch (SQLException e) { e.printStackTrace() ; } } } } }
相关文章推荐
- Redis 代理服务Twemproxy(redis分布式中间件)
- 数据库使用实例
- 数据库连接操作
- 使用homebrew 安装 mongodb 出现问题
- Redis: Jedis 源代码剖析1-链接建立和收发命令
- mysql笔记1
- Mysql出现插入中文???的问题
- MySQL中的find_in_set(A, B)函数,判断字符串A是否存在B中
- SQL实现将一个表的数据插入到另外一个表的代码
- oracle触发器调试
- SparkSQL样例
- sql2014 新建用户并登陆
- redis源码阅读(一):开始
- sqlite问题
- Oracle exp imp 常见问题解决
- SQL Server系列(7) -- 游标
- Windows UWP应用使用本地Sqlite和远程Sql(一)
- xml 文件介绍
- win8 64位使用plsql developer连接oracle数据库问题
- MySQL命令大全