仅以这篇文章作为自己知识的回顾,工作刚刚满半年。最近一直在写程序,测试程序。对于这段时间归纳为基础知识边做边学,程序bug越测越多。对于基础操作的来说数据库无疑是重点,来写写oracle的知识小结。
(1).oracle 语句的操作
1.创建表 根据旧表创建新表,create newtable as select * from oldtable where 1=2 ; (设置1=2,这个是个不成立的等式,这样可以避免拷贝数据到新表。只用表结构就可以了。)
2.创建序列,create sequence seq_table_id minvalue 1 maxvalue 99999999999999 start with 1 increment by 1 nocache;
3.varchar2() 存放的字段,例如varchar2(8),存放8个英文字符。存放中文字符:utf-8时,一个中文占三个字节;ZHS16GBK,一个中文占2个字节。所以一般可以存放4个中文。而nvarchar2(8),中英文都可放8个。在java读取长度时,转换为string后length的值,一个汉字,一个英文,标点,空格都为1.
4.oracle 分组查询,并且(distinct)去除重复的,select *,count(distinct tagid) from table group by tagid order by tagid desc,可以再查询,left join table /(select * from ***) on a.id=b.id 一类的写法,左连接或者右连接
4.1 oracle去重查询:
select distinct mid ,content,createdtime from t_mblog where wbid='"+wbid+"' order by createdtime desc
5.统计人数查询,比如(decode())男女人数: select sum(decode(sex,'男',1)) man ,sum(decode(sex,'女',1)) woman from (select * from usertable)
----------------------------更新--------------2013-6-7------------------------
6.联合查询,连接
如果table1的值有,但对应的table2中却没有与之对应的mmid,这样却查不出来table1的内容
(select mmid, count(mmid) tt from table2 group by mmid ) b on a.id = b.mmid
(select mmid, count(mmid) tt from table2 group by mmid) b on a.id = b.mmid
(select mmid, count(mmid) tt from table2 group by mmid) b on a.id = b.mmid
2.java 基础,读写数据流
public static void main(String[] args) {
doReadFile();
doWriteFile();
}
/**
* 读入数据
*/
public static void doReadFile(){
String file = "d:/write.txt";
try {
//根据文件来读取实现读取流,File
FileInputStream input = new FileInputStream(new File(file));
InputStreamReader in = new InputStreamReader(input);
BufferedReader reader = new BufferedReader(in);
String str="";
while((str=reader.readLine())!=null){
System.out.println(str);
}
reader.close();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
/**
* 往文件写入数据
*/
public static void doWriteFile(){
String file = "d:/write.txt";
File wfile = new File(file);
try {
String str="cjplife";
//后面加入true,就是保留原有内容,在后面插入新的内容
FileOutputStream os = new FileOutputStream(wfile,true);
os.write(str.getBytes());
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
2.采用jdbc查询,并且读取查询的数据,由于list,强制转换存放的是map集合,list循环跟iterator循环一样的
String sql="select * from table where status = 1";
List list = this.getJdbcTemplate().queryForList(sql);
Iterator it = list.iterator();
while(it.hasNext()){
Map rs = (Map) it.next();
String name = rs.get("nickname").toString();
String school = rs.get("school").toString();
}
//或者使用
for(int i=0;i<list.size();i++){
Map rs = (Map)list.get(i);
String name = rs.get("nickname").toString();
String school = rs.get("school").toString();
}
---------------------------------------5月22日更新---------------------------------------------------------
3,采用jdbc分页原理,oracle有个默认的rownum,这个值可作为分页使用的依据。Hibernate分页也是采用这个原理。批量更新操作是,如果是虚拟列,不允许更新,所以根据Id来先查询再进行更新。
String sql = "select * from (select a.*,rownum rn from (select * from table where status =0 order by id ) a ) where rn<=100 and rn>0"
String sql2="update ( select * from table where id in ( select id from (select a.*,rownum rr from (select * from table where status=0 order by id ) a ) where rr<=100 and rn>0 ) ) set status=1";
4.LinkedHashMap<K,V>和HashMap的使用
hashMap是无序存储数据的,根据keySet来循环时,会出现乱序读取数据,而LinkedHashMap则是按照序列来读取集合内的数据
Map<Integer,String> linkMap = new LinkedHashMap<Integer,String>();
linkMap.put(3,"cc");
linkMap.put(2,"cc");
linkMap.put(1,"cc");
//三种循环读取map元素形式
//entrySet循环
for(Entity<Integer,String> entry:linkMap.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}
//keyset循环
for(Integer key:linkMap.keySet()){
System.out.println(key+":"+linkMap.get(key));
}
//迭代器
for(Iterator it = linkMap.keySet().iterator();it.hasNext()){
Object key = it.next();
System.out.println(key+":"+linkMap.get(key));
}
//清空map集合的元素
newMap.remove(key);
//清空StringBuffer里面的内容
StringBuffer strbuf=new StringBuffer("aaa");
strbuf.append("bbbb");
strbuf.setLength(0);
------------------------------5月28日更新--------------------------
循环对HashMap里面的值,按Value的值排序
/**
* @方法 HashMap 按照value的double排序
* @author Cjp
*
*/
public class MyCompare {
public static void main(String[] args) {
Map<String, Double> dMap= new HashMap<String, Double>();
dMap.put("C", 0.7756);
dMap.put("J", 0.5025);
dMap.put("P", 0.5324);
dMap.put("W", 0.9864);
dMap.put("H", 0.8756);
System.out.println(map_Data);
//添加到list里面去
List<Map.Entry<String, Double>> list_Map = new ArrayList<Map.Entry<String, Double>>(dMap.entrySet());
System.out.println(list_Map);
Collections.sort(list_Map, new Comparator<Map.Entry<String, Double>>()
{
public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2)
{
if ((o2.getValue() - o1.getValue())>0)
return 1;
else if((o2.getValue() - o1.getValue())==0)
return 0;
else
return -1;
}
});
//循环list_map,取出里面的值
for(int i=0;i<list_Map.size();i++){
System.out.println(list_Map.get(i).toString());
Entry<String, Double> entry=list_Map.get(i);
System.out.println(entry.getKey()+"###"+entry.getValue());
}
}
}