少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

#

主键与唯一索引的区别:
主键是一种约束,唯一索引是一种索引,两者在本质上是不同的。

主键创建后一定包含一个唯一性索引,唯一性索引并不一定就是主键。

唯一性索引列允许空值,而主键列不允许为空值。

主键列在创建时,已经默认为空值 + 唯一索引了。

主键可以被其他表引用为外键,而唯一索引不能。

一个表最多只能创建一个主键,但可以创建多个唯一索引。

主键更适合那些不容易更改的唯一标识,如自动递增列、身份证号等。


很简单,主键约束不能重复,不可为空(NULL)。
唯一性约束,不能重复,可为空(NULL),但是只能由一个NULL。
posted @ 2012-09-04 22:56 abin 阅读(444) | 评论 (0)编辑 收藏

package com.abin.lee.collection.pool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ConnectionPool {
 private List<Connection> pool;
 private static final String Url = "jdbc:oracle:thin:@localhost:1521:XE";
 private static final String USERNAME = "abing";
 private static final String PASSWORD = "abing";
 private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
 private int poolSize = 10;
 private static volatile ConnectionPool instance = null;

 private ConnectionPool() {
  init();
 }

 public void init() {
  pool = new ArrayList<Connection>(poolSize);
  addConnection();
 }

 public synchronized void release(Connection conn) {
  pool.add(conn);
 }

 public synchronized void closePool() {
  for (int i = 0; i < pool.size(); i++) {
   try {
    ((Connection) pool.get(i)).close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
   pool.remove(i);
  }
 }

 public static ConnectionPool getInstance() {
  if (null == instance) {
   synchronized (ConnectionPool.class) {
    if (null == instance) {
     return new ConnectionPool();
    }
   }
  }
  return instance;
 }

 public synchronized Connection getConnection() {
  if (pool.size() > 0) {
   Connection conn = pool.get(0);
   if(conn!=null){
    System.out.println("Connection Pool Create Success");
   }
   pool.remove(conn);
   return conn;
  }else{
   return null;
  }
 }

 public void addConnection() {
  Connection conn = null;
  for (int i = 0; i < poolSize; i++) {
   try {
    Class.forName(DRIVER);
    conn = DriverManager.getConnection(Url, USERNAME, PASSWORD);
    pool.add(conn);
   } catch (Exception e) {
    e.printStackTrace();
   }

  }
 }

}







测试代码:

package com.abin.lee.collection.pool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.abin.lee.collection.threadlocal.UserBean;

public class TestConnectionPool {

 public static List<UserBean> findAll(){
  Connection conn=null;
  PreparedStatement ps=null;
  ResultSet rs=null;
  String sql="select * from userbean order by id desc";
  List<UserBean> list=new ArrayList<UserBean>();
  UserBean user=null;
  try {
   conn=ConnectionPool.getInstance().getConnection();
   ps=conn.prepareStatement(sql);
   rs=ps.executeQuery();
   while (rs.next()&&rs!=null) {
    user=new UserBean();
    user.setId(rs.getString("id"));
    user.setUsername(rs.getString("username"));
    user.setPassword(rs.getString("password"));
    list.add(user);
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return list;
 }
 public static void main(String[] args) {
  List<UserBean> list=findAll();
  for(UserBean lst:list){
   System.out.println("id="+lst.getId());
   System.out.println("username="+lst.getUsername());
   System.out.println("id="+lst.getPassword());
  }
 }


}

posted @ 2012-09-04 00:20 abin 阅读(441) | 评论 (0)编辑 收藏

package com.abin.lee.collection.pool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class ConnectionManage {
 private static List<Connection> pool=new LinkedList<Connection>();;
 private static final String Url = "jdbc:oracle:thin:@localhost:1521:XE";
 private static final String USERNAME = "abing";
 private static final String PASSWORD = "abing";
 private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
 private static int initCount = 5;
 private static int maxCount = 10;
 private static int currentCount = 5;
 private static volatile ConnectionManage instance = null;
 private ConnectionManage(){
  init();
 }
 public static ConnectionManage getInstance(){
  if(null == instance){
   synchronized (ConnectionManage.class) {
    if(null == instance){
     return new ConnectionManage();
    }
   }
  }
  return instance;
 }
 public void init(){
  addConnection();
 }
 public static Connection createConnection() throws ClassNotFoundException {
  Connection conn=null;
  try {
   Class.forName(DRIVER);
   conn=DriverManager.getConnection(Url, USERNAME, PASSWORD);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return conn;
 }
 public static void addConnection(){
  for(int i=0;i<initCount;i++){
   try {
    pool.add(createConnection());
   } catch (ClassNotFoundException e) {
    e.printStackTrace();
   }
  }
 }
 
 public static void release(Connection conn){
  pool.add(conn);
 }
 public static Connection getConnection() throws SQLException, ClassNotFoundException{
  synchronized (pool) {
   if(pool.size()>0){
    System.out.println("Current Connection size is:"+pool.size());
    return pool.get(0);
   }else if(currentCount<maxCount){
    Class.forName(DRIVER);
    Connection conn=createConnection();
    pool.add(conn);
    currentCount++;
    return conn;
   }else{
    throw new SQLException("Current Connection is Zero");
   }
  }
 }

}





测试代码:

package com.abin.lee.collection.pool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.abin.lee.collection.threadlocal.UserBean;

public class TestConnectionPool {

 public static List<UserBean> findAll(){
  Connection conn=null;
  PreparedStatement ps=null;
  ResultSet rs=null;
  String sql="select * from userbean order by id desc";
  List<UserBean> list=new ArrayList<UserBean>();
  UserBean user=null;
  try {
   conn=ConnectionManage.getInstance().getConnection();
   ps=conn.prepareStatement(sql);
   rs=ps.executeQuery();
   while (rs.next()&&rs!=null) {
    user=new UserBean();
    user.setId(rs.getString("id"));
    user.setUsername(rs.getString("username"));
    user.setPassword(rs.getString("password"));
    list.add(user);
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return list;
 }
 public static void main(String[] args) {
  List<UserBean> list=findAll();
  for(UserBean lst:list){
   System.out.println("id="+lst.getId());
   System.out.println("username="+lst.getUsername());
   System.out.println("id="+lst.getPassword());
  }
 }


}

posted @ 2012-09-04 00:18 abin 阅读(1541) | 评论 (0)编辑 收藏

http://06peng.com/read.php/13.htm
http://blog.sina.com.cn/s/blog_70ce7e9e0100v9dh.html
http://blog.163.com/lgw1984_12/blog/static/4122245520096239913479/
http://mzba520.iteye.com/blog/939691
http://blog.csdn.net/java_blue/article/details/6581480
http://blog.csdn.net/a600423444/article/details/6542521
http://blog.csdn.net/caoyinghui1986/article/details/2315864

posted @ 2012-09-03 21:56 abin 阅读(358) | 评论 (0)编辑 收藏

下面是以前学java 的时候写的一个实现,没有考虑很多,比如同步处理等等。懒得改,你就看看吧。

package com.wangjia.tools;

import java.io.Serializable;
import java.sql.*;
import java.util.*;

/**
* 数据库连接池的简单实现
* @author wangjia
* @version 1.0
*@serial 2301410997974776106L
*/
public class DbConn implements Serializable{

/**
* 
*/
private static final long serialVersionUID = 2301410997974776106L;
private static List<Connection> connList=new ArrayList<Connection>();
//private static String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";//sql2000
private static String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//sql2005
private static String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Forum";
private static String userName="sa";
private static String password="sa";
private static int maxConn=20;
private static int minConn=5;
private static int usedConn=0;
private static DbConn dbconn=null;
// private static int flag=1;
/**
* 设置数据库连接的驱动
* @param driver 数据库连接的驱动<br/>例如:"com.microsoft.sqlserver.jdbc.SQLServerDriver"
*/
public static void setDriver(String driver) {
DbConn.driver = driver;
}

/**
* 设置数据库连接池的最小数
* @param maxConn 数据库连接池的最小数量
*/
public static void setMaxConn(int maxConn) {
DbConn.maxConn = maxConn;
}

/**
* 设置数据库连接池的最大数
* @param minConn 数据库连接池的最大数量
*/
public static void setMinConn(int minConn) {
DbConn.minConn = minConn;
}

/**
* 设置数据库连接的URL
* @param url 数据库连接的URL,包括数据库的名称<br/>例如: "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=master"
*/
public static void setUrl(String url) {
DbConn.url = url;
}

private DbConn() throws MyException{
try{
Class.forName(driver);
for(int i=0;i<minConn;i++){
connList.add(DriverManager.getConnection(url,userName.toString(),password.toString()));
}
}catch(ClassNotFoundException e){
throw new MyException("请设置正确的数据库连接驱动!\nusing: DbConn.setDriver(String driver)");

}catch(SQLException e){
throw new MyException("请设置正确的数据库连接URL以及用于连接的用户名和密码!\n" +
"using: DbConn.setUrl(String url),DbConn.setUserName(String userName),DbConn.setPassword(String password)");

}
}
/**
* 用于得到DbConn对象
* @return 返回一个DbConn对像
*/

public static Connection getConn() throws MyException{
// flag++;
if(dbconn==null)dbconn=new DbConn();
// System.out.println("falg="+flag);
// System.out.println("usedConn="+usedConn);
if(connList.size()==0){
throw new MyException("没有正确的设置相关信息,以至连接池未初始化");
}
if(usedConn==maxConn){

Thread thread=new Thread(new Runnable(){

@SuppressWarnings("static-access")
public void run() {
if(usedConn<maxConn){
try {
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
getConn();
} catch (MyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

});
thread.start();
}
if(usedConn>=minConn&&usedConn<maxConn){
try {
connList.add(DriverManager.getConnection(url,userName.toString(),password.toString()));
} catch (SQLException e) {

}
return connList.get(usedConn++);
}
if(usedConn<5){
return connList.get(usedConn++);
}
return null;
}

public static void close(){
try {
if(usedConn>5)
connList.get(--usedConn).close();
else usedConn--;
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}

}

/**
* 设置用于连接数据库的密码
* @param password 用于连接数据库的密码
*/
public static void setPassword(String password) {
DbConn.password = password;
}

/**
* 设置用于连接数据库的用户名
* @param userName 用于连接数据库的用户名
*/
public static void setUserName(String userName) {
DbConn.userName = userName;
}

}

在项目中用到连接池的话,一般会直接使用已有的开源连接池实现,比如用 apache 的common-dbcp.jar和common-pool.jar 相结合,很少去自己实现。

posted @ 2012-09-03 21:55 abin 阅读(512) | 评论 (0)编辑 收藏

http://blog.csdn.net/ohmygirl/article/details/7776013 

http://blog.csdn.net/laigood12345/article/details/7350295
posted @ 2012-09-03 13:14 abin 阅读(480) | 评论 (0)编辑 收藏

1、去官网下载hession的java架包:
官网地址:http://hessian.caucho.com/
我这里下载的是hessian-4.0.7.jar


2、编写接口:
package com.abin.lee.hession;
public interface IBasicSource {
public String getMessage(String name);
public UserBean getUserBean();
}

3、编写接口实现类:
package com.abin.lee.hession;
public class BasicSource implements IBasicSource{
public String getMessage(String name) {
return "hello,"+name;
}
public UserBean getUserBean() {
UserBean userBean=new UserBean();
userBean.setId(1);
userBean.setAddress("liangcheng");
userBean.setName("lee");
return userBean;
}
}
4、实体类,这里需要注意一下,这里必须的实现序列化接口Serializable,因为是需要网络传输的

package com.abin.lee.hession;
import java.io.Serializable;
public class UserBean implements Serializable{
private static final long serialVersionUID = -8931696839145847364L;
private int id;
private String name;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}

5、配置web.xml
<!--下面是Hession的配置-->
<servlet>
<servlet-name>hessianServlet</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>  
<param-name>home-class</param-name>
<param-value>com.abin.lee.hession.BasicSource</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<param-value>com.abin.lee.hession.IBasicSource</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hessianServlet</servlet-name>
<url-pattern>/hessianServlet</url-pattern>
</servlet-mapping>



6、编写测试类:

package com.abin.lee.hession.test;
import java.net.MalformedURLException;
import junit.framework.TestCase;
import com.abin.lee.hession.IBasicSource;
import com.abin.lee.hession.UserBean;
import com.caucho.hessian.client.HessianProxyFactory;
public class TestHession extends TestCase{
private static final String HESSION_URL="http://localhost:8081/universal/hessianServlet";
public void testHession() throws MalformedURLException{
HessianProxyFactory factory=new HessianProxyFactory(); 
IBasicSource basic=(IBasicSource)factory.create(IBasicSource.class, HESSION_URL);
String result=basic.getMessage("abin");
System.out.println("result="+result);
UserBean user=basic.getUserBean();
System.out.println("ID="+user.getId()+",NAME="+user.getName()+",ADDRESS="+user.getAddress());
}
}


测试结果:
result=hello,abin
ID=1,NAME=lee,ADDRESS=liangcheng

到了这里就成功了。
posted @ 2012-09-02 19:11 abin 阅读(1887) | 评论 (0)编辑 收藏

package com.abin.lee.collection.inner;
public class XiXiang {
public static enum IdType{
ONE("one"),TWO("two"),THREE("three");
private String textVal;
private IdType(String textVal){
this.textVal=textVal;
}
private String getString(){
return textVal;
}
}
public static String get(IdType type,String id){
String status=null;
final class get{
String result=null;
public String getName(String id){
if(id.equals("1")){
result="lee";
}
return result;
}
}
if(type==IdType.valueOf("ONE")){
status=new get().getName(id);
}
return status;
}
public static void main(String[] args) {
String id="1";
IdType type=IdType.valueOf("ONE");
String result=new XiXiang().get(type,id);
System.out.println("result="+result);
}
}
posted @ 2012-09-02 18:00 abin 阅读(2372) | 评论 (0)编辑 收藏

package com.abin.lee.collection.inner;
public class LiangCheng {
public static String LocalProcess(String wu, String id) {
String well = null;
if (wu.equalsIgnoreCase("abin")) {
LiangCheng liangCheng=new LiangCheng();
LiangCheng.Process process=liangCheng.new Process();
well=process.getNameById(id);
}
return well;
}
public class Process {
private String result = null;
public String getNameById(String id) {
if (id.equalsIgnoreCase("15")) {
result = "lee";
}
return result;
}
}
public static void main(String[] args) {
LiangCheng liangCheng=new LiangCheng();
String wu="abin";
String id="15";
String result=liangCheng.LocalProcess(wu, id);
System.out.println("result="+result);
}
}
posted @ 2012-09-02 16:43 abin 阅读(451) | 评论 (0)编辑 收藏

package com.abin.lee.collection.enumer;
public class ChangeType {
public static enum stype{
CHINA("china"),JAPAN("japan"),GERMAN("german");
private String textVal;
private stype(String textVal){
this.textVal=textVal;
}
public String toString(){
return textVal;
}
}
public static void main(String[] args) {
String abin=stype.CHINA.toString();
System.out.println(abin);
stype abing=stype.valueOf("CHINA");//注意这里不应该传china、japan、german,必须传CHINA,JAPAN,GERMAN
System.out.println("abing="+abing);
}
}
posted @ 2012-09-02 15:46 abin 阅读(404) | 评论 (0)编辑 收藏

仅列出标题
共50页: First 上一页 29 30 31 32 33 34 35 36 37 下一页 Last