数据库MySQL操作

1.安装mysql,Front中SQL编辑器中使用sql语句

CREATE DATABASE STUDENTCLASS

CREATE TABLE student(id int(11) not null primary key auto_increment,name varchar(30) not null,sex int(1),age int(3))

CREATE TABLE IF NOT EXISTS user(id int(11) not null primary key auto_increment,name varchar(30) BINARY not null,password varchar(30) BINARY not null)

insert into student(name,sex,age) values('张三',0,10)

update student set age=21 where age=20

select name,age from student where age>20

delete from student where id=1

MySQL忽略大小写,创建表的时候在NOT NULL前加BINARY则区分大小写
IF NOT EXISTS 如果不存在则创建表
主键primary key(一个表中唯一标识的一条数据,不能重复)
外键(一个表中存在另一个表中的主键)

2.eclipse中,先导包,加载驱动,连接数据库,操作语句(详见代码)
注意ResultSet的方法,增删改execute,查executeQuery
注意name=’”+name+”’

3.PreparedStatement 预编译,速度快,还可避免1=’1’的问题,一般用PreparedStatement

PreparedStatement pre = conn.prepareStatement("select * from user where name=? and password=?");
    pre.setString(1, name);
    pre.setString(2, password);
    ResultSet set=pre.executeQuery();

4.注意JAVA二十三种设计模式,其中MVC设计模式,model,view,control。

5.事务(一组不可拆分的操作) 不进行任何操作的话,数据库连接默认为每一条语句都是一个事务。

            Statement sta = conn.createStatement();

//          conn.setAutoCommit(false);  //首先设置connection不自动提交
//          sta.execute(s4);
//          sta.execute(s5);
//          sta.execute(s6);
//          conn.commit(); 

            conn.setAutoCommit(false);
            sta.addBatch(s4);//将多条语句将给定的 SQL 命令添加到此 Statement 对象的当
            //前命令列表中。通过调用方法 executeBatch 可以批量执行此列表中的命令。 
            sta.addBatch(s5);
            sta.addBatch(s6);
            sta.executeBatch();
            conn.commit();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值