java访问xml文件
import java.io.*;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;public class xmljava{ public static void main(String args[]) { Element element=null; File f =new File("a.xml"); DocumentBuilder db=null; //documentBuilder为抽象不能直接实例化(将XML文件转换为DOM文件) DocumentBuilderFactory dbf=null; try{ dbf= DocumentBuilderFactory.newInstance(); //返回documentBuilderFactory对象 db =dbf.newDocumentBuilder();//返回db对象用documentBuilderFatory对象获得返回documentBuildr对象 Document dt= db.parse(f); //得到一个DOM并返回给document对象 element = dt.getDocumentElement();//得到一个elment根元素 System.out.println("根元素:"+element.getNodeName()); //获得根节点 NodeList childNodes =element.getChildNodes() ; // 获得根元素下的子节点 for (int i = 0; i < childNodes.getLength(); i++) // 遍历这些子节点 { Node node1 = childNodes.item(i); // childNodes.item(i); 获得每个对应位置i的结点 if ("Account".equals(node1.getNodeName())) { // 如果节点的名称为"Account",则输出Account元素属性type System.out.println("\r\n找到一篇账号. 所属区域: " + node1.getAttributes().getNamedItem ("type").getNodeValue() + ". "); NodeList nodeDetail = node1.getChildNodes(); // 获得下的节点 for (int j = 0; j < nodeDetail.getLength(); j++) { // 遍历下的节点 Node detail = nodeDetail.item(j); // 获得元素每一个节点 if ("code".equals(detail.getNodeName())) // 输出code System.out.println("卡号: " + detail.getTextContent()); else if ("pass".equals(detail.getNodeName())) // 输出pass System.out.println("密码: " + detail.getTextContent()); else if ("name".equals(detail.getNodeName())) // 输出name System.out.println("姓名: " + detail.getTextContent()); else if ("money".equals(detail.getNodeName())) // 输出money System.out.println("余额: "+ detail.getTextContent()); } } }}catch(Exception e){System.out.println(e);}}}xmlversion="1.0"encoding="gbk"?><Accounts><Accounttype="by0003"><code>100001code><pass>123pass><name>李四name><money>1000000.00money>Account><Accounttype="hz0001"><code>100002code><pass>123pass><name>张三name><money>1000.00money>Account>Accounts>java jdbc数据库连接
importjava.io.InputStream;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.Properties;publicclassJDBConnection {publicConnection conn =null;// 声明Connection对象的实例publicStatement stmt =null;// 声明Statement对象的实例publicResultSet rs =null;// 声明ResultSet对象的实例privatestaticString dbClassName ="com.microsoft.jdbc.sqlserver.SQLServerDriver";//定义保存数据库驱动的变量privatestaticString dbUrl ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=DB_ATM";privatestaticString dbUser ="sa";privatestaticString dbPwd ="sa";publicJDBConnection(String propertyFileName) {// 带属性文件名的构造方法Properties prop =newProperties();// 属性集合对象InputStream is =null;try{is = JDBConnection.class.getClassLoader().getResourceAsStream(propertyFileName);// 属性文件输入流// is = new FileInputStream("src/" + propertyFileName);prop.load(is);// 将属性文件流装载到Properties对象中is.close();// 关闭流dbClassName = prop.getProperty("dbClassName");dbUrl = prop.getProperty("dbUrl");dbUser = prop.getProperty("dbUser");dbPwd = prop.getProperty("dbPwd");}catch(Exception e) {System.out.println("属性文件 "+ propertyFileName +" 打开失败!");}try{Class.forName(dbClassName);// 1.注册驱动}catch(ClassNotFoundException e) {e.printStackTrace();}}publicJDBConnection() {// 默认的不带参数的构造函数try{Class.forName(dbClassName);// 1.注册驱动}catch(ClassNotFoundException e) {e.printStackTrace();}}publicstaticConnection getConnection() {Connection conn =null;try{// Class.forName(dbClassName);// 1.注册驱动conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);//2.建立与数据库的链接}catch(Exception ee) {ee.printStackTrace();}if(conn ==null) {System.err.println("警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:"+ dbClassName+"\r\n链接位置:"+ dbUrl+"\r\n用户/密码"+ dbUser +"/"+ dbPwd);}returnconn;}/** 功能:执行查询语句*/public ResultSet executeQuery(String sql) {try { // 捕捉异常conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例connstmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,//3.创建语句ResultSet.CONCUR_READ_ONLY);rs = stmt.executeQuery(sql);//4.执行查询} catch (SQLException ex) {System.err.println(ex.getMessage()); // 输出异常信息}return rs; // 返回结果集对象 5.结果处理}/** 功能:执行更新操作*/public int executeUpdate(String sql) {int result = 0; // 定义保存返回值的变量try { // 捕捉异常conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例connstmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);result = stmt.executeUpdate(sql); // 执行更新操作} catch (SQLException ex) {result = 0; // 将保存返回值的变量赋值为0}return result; // 返回保存返回值的变量}/** 功能:关闭数据库的连接*/publicvoidclose() {//6.释放资源try{// 捕捉异常try{if(rs !=null) {// 当ResultSet对象的实例rs不为空时rs.close();// 关闭ResultSet对象}}finally{try{if(stmt !=null) {// 当Statement对象的实例stmt不为空时stmt.close();// 关闭Statement对象}}finally{if(conn !=null) {// 当Connection对象的实例conn不为空时conn.close();// 关闭Connection对象}}}}catch(Exception e) {e.printStackTrace(System.err);// 输出异常信息}}}属性文件dbClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver
dbClassName2=com.mysql.jdbc.Driver
dbPwd=sa
dbPwd2=root
dbUrl=jdbc\:microsoft\:sqlserver\://localhost\:1433;DatabaseName\=DB_ATM
dbUrl2=jdbc\:mysql\://localhost\:3306/db_atm
dbUser=sa
dbUser2=root
java自定义按钮外观
importjava.awt.FlowLayout;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.UIManager;importjavax.swing.plaf.synth.SynthLookAndFeel;publicclassMyButton {JFrame frame =newJFrame("Test Buttons");JButton jButton =newJButton("JButton");// 按钮publicMyButton() {frame.setLayout(newFlowLayout());frame.getContentPane().add(jButton);}publicvoidshow() {frame.pack();frame.show();}publicstaticvoidmain(String[] args) {MyButton tb =newMyButton();tb.show();SynthLookAndFeel slf =newSynthLookAndFeel();try{slf.load(MyButton.class.getResourceAsStream("mybutton.xml"), MyButton.class);UIManager.setLookAndFeel(slf);}catch(Exception e) {e.printStackTrace();return;}}}
<synth><styleid="mybutton"><state><imagePaintermethod="buttonBackground"path="mybutton.png"sourceInsets="3 6 12 20"paintCenter="true"stretch="true"/><insetstop="3"left="6"bottom="12"right="20"/><fontname="Aharoni"size="16"/>state><propertykey="Button.margin"type="insets"value="0 0 5 8"/>style><bindstyle="mybutton"type="region"key="Button"/>synth>
java访问资源文件
importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.util.Properties;publicclassPropertyEditor {publicstaticvoidmain(String[] args)throwsException {Properties prop =newProperties();// 属性集合对象FileInputStream fis =newFileInputStream("prop.properties");// 属性文件输入流 (相对于根目录下的文件名,要加上包名 “src/prop.properties”)prop.load(fis);// 将属性文件流装载到Properties对象中fis.close();// 关闭流// 获取属性值,sitename已在文件中定义System.out.println("获取属性值:sitename="+ prop.getProperty("sitename"));// 获取属性值,country未在文件中定义,将在此程序中返回一个默认值,但并不修改属性文件System.out.println("获取属性值:country="+ prop.getProperty("country","中国"));// 修改sitename的属性值prop.setProperty("sitename","中国");// 添加一个新的属性studioprop.setProperty("studio","Boxcode Studio");// 文件输出流FileOutputStream fos =newFileOutputStream("prop.properties");// 将Properties集合保存到流中prop.store(fos,"Copyright (c) Boxcode Studio");fos.close();// 关闭流}}就先分享到这一会在给大家继续分享
想要了解更多Java知识那就来关注我们吧! 精彩内容多多哦!不从错过哦!
多多关注
