恩~是可行,但代码方面如何?用JDOM? 如果xml比较复杂?
解决方案 »
- 麻烦请教大家一个问题,jbpm的插件我装好之后,鼠标右键new other里边一定会有一个 jBoss jbpm-process project这个项吗?我装好
- dwr高手请进,小弟不胜感激
- 嵌套循环在页面的显示问题
- 有没有好看的java 安装文件制作工具
- 在linux系统下,如何使用java连接局域网内的access数据库?
- 急,中文URL的问题。谢谢了。
- 我想学J2EE,请问大家用什么集成开发环境好?
- 谁有用JAVA开发socket程序的例子?
- 求 j2ee 的java.lang.NullPointerException问题快要发疯了
- 求助 The function equals must be used with a prefix when a default namespace is no
- 帮忙看看该怎么改~~~
- 【100分】谁帮我解决下Linux下部署java应用后出现乱码问题!
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import java.sql.*;
import java.util.*;public class ReadXml
{
public Connection conn = null;
public Statement stmt = null ;
public void readXml()
{
//***************************建立数据库连接***************************
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/jdomdb?user=root&password=";//jdomdb为你的数据库名
conn = DriverManager.getConnection(url);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
catch(Exception sqlexception)
{
System.out.println("数据库连接发生异常!");
}
//********************************************************************* //**********************读xml文件**************************************
try
{
SAXBuilder sb = new SAXBuilder(); Document doc = sb.build("userinfo.xml");
Element root = doc.getRootElement(); String username = "",id="" ,homephone="",officephone="";
String password ="",usernum="" ;
String homeaddress ="",officeaddress = "",mobile="" ;
Element elms = null;
Element elms2 = null;
Element elms3 = null; List list2 = null;
List list3 = null;
List list1 = root.getChildren("userinfo");
for(int i=0; i< list1.size(); i++)
{
elms = (Element)list1.get(i); //userinfo节点子元素 id = elms.getChildText("id");
usernum = elms.getChildText("usernum");
username = elms.getChildText("username");
password = elms.getChildText("password");
list2 = elms.getChildren("userphone"); //读出userinfo节点的userphone子节点的元素
for(int k=0; k< list2.size(); k++)
{
elms2 = (Element)list2.get(k);
homephone = elms2.getChildText("homephone");
officephone = elms2.getChildText("officephone");
mobile = elms2.getChildText("mobile");
}
list3 = elms.getChildren("useraddress"); //读出useraddress节点的元素
for(int k=0; k< list3.size(); k++)
{
elms3 = (Element)list3.get(k);
homeaddress = elms3.getChildText("homeaddress");
officeaddress = elms3.getChildText("officeaddress");
} //插入数据库的表tmpinfo
String sql = "insert into tmpinfo(usernum,username,password,homephone,officephone,mobile,homeaddress,officeaddress)values ('"+usernum+"','"+username+"','"+password+"','"+homephone+"','"+officephone+"','"+mobile+"','"+homeaddress+"','"+officeaddress+"')";
stmt.executeUpdate(sql); }//for
stmt.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
//**********************读xml文件**************************************
}
public static void main(String[] args)
{
ReadXml rx = new ReadXml();
rx.readXml();
}
}
<table name="ABC">
<column name="A" type="char" length=""/>
<rows>
<row id="" A="" B=""/>
</rows>
</table>
应该怎么修改?成功连接数据库:xml
java.lang.NullPointerException
at XMLViewGenerator.xml.xmltomysql(xml.java:82)
at XMLViewGenerator.xml.main(xml.java:98)
package XMLViewGenerator;import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import java.sql.*;
import java.util.*;public class xml
{
public Connection con = null;
public Statement stmt = null ;
private final String url = "jdbc:mysql://localhost:3306/test";
private final String databaseName= "ppd";
private final String userName = "root";
private final String password = "123456";
private java.sql.Connection getConnection() {
try{ Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url,userName,password);
if(con!=null)
System.out.println("成功连接数据库:"+databaseName); } catch(Exception e) {
e.printStackTrace();
System.out.println("跟踪在方法getConnection()出现的错误 : " + e.getMessage()); }
return con; } private void closeConnection() {
try {
if(con!=null)
con.close();
con=null; } catch(Exception e){ e.printStackTrace(); } }
public void xmltomysql()
{
//***************************建立数据库连接***************************
try{con= this.getConnection();
if(con!=null) {
//********************************************************************* //**********************读xml文件**************************************
try
{
SAXBuilder sb = new SAXBuilder(); Document doc = sb.build("xml.xml");
Element root = doc.getRootElement(); String id="";
String name="";
String game="";
String score="";
Element elms = null;
Element elms2 = null;
List list2 = null;
List list3 = null;
List list1 = root.getChildren("xml");//得到根节点的所有子节点,返回的是List类型的变量
for(int i=0; i< list1.size(); i++)
{
elms = (Element)list1.get(i); //userinfo节点子元素 id = elms.getChildText("id");
name = elms.getChildText("name");
list2 = elms.getChildren("game"); //读出userinfo节点的userphone子节点的元素
for(int k=0; k< list2.size(); k++)
{
elms2 = (Element)list2.get(k);
score = elms2.getChildText("score");
} //插入数据库的表info
String sql1 ="CREATE TABLE info (id INT,name CHAR,game CHAR, score INT);";
String sql = "insert into info(id,name,game,score)values ('"+id+"','"+name+"','"+game+"','"+score+"')";
stmt.executeUpdate(sql1);
stmt.executeUpdate(sql); }//for
stmt.close();
closeConnection();
}
catch(Exception e)
{e.printStackTrace();}
//**********************读xml文件**************************************
}
else
System.out.println("错误: 没有可用的数据库连接!");} catch(Exception e){ e.printStackTrace(); }
}
public static void main(String[] args)throws Exception
{
xml rx = new xml();
rx.xmltomysql();
}
}
不确定的文件没啥好办法。
package XMLViewGenerator;import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import java.sql.*;
import java.util.*;public class xml
{
public Connection con = null;
public Statement stmt = null ;
private final String url = "jdbc:mysql://localhost:3306/test";
private final String databaseName= "ppd";
private final String userName = "root";
private final String password = "123456";
private java.sql.Connection getConnection() {
try{ Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url,userName,password);
if(con!=null)
System.out.println("成功连接数据库:"+databaseName); } catch(Exception e) {
e.printStackTrace();
System.out.println("跟踪在方法getConnection()出现的错误 : " + e.getMessage()); }
return con; } private void closeConnection() {
try {
if(con!=null)
con.close();
con=null; } catch(Exception e){ e.printStackTrace(); } }
public void xmltomysql()
{
//***************************建立数据库连接***************************
try{con= this.getConnection();
if(con!=null) {
//*********************************************************************
stmt =con.createStatement();
//**********************读xml文件**************************************
try
{
SAXBuilder sb = new SAXBuilder(); Document doc = sb.build("xml.xml");
Element root = doc.getRootElement(); String id="";
String name="";
String game="";
String score="";
Element elms = null;
Element elms2 = null;
List list2 = null;
List list3 = null;
List list1 = root.getChildren("xml");//得到根节点的所有子节点,返回的是List类型的变量
for(int i=0; i< list1.size(); i++)
{
elms = (Element)list1.get(i); //userinfo节点子元素 id = elms.getChildText("id");
name = elms.getChildText("name");
list2 = elms.getChildren("game"); //读出userinfo节点的userphone子节点的元素
for(int k=0; k< list2.size(); k++)
{
elms2 = (Element)list2.get(k);
score = elms2.getChildText("score");
} //插入数据库的表info
String sql0 ="CREATE TABLE info (id INT,name CHAR,game CHAR, score INT);";
String sql = "insert into info(id,name,game,score)values ('"+id+"','"+name+"','"+game+"','"+score+"')";
stmt.executeUpdate(sql0);
stmt.executeUpdate(sql); }//for
stmt.close();
closeConnection();
}
catch(Exception e)
{e.printStackTrace();}
//**********************读xml文件**************************************
}
else
System.out.println("错误: 没有可用的数据库连接!");} catch(Exception e){ e.printStackTrace(); }
}
public static void main(String[] args)throws Exception
{
xml rx = new xml();
rx.xmltomysql();
}
}