1.配置文件在jdbc中的作用
1.在Java中,JDBC(Java Database Connectivity)是一种用于连接和操作数据库的API。配置文件在JDBC中的作用主要是用来存储数据库连接的信息,如数据库的URL、驱动程序的类名、用户名和密码等。这些信息通常不应该硬编码到Java代码中,因为这样做会使代码变得非常不灵活,而且会增加维护的难度。相反,将这些信息保存在配置文件中,可以轻松地更改它们,从而避免了在更改连接信息时需要修改代码的麻烦。
2.配置文件通常用来指定JDBC驱动程序的加载和连接信息。在Java中,可以使用java.util.Properties类来读取配置文件。这个类提供了一种简单的方法来加载和解析属性文件,以获取应用程序所需的配置信息。调用Properties类的load()方法,可以从任何实现了java.io.InputStream接口的字节流中读取属性列表。然后,可以使用getProperty()方法获取配置信息,并将其传递给JDBC连接对象。
3.总之,配置文件在JDBC中极其重要,它可以方便地管理和维护连接信息,并使代码更加灵活和易于扩展。
2.读取不同位置下的配置文件
1.同包下读取配置文件
package com.niyin.xml2; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * * @author 匿瘾 *同包 */ public class demo1 { public static void main(String[] args) throws Exception { // 通过类加载器加载配置文件 InputStream inputStream = demo1.class.getResourceAsStream("db.properties"); Properties p=new Properties(); p.load(inputStream); System.out.println(p.getProperty("driver_Class")); System.out.println(p.getProperty("upass")); System.out.println(p.getProperty("url")); } }
被读取的配置文件
运行结果
2.根目录下读取配置文件
1.区别在与多了/,其它的与前面没有区别
package com.niyin.xml2; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * * @author 匿瘾 *同包 */ public class demo1 { public static void main(String[] args) throws Exception { // 通过类加载器加载配置文件 InputStream inputStream = demo1.class.getResourceAsStream("/db.properties"); Properties p=new Properties(); p.load(inputStream); System.out.println(p.getProperty("driver_Class")); System.out.println(p.getProperty("upass")); System.out.println(p.getProperty("url")); } }
3.读取安全目录WEB-INF下的配置文件
package com.niyin.xml2; 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; /** * * @author 匿瘾 * */ @WebServlet("/WebinfServlet") public class WebinfServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { InputStream resourceAsStream = request.getServletContext().getResourceAsStream("/WEB-INF/lib/db.properties"); Properties p=new Properties(); p.load(resourceAsStream); System.out.println(p.getProperty("driver_Class")); System.out.println(p.getProperty("upass")); System.out.println(p.getProperty("url")); } }
运行结果
3.xml解析常用方法
1. 获取xml文件的内容DOM4J解析
xml,文件,接下来都会用它演示
package com.niyin.xml2; import java.io.InputStream; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.SAXReader; /** * * @author 匿瘾 * */ public class demo2 { public static void main(String[] args) throws Exception { InputStream inputStream = demo2.class.getResourceAsStream("students.xml"); SAXReader s = new SAXReader(); Document doc = s.read(inputStream); System.out.println(doc.asXML()); } }
运行结果
2. 使用selectNodes获取student里的内容
package com.niyin.xml2; import java.io.InputStream; import java.util.List; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; /** * * @author 匿瘾 * */ public class demo2 { public static void main(String[] args) throws Exception { InputStream inputStream = demo2.class.getResourceAsStream("students.xml"); SAXReader s = new SAXReader(); Document doc = s.read(inputStream); // System.out.println(doc.asXML()); System.out.println(doc.asXML()); List<Element> list = doc.selectNodes("/students/student"); // List<Element> stuEles = doc.selectNodes("/students/student"); System.out.println(list); } }
运行结果
3.获取name标签内容,selectsingleNode
package com.niyin.xml2; 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; /** * * @author 匿瘾 * */ public class demo2 { public static void main(String[] args) throws Exception { InputStream inputStream = demo2.class.getResourceAsStream("students.xml"); SAXReader s = new SAXReader(); Document doc = s.read(inputStream); // System.out.println(doc.asXML()); // System.out.println(doc.asXML()); List<Element> list = doc.selectNodes("/students/student"); List<Element> stuEles = doc.selectNodes("/students/student"); for (Element element : stuEles) { // System.out.println(element.asXML()); Element nameELe = (Element) element.selectSingleNode("name"); System.out.println(nameELe.asXML()); } } }
4.获得标签内容,getText
package com.niyin.xml2; 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; /** * * @author 匿瘾 * */ public class demo2 { public static void main(String[] args) throws Exception { InputStream inputStream = demo2.class.getResourceAsStream("students.xml"); SAXReader s = new SAXReader(); Document doc = s.read(inputStream); // System.out.println(doc.asXML()); // System.out.println(doc.asXML()); List<Element> list = doc.selectNodes("/students/student"); List<Element> stuEles = doc.selectNodes("/students/student"); for (Element element : stuEles) { // System.out.println(element.asXML()); Element nameELe = (Element) element.selectSingleNode("name"); // System.out.println(nameELe.asXML()); System.out.println(nameELe.getText()); // System.out.println(element.attributeValue("sid")); } } }
运行结果
4.获取标签属性值,及学生学号,attributeValue
package com.niyin.xml2; 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; /** * * @author 匿瘾 * */ public class demo2 { public static void main(String[] args) throws Exception { InputStream inputStream = demo2.class.getResourceAsStream("students.xml"); SAXReader s = new SAXReader(); Document doc = s.read(inputStream); // System.out.println(doc.asXML()); // System.out.println(doc.asXML()); List<Element> list = doc.selectNodes("/students/student"); List<Element> stuEles = doc.selectNodes("/students/student"); for (Element element : stuEles) { // System.out.println(element.asXML()); Element nameELe = (Element) element.selectSingleNode("name"); // System.out.println(nameELe.asXML()); //System.out.println(nameELe.getText()); System.out.println(element.attributeValue("sid")); } } }
运行结果
4.xml快速解析方法
1.普通方式获得sid对应的name
package com.niyin.xml2; import java.io.InputStream; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.SAXReader; /** * * @author 匿瘾 * Xpath解析 * */ public class demo3 { public static void main(String[] args) throws Exception { InputStream inputStream = demo2.class.getResourceAsStream("students.xml"); SAXReader s = new SAXReader(); Document doc = s.read(inputStream); List<Element> stuEles = doc.selectNodes("/students/student"); for (Element element : stuEles) { if ("s002".equals(element.attributeValue("sid"))) { Element nameEle = (Element) element.selectSingleNode("name"); System.out.println(nameEle.getText()); } //xpath,解析 //Element nameEle = (Element) doc.selectSingleNode("/students/student[@sid='s002']/name"); // System.out.println(nameEle.getText()); // } } }
运行结果
2.通过Xpath方式获得
package com.niyin.xml2; import java.io.InputStream; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.SAXReader; /** * * @author 匿瘾 * Xpath解析 * */ public class demo3 { public static void main(String[] args) throws Exception { InputStream inputStream = demo2.class.getResourceAsStream("students.xml"); SAXReader s = new SAXReader(); Document doc = s.read(inputStream); // List<Element> stuEles = doc.selectNodes("/students/student"); // // for (Element element : stuEles) { //if ("s002".equals(element.attributeValue("sid"))) { // Element nameEle = (Element) element.selectSingleNode("name"); // // System.out.println(nameEle.getText()); // //} //xpath,解析 Element nameEle = (Element) doc.selectSingleNode("/students/student[@sid='s002']/name"); System.out.println(nameEle.getText()); } } //}
总结,两者,比较,xpath的方式更为简便,快捷。