JAVA 常见代码分享
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);} } }
XML code
100001
123 李四 1000000.00 100002
123 张三 1000.00
java jdbc 数据库连接
import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties;
public class JDBConnection { public Connection conn = null; // 声明 Connection 对象的实例 public Statement stmt = null; // 声明 Statement 对象的实例 public ResultSet rs = null; // 声明 ResultSet 对象的实例 private static String dbClassNam
e = "com.microsoft.jdbc.sqlserver.SQLServerDriver";//定义 保存数据库驱动的变量 private static String dbUrl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=DB_ATM"; private static String dbUser = "sa"; private static String dbPwd = "sa"; public JDBConnection(String propertyFileName) {// 带属性文件名的构造方法 Properties prop = new Properties();// 属性集合对象 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(); } } public JDBConnection() {// 默认的不带参数的构造函数 try { Class.forName(dbClassName);// 1.注册驱动 } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection 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); } return conn; } /* * 功能:执行查询语句 */ public ResultSet executeQuery(String sql) { try { // 捕捉异常 conn = getConnection(); // 调用 getConnection()方法构造 Connection 对象的一 个实例 conn
stmt = 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 对象的一 个实例 conn stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); result = stmt.executeUpdate(sql); // 执行更新操作 } catch (SQLException ex) { result = 0; // 将保存返回值的变量赋值为 0 } return result; // 返回保存返回值的变量 } /* * 功能:关闭数据库的连接 */ public void close() {//6.释放资源 try { // 捕捉异常 try { if (rs != null) { // 当 ResultSet 对象的实例 rs 不为空时 rs.close(); // 关闭 ResultSet 对象 } } finally { try { i
f (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 自定义按钮外观
import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.UIManager; import javax.swing.plaf.synth.SynthLookAndFeel; public class MyButton { JFrame frame = new JFrame("Test Buttons"); JButton jButton = new JButton("JButton"); // 按钮 public MyButton() { frame.setLayout(new FlowLayout()); frame.getContentPane().add(jButton); } public void show() { frame.pack(); frame.show(); } public static void main(String[] args) { MyButton tb = new MyButton(); tb.show(); SynthLookAndFeel slf = new SynthLookAndFeel(); try { slf.load(MyButton.class.getResourceAsStream("mybutton.xml"), MyButton.class); UIManager.setLookAndFeel(slf);
} catch (Exception e) { e.printStackTrace(); return; } } }
XML code