数据库的建立:
CREATE DATABASE database-name
表格的建立:
create table tabname(coll type1 [not null] [primary key],col2 type2 [not null],..)
数据库的增删查改操作:
insert into student (name,sex,age)value('王五',0,10)
delete from student where name ='张三'
update student set sex =0,age =22 where id =2
在代码中实现对数据库的操作
String driver = "com.mysql.jdbc.Driver";
String url= "jdbc:mysql://localhost:3306/CLAZZ";
String user ="root";
String password = "123456";
try {
//加载驱动
Class.forName(driver);
//与数据库建立连接
Connection conn = DriverManager.getConnection(url,user,password);
if(!conn.isClosed()){
//建立对数据库进行操作的类。
state = conn.createStatement();
String s = "create table if not exists user(id int(3) auto_increment not null primary key,password varchar(30) not null,name varchar(30) not null)";
state.execute(s);
}else{
System.out.println("请打开数据库");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
参考代码:
Statement state = conn.createStatement();
// String sinser =" insert into student (name,sex,age)value('王五',0,10)";
// String delete ="delete from student where name ='张三'";
// String update =" update student set sex =0,age =22 where id =2";
//state.execute(sql);//增减改时的用法
String select = "select *from student";
ResultSet set= state.executeQuery(select);//查询时的用法
set.first();//将游标调到第一的位置
//判断游标是否是在最后一位的后边
while(!set.isAfterLast()){
System.out.println(set.getString("age"));
set.next();//游标下移
}