XML导入数据库

实体类代码 article.java

package sax;


public class article {
private String author;
private String title;
private String pages;
private int year;
private int volume;
private String journal;
private String number;
private String ee;
private String url;
public article(String author, String title, String pages, int year, int volume, String journal, String number,
String ee, String url) {
super();
this.author = author;
this.title = title;
this.pages = pages;
this.year = year;
this.volume = volume;
this.journal = journal;
this.number = number;
this.ee = ee;
this.url = url;
}
public article() {

}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPages() {
return pages;
}
public void setPages(String pages) {
this.pages = pages;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
}
public String getJournal() {
return journal;
}
public void setJournal(String journal) {
this.journal = journal;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getEe() {
return ee;
}
public void setEe(String ee) {
this.ee = ee;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}

}


自定义MyHelper.java

package sax;


import java.util.ArrayList;
import java.util.List;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MyHandler extends DefaultHandler {
private List<article> articles=new ArrayList<>();
private article article;
private String author="";
privat int porpertyorder=0;//author=1,title=2,pages=3,year=4,volume=5,journal=6,number=7,ee=8,url=9
@Override
public void startDocument() throws SAXException {
System.out.println("文档开始");
}
@Override
public void endDocument() throws SAXException {
System.out.println("文档结束");
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if(qName.equals("article"))
{
article=new article();
}
switch(qName)
{
case "author":
porpertyorder=1;
break;
case "title":
porpertyorder=2;
break;
case "pages":
porpertyorder=3;
break;
case "year":
porpertyorder=4;
break;
case "volume":
porpertyorder=5;
break;
case "journal":
porpertyorder=6;
break;
case "number":
porpertyorder=7;
break;
case "ee":
porpertyorder=8;
break;
case "url":
porpertyorder=9;
break;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if(qName.equals("article"))
{
articles.add(article);
article=null;
author="";
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String content=new String(ch,start,length);
switch(porpertyorder)
{
case 1:
author=author+" "+content;
article.setAuthor(author);
break;
case 2:
article.setTitle(content);
break;
case 3:
article.setPages(content);
break;
case 4:
article.setYear(Integer.parseInt(content));;
break;
case 5:
article.setVolume(Integer.parseInt(content));
break;
case 6:
article.setJournal(content);
break;
case 7:
article.setNumber(content);
break;
case 8:
article.setEe(content);
break;
case 9:
article.setUrl(content);
break;
}
}
public List<article> getarticles()
{
return articles;
}
}

主函数

package sax;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import javax.imageio.stream.IIOByteBuffer;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;


import org.xml.sax.SAXException;


public class saxpaser {
public static void main(String[] args) throws Exception {
articleDao dao=new articleDao();
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
MyHandler handler=new MyHandler(); 
parser.parse("dblp/dblp.xml",handler);
List<article> articles=new ArrayList<>();
articles=handler.getarticles();
System.out.println(articles.size());
for(int i=0;i<articles.size();i++)
{
dao.insert(articles.get(i));
}
}

}


Dao层

package sax;


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


public class articleDao {
public int insert(article article) throws Exception{
String driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://localhost:1433;DatabaseName=dblp";
Class.forName(driverClass);
Connection con=DriverManager.getConnection(url, "dblplogin", "123456");
String sql="insert into article values(?,?,?,?,?,?,?,?,?)";  
PreparedStatement pstmt=con.prepareStatement(sql);
         pstmt.setString(1,article.getAuthor());  
         pstmt.setString(2,article.getTitle());
         pstmt.setString(3, article.getPages());
         pstmt.setInt(4, article.getYear());
         pstmt.setInt(5, article.getVolume());
         pstmt.setString(6, article.getJournal());
         pstmt.setString(7, article.getNumber());
         pstmt.setString(8, article.getEe());
         pstmt.setString(9, article.getUrl());
        int count=pstmt.executeUpdate();
return 0;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值