发表于: 2019-10-11 18:37:13

1 1014


今天做的事

完成了入学的所有操作

巩固一下基础

再次熟悉主要的mysql语句用法


启动mysql服务器 net start mysql 

#关闭 net stop mysql  

 #进入 mysql -h 主机地址 -u 用户名 -p 用户密码 #退出exit

显示当前mysql的version的各种信息。 

 #修改密码:首先在DOS 下进入mysql安装路径的bin目录下,然后键入以下命令: mysqladmin -uroot -p123 password 456

 #增加用户 #格式:grant 

#显示数据库 show databases;  

#判断是否存在数据库wpj1105,有的话先删除

drop database if exists wpj1105; 

 #创建数据库create database wpj1105;

 #删除数据库drop database wpj1105; 

 #使用该数据库use wpj1105; 

 #显示数据库中的表 show tables; 

 #先判断表是否存在,存在先删除drop table if exists student; 

 #创建表create table student( id int auto_increment primary key, name varchar(50), sex varchar(20), date varchar(50), content varchar(100) )default charset=utf8; 

 #删除表drop table student; 

 #查看表的结构 describe student;  #可以简写为desc student;

1、说明:创建数据库CREATE DATABASE database-name

2、说明:删除数据库drop database dbname

3、说明:备份sql server--- 创建 备份数据的 deviceUSE masterEXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'--- 开始 备份BACKUP DATABASE pubs TO testBack

4、说明:创建新表create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) 根据已有的表创建新表: A:create table tab_new like tab_old (使用旧表创建新表) B:create table tab_new as select col1,col2… from tab_old definition only

5、说明:删除新表drop table tabname

6、说明:增加一个列Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。

7、说明:添加主键: Alter table tabname add primary key(col) 说明:删除主键: Alter table tabname drop primary key(col)

8、说明:创建索引:create [unique] index idxname on tabname(col….) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。

9、说明:创建视图:create view viewname as select statement 删除视图:drop view viewname

10、说明:几个简单的基本的sql语句 选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 where 范围 查找:select * from table1 where field1 like%value1%---like的语法很精妙,查资料!排序:select * from table1 order by field1,field2 [desc]总数:select count as totalcount from table1 求和:select sum(field1) as sumvalue from table1 平均:select avg(field1) as avgvalue from table1 最大:select max(field1) as maxvalue from table1 最小:select min(field1) as minvalue from table111、说明:几个高级查询运算词 A: UNION 运算符UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALLUNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。 B: EXCEPT 运算符EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALLEXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。 C: INTERSECT 运算符INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALLINTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。 注:使用运算词的几个查询结果行必须是一致的。

11、说明:使用外连接 A、leftouterjoin: 左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。 SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c B:rightouterjoin: 右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。 C:full/crossouterjoin: 全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。

12、分组:Group by:  一张表,一旦分组完成后,查询后只能得到组相关的信息。 组相关的信息:(统计信息) count,sum,max,min,avg  分组的标准)    在SQLServer中分组时:不能以text,ntext,image类型的字段作为分组依据 在selecte统计函数中的字段,不能和普通的字段放在一起;13、对数据库进行操作: 分离数据库: sp_detach_db; 附加数据库:sp_attach_db 后接表明,附加需要完整的路径名14.如何修改数据库的名称: sp_renamedb 'old_name', 'new_name'


JdbcTemplate控制数据库


首先导入jar包

在师兄的指导下知道了一个专门搜索下载的网站

https://mvnrepository.com

导入了两个jar包分别是springjdbc4.3.0 和spring tx4.3.0


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.0.RELEASE</version>
</dependency>

然后完成了本地仓库配置

用JDBC TEMPLATE控制数据库代码如下


package jdbc;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
public class JDBCTMP {

private int cateId;

private String cateName;

public int getCateId() {
return cateId;
}

public void setCateId(int cateId) {
this.cateId = cateId;
}

public String getCateName() {
return cateName;
}

public void setCateName(String cateName) {
this.cateName = cateName;
}

@Override
       public String toString() {
return "id="+cateId+" name="+cateName;
}
}

结果

测试

ContextConfiguration(locations = "classpath:applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class testCategoryDao {
    @Autowired
    private CategoryDao categoryDao;
 
    @Test
    public void testAdd() {
        Category category = new Category();
        category.setCateId(4);
        category.setCateName("母婴");
 
        int result = categoryDao.add(category);
        System.out.println(result);
    }
 
    @Test
    public void testUpdate() {
        Category category = new Category();
        category.setCateId(4);
        category.setCateName("男装");
 
        int result = categoryDao.update(category);
        System.out.println(result);
    }
 
 
    @Test
    public void testGetById() {
        int id = 4;
        Category category = categoryDao.getById(id);
        System.out.println(category.toString());
    }
 
    @Test
    public void testGetAll() {
        List<Category> categories = categoryDao.getAll();
        for (Category item : categories) {
            System.out.println(item);
        }
    }
 
    @Test
    public void testCount() {
        int count = categoryDao.count();
        System.out.println(count);
    }
 
    @Test
    public void testDelete() {
        int id = 4;
        int result = categoryDao.delete(id);
        System.out.println(result);
    }
}

遇到的问题

安装jar包时操作失误最后师兄指导下完成

收获

对整体框架有了进一步认识

明天的计划

进一步掌握jdbc控住数据库



返回列表 返回列表
评论

    分享到