Java 使用jdbc和xml文件建立与数据库的连接
我在我的项目中面临一个问题。我必须读取一个xml文件,根据从xml中获取的一些值,我必须获取一个查询,在该查询中,它应该根据匹配的xml值获取值Java 使用jdbc和xml文件建立与数据库的连接,java,xml,jdbc,Java,Xml,Jdbc,我在我的项目中面临一个问题。我必须读取一个xml文件,根据从xml中获取的一些值,我必须获取一个查询,在该查询中,它应该根据匹配的xml值获取值 public class XMLParser extends HttpServlet { HashMap<String,String>hMap1=new HashMap<String, String>(); HashMap<String,String>hMap2=new HashMap<
public class XMLParser extends HttpServlet
{
HashMap<String,String>hMap1=new HashMap<String, String>();
HashMap<String,String>hMap2=new HashMap<String, String>();
String series1,series2,ID;
int s1, s2;
public void getAllUserNames(String fileName)
{
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
File file = new File(fileName);
if (file.exists())
{
Document doc = db.parse(file);
Element docEle = doc.getDocumentElement();
System.out.println("Root element of the document: "+ docEle.getNodeName());
NodeList studentList = docEle.getElementsByTagName("emplopyee");
System.out.println("Total emplopyee: " + studentList.getLength());
if (studentList != null && studentList.getLength() > 0)
{
for (int i = 0; i < studentList.getLength(); i++)
{
Node node = studentList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element e = (Element) node;
NodeList nodeList1 = e.getElementsByTagName("ID");
ID= nodeList1.item(0).getChildNodes().item(0).getNodeValue();
System.out.println("ID: "+nodeList1.item(0).getChildNodes().item(0).getNodeValue());
NodeList nodeList2 = e.getElementsByTagName("Name");
series1 =nodeList2.item(0).getChildNodes().item(0).getNodeValue();
System.out.println("Name: "+ nodeList2.item(0).getChildNodes().item(0).getNodeValue());
NodeList nodeList3 = e.getElementsByTagName("Days");
series2 = nodeList3.item(0).getChildNodes().item(0).getNodeValue();
System.out.println("Days: "+ nodeList3.item(0).getChildNodes().item(0).getNodeValue());
hMap1.put(ID,series1);
hMap2.put(ID, series2);
System.out.println(hMap1);
System.out.println(hMap2)
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root",null);
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from emp1 where eid ='"+ID+"' ");
while(rs.next())
{
int n1 = Integer.parseInt(rs.getString(1));
String n2 = rs.getString(2);
int n3 = Integer.parseInt(rs.getString(3));
int n4 = Integer.parseInt(rs.getString(4));
System.out.println(n1);
System.out.println(n2);
System.out.println(n3);
System.out.println(n4);
}
}
}
}
else {
System.exit(1);
}
}
} catch (Exception e) {
System.out.println(e);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException
{
XMLParser parser = new XMLParser();
parser.getAllUserNames("D:\\employee.xml");
}
}
public类XMLParser扩展了HttpServlet
{
HashMaphMap1=新的HashMap();
HashMaphMap2=新的HashMap();
字符串序列1,序列2,ID;
int s1,s2;
public void getAllUserNames(字符串文件名)
{
试一试{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
文件=新文件(文件名);
if(file.exists())
{
文档doc=db.parse(文件);
Element docEle=doc.getDocumentElement();
System.out.println(“文档的根元素:+docEle.getNodeName());
NodeList studentList=docEle.getElementsByTagName(“员工”);
System.out.println(“员工总数:+studentList.getLength());
if(studentList!=null&&studentList.getLength()>0)
{
对于(int i=0;i
“我面临一个问题”-是的,然后告诉我们。例外?堆栈跟踪?谢谢长官实际上我已经解决了我的问题之一是另一点我想把数值读成整数,double或根据xml文件中的数字格式这里我想要整数格式,因为你可以看到序列2我想要整数格式,这样我可以做一些计算,但我无法转换,请帮助我。任何人请帮助我…你想要得到整数值,你正在读一个double?在这种情况下,通过使用:“(int)variable”来转换值应该可以工作是的,先生,我犯了错误thnks:)