【Java】执行数据库的修改操作

mport java.sql.*;

//执行数据库的修改操作

public class UpdateData {
 //定义MySQL的数据库驱动程序
 public static final String DBDRIVER="org.gjt.mm.mysql.Driver";
 //定义MySQL数据库的连接地址,3306为mysql数据库的端口号,user为数据库名称
 public static final String DBURL="jdbc:mysql://localhost:3306/user";
 //MySQL数据库的连接用户名
 public static final String DBUSER="root";
 //mysql数据库的连接密码
 public static final String DBPASS="1";
 
 public static void main(String[] args)throws Exception {
  // TODO Auto-generated method stub
  Connection conn=null;      //数据库连接
  Statement stmt=null;       //数据库操作
  int id=9;
  String name="原鸽科技";   
  String password="yuange";
  int age=56;
  String sex="男";
  String birthday="2016-06-01";


  String sql="UPDATE user SET name='"+name+"',password='"+password+"'," +
    "age='"+age+"',sex='"+sex+"',birthday='"+birthday+"'WHERE id="+id;
    
  Class.forName(DBDRIVER);    //加载驱动程序
  //连接数据库时,要写上连接数据库的用户名和密码
  conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
  stmt=conn.createStatement();   //实例化Statement对象
  stmt.executeUpdate(sql);    //执行数据库更新操作
  stmt.close();     //操作关闭
  conn.close();     //数据库关闭
  
 }

}

【Java】执行数据库的修改操作

posted @ 2014-12-24 10:20  宋发元  阅读(336)  评论(0编辑  收藏  举报