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();