SAX解析XML文件并存入MySQL数据库

public class EventHandler extends DefaultHandler {
private String biao;
private Project project;
private int j;


@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
String text = new String(ch, start, length);
text = text.replaceAll("[-:\\s]", "");
// System.out.println(text);
if (!"".equals(text)) {
switch (biao) {
case "school":
project.setSchool(text);
break;
case "pid":
project.setPid(text);
break;
case "name":
project.setName(text);
break;
case "type":
project.setType(text);
break;
case "leader":
project.setLeader(text);
break;
case "supervisor":
project.setSupervisor(text);
break;
case "stitle":
project.setStitle(text);
break;
case "fee":
project.setFee(Integer.parseInt(text));
break;
case "introduction":
project.setIntroduction(text);
break;
}
}

}


public class SAX {

public static void main(String[] args) {
File file = new File("src/2013部属高校国家级大学生创新项目.xml");
SAXParserFactory sFactory =SAXParserFactory.newInstance();
try {
SAXParser saxParser = sFactory.newSAXParser();
DefaultHandler handler = new EventHandler();
saxParser.parse(file, handler);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();

}

}


package util;


public class Project {
    private String pid;
    private String School;
    private String name;
    private String type;
    private String  leader;
    private String supervisor;
    private String stitle;
    private Integer fee;
    private String introduction;
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getSchool() {
return School;
}
public void setSchool(String school) {
School = school;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getLeader() {
return leader;
}
public void setLeader(String leader) {
this.leader = leader;
}
public String getSupervisor() {
return supervisor;
}
public void setSupervisor(String supervisor) {
this.supervisor = supervisor;
}
public String getStitle() {
return stitle;
}
public void setStitle(String stitle) {
this.stitle = stitle;
}
public Integer getFee() {
return fee;
}
public void setFee(Integer fee) {
this.fee = fee;
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
    
    

}


package util;




import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class DbHelper {
private static DbHelper dbHelper = null;
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/education";
private static final String USER = "root";
private static final String PASSWORD = "12345";

private Connection connection = null;
private Statement stmt = null;

static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

private DbHelper() {
try {
connection = DriverManager.getConnection(URL, USER, PASSWORD);
stmt = connection.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}

public static DbHelper getDbHelper() {
try {
if (dbHelper == null || dbHelper.connection == null || dbHelper.connection.isClosed()) {
dbHelper = new DbHelper();
}
} catch (SQLException e) {
e.printStackTrace();
}
return dbHelper;
}

/**
* 鎵ц鏌ヨ
* @param sql 鏃犲弬鏁扮殑SQL璇彞
* @return
* @throws Exception
*/
public ResultSet executeQuery(String sql) throws Exception {
return stmt.executeQuery(sql);
}

/**
* 鎵ц鏌ヨ
* @param sql 甯﹀弬鏁扮殑SQL璇彞
* @param args 鍙傛暟鍒楄〃
* @return
* @throws Exception
*/
public ResultSet executeQuery(String sql, Object...args) throws Exception {
// 鑾峰彇棰勭紪璇戝璞�
PreparedStatement ps = connection.prepareStatement(sql);
// 瀹炰緥鍖栧弬鏁�
if (args != null && args.length > 0) {
for (int i = 1; i <= args.length; i++) {
ps.setObject(i, args[i-1]);
}
}
// 鎵ц鏌ヨ
return ps.executeQuery();
}


/**
* 鎵ц鏇存柊
* @param sql 鏃犲弬鏁扮殑SQL璇彞
* @return
* @throws Exception
*/
public int executeUpdate(String sql) throws Exception {
return stmt.executeUpdate(sql);
}


/**
* 鎵ц鏇存柊
* @param sql 甯﹀弬鏁扮殑SQL璇彞
* @param args 鍙傛暟鍒楄〃
* @return
* @throws Exception
*/
public int executeUpdate(String sql, Object...args) throws Exception {
// 鑾峰彇棰勭紪璇戝璞�
PreparedStatement ps = connection.prepareStatement(sql);
// 瀹炰緥鍖栧弬鏁�
if (args != null && args.length > 0) {
for (int i = 1; i <= args.length; i++) {
ps.setObject(i, args[i-1]);
}
}
// 鎵ц鏌ヨ
return ps.executeUpdate();
}

public PreparedStatement prepare(String sql) throws Exception {
return connection.prepareStatement(sql);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值