网站首页 > 教程分享 正文
//15.Java Mail
// 发送Mail
protected String msgRecIp = "hxydream@163.com";
protected String msgSubject = "babytree";
protected String msgCc = "nobody@erewhon.com";
protected String msgBody = "test body";
protected Session session;
protected Message msg;
public void doSend() {
// 创建属性文件
Properties props = new Properties();
props.put("mail.smtp.host", "mailhost");
// 创建Session对象
session = Session.getDefaultInstance(props, null);
session.setDebug(true);
msg = new MimeMessage(session); // 创建邮件
msg. setFrom(new InternetAddress("nobody@host.domain"));
InternetAddress toAddr = new InternetAddress(msgRecIp);
msg.addRecipient(Message.RecipientType.TO, toAddr);
InternetAddress ccAddr = new InternetAddress(msgCc);
msg.addRecipient(Message.RecipientType.CC, ccAddr);
msg.setSubject(msgSubject);
msg.setText(msgBody);
Transport.send(msg);
}
// 发送MIME邮件
Multipart mp = new MimeMultipart();
BodyPart textPart = new MimeBodyPart();
textPart.setText(message_body); // 设置类型"text/plain"
BodyPart pixPart = new MimeBodyPart();
pixPart.setContent(html_data, "text/html");
mp.addBodyPart(textPart);
mp.addBodyPart(pixPart);
mesg.setContent(mp);
Transport.send(mesg);
// 读Mail
Store store = session.getStore(protocol);
store.connect(host, user, password);
Folder rf;
rf = store.getFolder(root);
rf = store.getDefaultFolder();
rf.open(Folder.READ_WRITE);
//16. 数据库访问
// JDO
Properties p = new Properties();
p.load(new FileInputStream("jdo.properties"));
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(p);
PersistenceManager pm = pmf.getPersistenceManager();
// 提交数据
pm.currentTransaction().begin();
if (o instanceof Collection) {
pm.makePersistentAll((Collection) o);
} else {
pm.makePersistent(o);
}
pm.currentTransaction().commit();
pm.close();
// 取出数据
Object[] data = new Object[3];
pm.retrieveAll(data);
for (int i = 0; i < data.length; i++) {
System.out.println(data[i]);
}
pm.close();
// 数据操作
Class clz = Class.forName("oracle.jdbc.driver.OracleDriver");
String dbUrl = "jdbc:oracle:thin:@192.168.0.23:1521#:nms";
Connection conn = DriverManager.getConnection(dbUrl, "su", "1234");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from pmtable");
while (rs.next()) {
String name = rs.getString(1);
String otherName = rs.getString("name");
}
// 使用PreparedStatement提高性能,除了查询,都使用executeUpdate执行操作
PreparedStatement pstmt = conn.prepareStatement("select * from pmtable where name = ?");
pstmt.setString(1, "sean");
ResultSet rs = pstmt.executeQuery();
// 调用存储过程
CallableStatement cs = conn.prepareCall("{ call ListDefunctUsers }");
ResultSet rs = cs.executeQuery();
// 显示数据库表信息
DatabaseMetaData meta = conn.getMetaData();
meta.getDatabaseProductName();
meta.getDatabaseProductVersion();
meta.getDefaultTransactionIsolation();
/*
17.XML
SAX: 在读取文档提取相应的标记事件(元素起始、元素结束、文档起始)
DOM: 在内存中构造与文档中元素相应的树,可以遍历、搜索、修改
DTD: 验证文档是否正确
JAXP: 用于XML处理的Java API
Castor: 开源项目,用于Java对象与XML映射
*/
// 从对象中生成XML
private final static String FILENAME = "serial.xml";
public static void main(String[] args) throws IOException {
String a = "hard work and best callback";
new SerialDemoXML().write(a);
new SerialDemoXML().dump();
}
public void write(Object obj) throws IOException {
XMLEncoder os = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(FILENAME)));
os.writeObject(obj);
os.close();
}
public void dump() throws IOException {
XMLDecoder out = new XMLDecoder(new BufferedInputStream(new FileInputStream(FILENAME)));
System.out.println(out.readObject());
out.close();
}
serial.xml格式内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.6.0_02" class="java.beans.XMLDecoder">
<string>hard work and best callback</string>
</java>
控制台输出
hard work and best callback
// XSLT转换XML
XSLT可以用来对输出格式进行各种控制
Transformer tx = TransformerFactory.newInstance().newTransformer(new StreamSource("people.xml"));
tx.transform(new StreamSource("people.xml"), new StreamResult("people.html"));
// 用SAX解析XML - 主要用于查找关键元素,不用全文遍历
public SaxLister() throws SAXException, IOException {
XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
parser.setContentHandler(new PeopleHandler());
parser.parse("C:\\StudySource\\javacooksrc2\\xml\\people.xml");
}
class PeopleHandler extends DefaultHandler {
boolean parent = false;
boolean kids = false;
public void startElement(String nsURI, String localName, String rawName, Attributes attr) throws SAXException {
System.out.println("startElement: " + localName + "," + rawName);
if (rawName.equalsIgnoreCase("name"))
parent = true;
if (rawName.equalsIgnoreCase("children"))
kids = true;
}
public void characters(char[] ch, int start, int length) {
if (parent) {
System.out.println("Parent: " + new String(ch, start, length));
parent = false;
} else if (kids) {
System.out.println("Children: " + new String(ch, start, length));
kids = false;
}
}
public PeopleHandler() throws SAXException {
super();
}
}
// DOM解析XML - 遍历整个树
String uri = "file:" + new File("C:\\StudySource\\javacooksrc2\\xml\\people.xml").getAbsolutePath();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(uri);
NodeList nodes = doc.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node n = nodes.item(i);
switch (n.getNodeType()) {
case Node.ELEMENT_NODE:
// todo
break;
case Node.TEXT_NODE:
// todo
break;
}
}
// 使用DTD或者XSD验证
定义好DTD或XSD文件
XmlDocument doc = XmlDocument.createXmlDocument(uri, true);
// 用DOM生成XML
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = fact.newDocumentBuilder();
Document doc = parser.newDocument();
Node root = doc.createElement("Poem");
doc.appendChild(root);
Node stanza = doc.createElement("Stanza");
root.appendChild(stanza);
Node line = doc.createElement("Line");
stanza.appendChild(line);
line.appendChild(doc.createTextNode("Once, upon a midnight dreary"));
line = doc.createElement("Line");
stanza.appendChild(line);
line.appendChild(doc.createTextNode("While I pondered, weak and weary"));
//18.RMI
a. 定义客户端与服务器之间的通信接口
public interface RemoteDate extends Remote {
public Date getRemoteDate() throws RemoteException;
public final static String LOOKUPNAME = "RemoteDate";
}
b. 编写RMI服务器
public class RemoteDateImpl extends UnicastRemoteObject implements RemoteDate {
public RemoteDateImpl() throws RemoteException {
super();
}
public Date getRemoteDate() throws RemoteException {
return new Date();
}
}
RemoteDateImpl im = new RemoteDateImpl();
System.out.println("DateServer starting...");
Naming.rebind(RemoteDate.LOOKUPNAME, im);
System.out.println("DateServer ready.");
c. 运行rmic生成stub
javac RemoteDateImpl.java
rmic RemoteDateImpl
d. 编写客户端
netConn = (RemoteDate)Naming.lookup(RemoteDate.LOOKUPNAME);
Date today = netConn.getRemoteDate();
System.out.println(today.toString());
e. 确保RMI注册表运行
rmiregistry
f. 启动服务器
java RemoteDateImpl
g. 运行客户端
java DateClient
猜你喜欢
- 2024-09-08 Web前端应该懂的JavaScript、Ajax、jQuery知识点!
- 2024-09-08 72 个网络应用安全实操要点,全方位保护 Web 应用的安全
- 2024-09-08 XSLT 实例(xsl:choose)
- 2024-09-08 JAVA历史版本(java历史版本列表)
- 2024-09-08 Ajax入门教程(非常详细)动力节点ajax教程,讲解全面
- 2024-09-08 如何在J2EE平台开发基于Velocity模板的Web应用
- 2024-09-08 前端 JS 之 AJAX 简介及使用(web前端ajax数据请求)
- 2024-09-08 网站开发必知流程项目开发(网站开发流程图)
- 2024-09-08 有同学问我:Fetch 和 Ajax 有什么区别?
- 2024-09-08 前端程序员进阶篇——深入理解Ajax
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- css导航条 (66)
- sqlinsert (63)
- js提交表单 (60)
- param (62)
- parentelement (65)
- jquery分享 (62)
- check约束 (64)
- curl_init (68)
- sql if语句 (69)
- import (66)
- chmod文件夹 (71)
- clearinterval (71)
- pythonrange (62)
- 数组长度 (61)
- javafx (59)
- 全局消息钩子 (64)
- sort排序 (62)
- jdbc (69)
- php网页源码 (59)
- assert h (69)
- httpclientjar (60)
- postgresql conf (59)
- winform开发 (59)
- mysql数字类型 (71)
- drawimage (61)
本文暂时没有评论,来添加一个吧(●'◡'●)