1、数据库连接池:
之前对数据库连接池概念一直很模糊,在项目中修改数据库连接方式时才对jdbc数据库的连接池方式有了一个更深刻的认识,下面简要介绍使用方法:
(1)、定义数据库连接的配置文件,如下所示:
driverClassName = com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/db
username=username
password=pwd
initialSize=10
maxActive=50
maxIdle=20
minIdle=5
maxWait=60000
connectionProperties=useUnicode=true;characterEncoding=utf8
defaultAutoCommit=true
(2)、获取数据源的方法如下:
public class ConnectManager {
static Logger logger = Logger.getLogger(ConnectManager.class);
private static DataSource dataSource = null;
static {
Properties prop = new Properties();
InputStream inStream = ConnectManager.class.getClassLoader()
.getResourceAsStream("jdbc.properties");
try {
prop.load(inStream);
//创建dataSource
dataSource = BasicDataSourceFactory.createDataSource(prop);
} catch (IOException e) {
logger.info("读取jdbc配置文件失败");
}catch (Exception e) {
logger.info("数据源创建失败");
}
}
public static Connection getConnection() {
logger.info("使用JDBC获取数据库连接");
try {
//从数据源中获取一个连接
return dataSource.getConnection();
} catch (SQLException e) {
logger.info("获取数据库连接失败!");
e.printStackTrace();
return null;
}
}
//JNDI连接方式
public static Connection getConnection1() {
logger.info("使用tomcat数据库连接池获取连接");
Connection conn = null;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
conn = ds.getConnection();
} catch (NamingException e) {
logger.error("注册数据库连接池失败" + e);
} catch (SQLException e) {
logger.error("创建连接发生异常" + e);
}
return conn;
}*/
public static void free(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
logger.error("关闭rs异常", e);
}
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
logger.error("关闭ps异常", e);
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
logger.error("关闭conn异常", e);
}
}
public static void free(Connection conn, Statement st, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
logger.error("关闭rs异常", e);
}
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
logger.error("关闭st异常", e);
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
logger.error("关闭conn异常", e);
}
}
}
注意:使用这种最基本的连接池方式,每次对数据库的操作完成后都要手动完成对连接的释放,否则会造成数据库连接无法释放,连接超时问题,更多jdbc-封装知识见http://www.nihiler.com/。
2、select2选择控件:
访问http://ivaynberg.github.io/select2/可访问select2官网的使用方法的一些说明,在做项目时用到的属性不多,主要是赋值的问题,使用方法如下:
(1)、引入头文件,如下所示:
<link href="<%=request.getContextPath() %>/bootstrap/js/select/select2.css" rel="stylesheet"/>
<script src="<%=request.getContextPath() %>/bootstrap/js/select/select2.min.js"></script>
(2)、在给某个html控件如input、select赋值数据源时要求数据格式为:{"tags":[{id:0,text:'enhancement'},{id:1,text:'bug'},{id:2,text:'duplicate'},{id:3,text:'invalid'},{id:4,text:'wontfix'}]},具体赋某个值时使用方法:$("#e8").select2("data",{id:"CA", text:"California"});
3、多文件上传uploadify:
(1)、页面代码如下:
<script type="text/javascript">
$(document).ready(function(){
$("#fileUpload").uploadify({
'buttonText':"选择文件",
'buttonCursor':'hand',
'buttonClass':"some-class",
'auto':false,
'swf':"<%=request.getContextPath() %>/uploadify/uploadify.swf",
'uploader':"manyAjaxUploadFile_uploadManyFile.action",
'multi':"true",
'queueSizeLimit':10,
'fileSizeLimit':"10MB",
'fileObjName':"uploadImages",//跟后台fileName对应
'removeCompleted':false,
'progressData':"speed",
'onUploadStart': function(){
$("#windowMark").show();
},
'onUploadProgress' : function(file, bytesUploaded, bytesTotal, totalBytesUploaded, totalBytesTotal) {
$('#progress').html(totalBytesUploaded + ' bytes uploaded of ' + totalBytesTotal + ' bytes.');
},
'#onUploadError': function(){
$("filePromt").html("文件限传10个,每个文件最大10MB,请检查是否选择正确......");
},
'onQueueComplete': function(){
$("#windowMark").hide();
}
});
$("#delUploadRecord").click(function(){
$('#fileUpload').uploadify('cancel','*');
$('#progress').html("");
});
});
</script>
<div class="container">
<input type="file" name="fileUpload" id="fileUpload"/>
<input type="button" style="width:100px" class="btn btn-blue" value="全部上传" οnclick="javascript:$('#fileUpload').uploadify('upload','*')">
<input type="button" style="width:100px" class="btn btn-blue" id="delUploadRecord" value="删除记录" οnclick="javascript:$('#fileUpload').uploadify('cancel','*')">
<div id="progress"></div>
</div>
(2)后台接收上传文件代码:
private List<File> uploadImages;//与上面fileObjName属性值对应
3、java跨域访问资源:
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setConnectTimeout(120000);
conn.setReadTimeout(240000);
conn.connect();
br = new BufferedReader(//获取服务器返回结果
new InputStreamReader(conn.getInputStream()));
StringBuffer buff = new StringBuffer();
String lineStr = null;
while ((lineStr = br.readLine()) != null) {
buff.append(lineStr);
}
report = buff.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}