执行数据库更新操作

数据库连接之后,要进行数据库操作,则要使用Statement接口完成,此接口可以使用Connection接口中提供的createStatement(方法实例化)
例如 向 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() ;
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值