数据库MySQL

数据库的建立:

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();//游标下移
                }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值