//ThreadLocal第一种用法:
//建表:
-- Create table
create table USERBEAN
(
ID NVARCHAR2(40) not null,
USERNAME NVARCHAR2(40),
PASSWORD NVARCHAR2(40)
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table USERBEAN
add constraint SSSID primary key (ID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
package com.abin.lee.collection.threadlocal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MyThreadLocal {
//使用ThreadLocal保存Connection变量
public static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
private static final String URL = "jdbc:oracle:thin:@localhost:1521:XE";
private static final String USERNAME = "abing";
private static final String PWD = "abing";
public static Connection getConnection() {
Connection connection = null;
String url = "";
//如果connThreadLocal没有本线程对应的Connection创建一个新的Connection,并将其保存到线程本地变量中。
if (threadLocal.get() == null) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
connection = DriverManager.getConnection(URL, USERNAME, PWD);
threadLocal.set(connection);
} catch (Exception e) {
e.printStackTrace();
}
}else {
return threadLocal.get();//直接返回线程本地变量
}
return connection;
}
public static void remove() {
threadLocal.remove();
}
}
package com.abin.lee.collection.threadlocal;
import java.io.Serializable;
public class UserBean implements Serializable{
private String id;
private String username;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
//测试代码
package com.abin.lee.collection.threadlocal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class OracleOperate {
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=MyThreadLocal.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());
}
}
}
//ThreadLocal第二种用法:
package com.abin.lee.collection.threadlocal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MyThreadLocal {
private static final String URL = "jdbc:oracle:thin:@localhost:1521:XE";
private static final String USERNAME = "abing";
private static final String PWD = "abing";
//使用ThreadLocal保存Connection变量
public static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>(){
protected Connection initialValue() {
Connection connection = null;
//如果connThreadLocal没有本线程对应的Connection创建一个新的Connection,并将其保存到线程本地变量中。
if (connection == null) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
connection = DriverManager.getConnection(URL, USERNAME, PWD);
} catch (Exception e) {
e.printStackTrace();
}
}
return connection;
}
};
public static Connection getConnection() {
return threadLocal.get();
}
public static void remove() {
threadLocal.remove();
}
}
package com.abin.lee.collection.threadlocal;
import java.io.Serializable;
public class UserBean implements Serializable{
private String id;
private String username;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.abin.lee.collection.threadlocal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class OracleOperate {
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=MyThreadLocal.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());
}
}
}