import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
//转换成list
public class BeanLists {
public static void main(String[] args) {
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/ptest";
String jdbcDriver = "com.mysql.jdbc.Driver";
String user = "root";
String password = "ptest";
DbUtils.loadDriver(jdbcDriver);
try {
conn = DriverManager.getConnection(url, user, password);
QueryRunner qr = new QueryRunner();
List results = (List) qr.query(conn, "select id,name from person", new BeanListHandler(Person.class));
for (int i = 0; i < results.size(); i++) {
Person p = (Person) results.get(i);
System.out.println("id:" + p.getId() + ",name:" + p.getName());
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DbUtils.closeQuietly(conn);
}
}
}
public class Person{
private Integer id;
private String name;
//省略set, get方法
}
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
//转换成map
public class MapLists {
public static void main(String[] args) {
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/ptest";
String jdbcDriver = "com.mysql.jdbc.Driver";
String user = "root";
String password = "ptest";
DbUtils.loadDriver(jdbcDriver);
try {
conn = DriverManager.getConnection(url, user, password);
QueryRunner qr = new QueryRunner();
List results = (List) qr.query(conn, "select id,name from person", new MapListHandler());
for (int i = 0; i < results.size(); i++) {
Map map = (Map) results.get(i);
System.out.println("id:" + map.get("id") + ",name:" + map.get("name"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DbUtils.closeQuietly(conn);
}
}
}
9、Email 提供的一个开源的API,是对javamail的封装。
//用commons email发送邮件
public static void main(String args[]){
Email email = new SimpleEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setSSLOnConnect(true);
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");
email.send();
}
10、FileUpload java web文件上传功能。
//官方示例:
//* 检查请求是否含有上传文件
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
//现在我们得到了items的列表
//如果你的应用近于最简单的情况,上面的处理就够了。但我们有时候还是需要更多的控制。
//下面提供了几种控制选择:
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(yourMaxMemorySize);
factory.setRepository(yourTempDirectory);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置最大上传大小
upload.setSizeMax(yourMaxRequestSize);
// 解析所有请求
List /* FileItem */ items = upload.parseRequest(request);
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory(
yourMaxMemorySize, yourTempDirectory);
//一旦解析完成,你需要进一步处理item的列表。
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
processFormField(item);
} else {
processUploadedFile(item);
}
}
//区分数据是否为简单的表单数据,如果是简单的数据:
// processFormField
if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
//...省略步骤
}
//如果是提交的文件:
// processUploadedFile
if (!item.isFormField()) {
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
//...省略步骤
}
//对于这些item,我们通常要把它们写入文件,或转为一个流
// Process a file upload
if (writeToFile) {
File uploadedFile = new File(...);
item.write(uploadedFile);
} else {
InputStream uploadedStream = item.getInputStream();
//...省略步骤
uploadedStream.close();
}
//或转为字节数组保存在内存中:
// Process a file upload in memory
byte[] data = item.get();
//...省略步骤
//如果这个文件真的很大,你可能会希望向用户报告到底传了多少到服务端,让用户了解上传的过程
//Create a progress listener
ProgressListener progressListener = new ProgressListener(){
public void update(long pBytesRead, long pContentLength, int pItems) {
System.out.println("We are currently reading item " + pItems);
if (pContentLength == -1) {
System.out.println("So far, " + pBytesRead + " bytes have been read.");
} else {
System.out.println("So far, " + pBytesRead + " of " + pContentLength
+ " bytes have been read.");
}
}
};
upload.setProgressListener(progressListener);
Google Guava,wiki,github,**code.google.com,mvn:最初名为Google Collections,2007.10 v0.5[ref],2009年年末创建Guava[ref],The Guava project contains several of Google’s core libraries that we rely on in our Java-based projects: collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, and so forth.