shendongshia
8/23/2018 - 9:33 AM

JDBC

package com.company;

public class StringEquals
{
    public static void main(String[] args)
    {
        String str1 = "java";
        String str2 = "java";
        String str3 = new String("java");
        String str4 = new String("java");

        System.out.println(str1 == str2 + "\n");
        System.out.println("str1.equals(str2) : " + str1.equals(str2) + "\n");

        System.out.println(str3 == str4 + "\n");
        System.out.println("str3.equals(str4) : " + str3.equals(str4) + "\n");

        System.out.println(str1 == str3 + "\n");
        System.out.println("str1.equals(str3) : " + str1.equals(str3) + "\n");

        //str1和str2是放在字符串常量池中,也就栈内存中的,因此str1和str2指向的是同一个字符串"java",因此它们地址和内容都是一样的
        //str3和str4是放在堆内存中的,它俩具有相同的内容,但是不具有相同的内存地址
        //当然str1和str3更不是相同的内存地址,只是相同的内容而已
    }
}
package com.company;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializeUtil
{
    public static byte[] serialize(Object object)
    {

        try
        {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(object);
            byte[] bytes = byteArrayOutputStream.toByteArray();
            return bytes;
        }
        catch (Exception e)
        {
            System.out.println("seriralize" + e);
        }
        return null;
    }

    public static Object unSerialize(byte[] bytes)
    {
        try
        {
            ByteArrayInputStream byteArrayOutputStream = new ByteArrayInputStream(bytes);
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayOutputStream);
            return objectInputStream.readObject();
        }
        catch (Exception e)
        {
            System.out.println("unseriralize" + e);
        }
        return null;
    }
}
 //JDBC如何实现回滚~
 1 public void transaction() throws SQLException {
 2         Connection conn = null;
 3         PreparedStatement st = null;
 4         ResultSet rs = null;
 5         Savepoint sp = null;   //代表回滚点对象
 6         try{
 7             conn = JdbcUtils.getConnection();
 8             conn.setAutoCommit(false);  //开启事务,相当于start transaction命令
 9             
10             String sql1 = "update account set money=money-100 where name='a'";
11             st = conn.prepareStatement(sql1);
12             st.executeUpdate();
13             
14             sp = conn.setSavepoint();   //在此处设置回滚点
15             
16             int x = 1/0;      //在此处模拟事务处理过程中出错
17             
18             String sql2 = "update account set money=money+100 where name='b'";
19             st = conn.prepareStatement(sql2);
20             st.executeUpdate();
21             
22             conn.commit();   //提交事务
23         }catch (Exception e) {
24             conn.rollback(sp);    //回滚到指定的回滚点处
25             conn.commit();        //回滚之后再次提交事务保证回滚点之前的SQL能被执行
26             
27         }finally{
28             JdbcUtils.release(conn, st, rs);
29         }
30     }
package com.company;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.ByteArrayOutputStream;
import java.util.*;

public class Main
{

    private JedisPool jedisPool;

    public static void main(String[] args)
    {
        // write your code here
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.flushAll();
        System.out.println("RedisServiceStart:" + jedis.ping());
        Person personXia = new Person(100, "xiazhendong");
        Person personJin = new Person(25, "jinpengcheng");
        jedis.set(String.valueOf(personXia.getId()).getBytes(), SerializeUtil.serialize(personXia));
        jedis.set(String.valueOf(personJin.getId()).getBytes(), SerializeUtil.serialize(personJin));

        System.out.println("_____________________________________________");
        Set<String> keysSet = jedis.keys("*");
        Iterator<String> iterator = keysSet.iterator();
        while (iterator.hasNext())
        {
            String key = iterator.next();
            System.out.println("jedis key type is " + jedis.type(key));
            byte[] person = jedis.get(key.getBytes());
            Person personResultl = (Person) SerializeUtil.unSerialize(person);
            System.out.println("Person " + personResultl.getName() +"is :\n"+ personResultl);
        }


        jedis.lpush("siteList","Runoob");
        jedis.lpush("siteList","Google");
        jedis.lpush("siteList","Tabao");
        List<String> siteList = jedis.lrange("siteList",0,jedis.llen("siteList"));
        jedis.del("siteList");

        jedis.set("siteTaobao","taobao");
        jedis.set("siteJD","JD");
        jedis.set("siteTmall","tmall");

        jedis.hset("peoppe","name","mayun");
        String name =jedis.hget("people","name");
        List<String> mget = jedis.mget("siteTaobao","siteJD","siteTmall");


        String[] stringArray= new String[4];
        stringArray[0] = "xia";
        stringArray[1] = "zhen";
        stringArray[2] = "dong";
        stringArray[3] = "caca";
        jedis.mset(stringArray);
        String[] keyArray = new String[2];
        keyArray[0]="xia";
        keyArray[1]="dong";
        List<String> stringList = jedis.mget(keyArray);

        Set<String> keys = jedis.keys("*");
        Iterator<String> it = keys.iterator();

        while(it.hasNext())
        {
            String key = it.next();
            System.out.println("--------------"+key+"--------------\n");
            System.out.println("jedis key ttl is "+jedis.ttl(key));
            System.out.println("jedis key persist is "+jedis.persist(key));
            System.out.println("jedis key type is "+jedis.type(key));
            if(jedis.type(key).equals("hash"))
            {
                System.out.println("jedis value hash is "+jedis.hget(key,"name"));
            }
            else if(jedis.type(key).equals("list"))
            {
                System.out.println("jedis value list is "+jedis.lrange(key,0,jedis.llen(key)));
            }
            else if(jedis.type(key).equals("string"))
            {
                System.out.println("jedis value string is "+jedis.get(key));
            }
            System.out.println("jedis key delete is "+jedis.del(key));
            System.out.println("jedis key exist is "+jedis.exists(key));
            System.out.println("--------------"+key+"--------------\n");
        }
    }
}
package com.company;

import java.io.Serializable;

public class Person implements Serializable
{
    private int id;
    private String name;

    public Person(int id,String name)
    {
        this.id = id;
        this.name = name;
    }

    public int getId()
    {
        return this.id;
    }

    public String getName()
    {
        return this.name;
    }

    @Override
    public String toString()
    {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}