- 配置文件在jdbc工具类中的作用
- java读取不同位置下的配置文件
- xml常用三种解析方法
- xml快速解析方式xpath
配置文件在jdbc工具类中的作用
1.方便自己使用数据库时的使用,使用哪个数据库就将哪个数据库解开注释,其他的注释
#oracle9i #driver=oracle.jdbc.driver.OracleDriver #url=jdbc:oracle:thin:@localhost:1521:orcl #user=scott #pwd=123 #sql2005 #driver=com.microsoft.sqlserver.jdbc.SQLServerDriver #url=jdbc:sqlserver://localhost:1433;DatabaseName=test1 #user=sa #pwd=123 #sql2000 #driver=com.microsoft.jdbc.sqlserver.SQLServerDriver #url=jdbc:microsoft:sqlserver://localhost:1433;databaseName=unit6DB #user=sa #pwd=888888 #mysql driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis_ssm?useUnicode=true&characterEncoding=UTF-8&useSSL=false user=root pwd=123456
java读取不同位置下的配置文件
1.同包
2.根目录
3.安全目录WEB-INF(提高安全性,db.properties必须放WEB-INF里面)
package com.zhulinjun; import java.io.InputStream; import java.util.Properties; public class demo1 { public static void main(String[] args) throws Exception { // 1.同包(/XMLl/src/com/zhulinjun/db.properties)src的packge里,通过类加载器加载配置文件 InputStream in = demo1.class.getResourceAsStream("db.properties"); Properties p = new Properties(); // 让properties工具类对象,加载含有答辩。properties文件类容所对应的流 p.load(in); System.out.println(p.getProperty("url")); System.out.println(p.getProperty("upass")); //2.根目录(/XMLl/conf/config.properties)新建一个Source Folder包conf InputStream in1 = demo1.class.getResourceAsStream("/db.properties"); Properties p1=new Properties(); p1.load(in1); System.out.println(p1.getProperty("url")); System.out.println(p1.getProperty("upass")); } }
package com.zhulinjun; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/Webinf") public class WebinfServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //3.安全目录WEB-INF(/XMLl/WebContent/WEB-INF/db.properties) InputStream in2 = request.getServletContext().getResourceAsStream("/WEB-INF/db.properties"); Properties p2=new Properties(); p2.load(in2); System.out.println(p2.getProperty("url")); System.out.println(p2.getProperty("upass")); } }
xml常用三种解析方法
package com.zhulinjun; import java.io.InputStream; import java.util.List; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class demo2 { public static void main(String[] args) throws Exception { // 先获取到需要解析的配置文件 InputStream in = demo2.class.getResourceAsStream("students.xml"); SAXReader sr=new SAXReader(); Document doc = sr.read(in); //System.out.println(doc.asXML()); // 1.获取所有student标签里的内容 List<Element> stuEles = doc.selectNodes("/students/student"); for (Element stuEle : stuEles) { //System.out.println(stuEle.asXML()); // 2.获取所有name标签的内容 Element nameEle = (Element) stuEle.selectSingleNode("name"); //System.out.println(nameEle.asXML()); // 3.获取标签内部的内容 //System.out.println(nameEle.getText()); // 4.获取标签属性值,及学生的学号id System.out.println(stuEle.attributeValue("sid")); } } }
xml快速解析方式xpath
package com.zhulinjun; import java.io.InputStream; import java.util.List; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.SAXReader; public class demo3 { public static void main(String[] args) throws Exception { // 获取到sid=s002学生的姓名 InputStream in = demo3.class.getResourceAsStream("students.xml"); SAXReader sr=new SAXReader(); Document doc = sr.read(in); //System.out.println(doc.asXML()); List<Element> stuEles = doc.selectNodes("/students/student"); for (Element stuEle : stuEles) { if("s002".equals(stuEle.attributeValue("sid"))) { Element nameEle = (Element) stuEle.selectSingleNode("name"); System.out.println(nameEle.getText()); } } //xpath解析 Element nameEle = (Element) doc.selectSingleNode("/students/student[@sid='s002']/name"); System.out.println(nameEle.getText()); } }