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