王剑编程网

分享专业编程知识与实战技巧

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 version="1.0" encoding="gbk"?>
<Accounts>
<Account type="by0003">
<code>100001code>
<pass>123pass>
<name>李四name>
<money>1000000.00money>
Account>
<Account type="hz0001">
<code>100002code>
<pass>123pass>
<name>张三name>
<money>1000.00money>
Account>
Accounts>

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 dbClassName = "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 {
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自定义按钮外观


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;
}
}
}



<synth>
<style id="mybutton">
<state>
<imagePainter method="buttonBackground" path="mybutton.png" sourceInsets="3 6 12 20" paintCenter="true" stretch="true"/>
<insets top="3" left="6" bottom="12" right="20"/>
<font name="Aharoni" size="16"/>
state>
<property key="Button.margin" type="insets" value="0 0 5 8"/>
style>
<bind style="mybutton" type="region" key="Button"/>
synth>


java访问资源文件


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
public class PropertyEditor {
public static void main(String[] args) throws Exception {
Properties prop = new Properties();// 属性集合对象
FileInputStream fis = new FileInputStream("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", "中国");
// 添加一个新的属性studio
prop.setProperty("studio", "Boxcode Studio");
// 文件输出流
FileOutputStream fos = new FileOutputStream("prop.properties");
// 将Properties集合保存到流中
prop.store(fos, "Copyright (c) Boxcode Studio");
fos.close();// 关闭流
}
}

就先分享到这一会在给大家继续分享



想要了解更多Java知识那就来关注我们吧! 精彩内容多多哦!不从错过哦!

多多关注



控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言