-1

这是我的 test_database.html 页面

<html>
<head> 
<title> <h1> Database Test </h1> </title>
<script>

    var xmlhttp;
    if (window.XMLHttpRequest) 
    {
        xmlhttp = new XMLHttpRequest();
    } else 
    {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    function loadXMLDoc()
    {
        xmlhttp.onreadystatechange = function() 
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) 
            {
                var xmlObj = xmlhttp.responseXML;

                var textXML = xmlObj.documentElement.firstChild.firstChild.nodeValue;


                if (window.DOMParser)
                {
                    parser=new DOMParser();
                    var xmlDoc=parser.parseFromString(textXML,"text/xml");
                }
                else // Internet Explorer
                {
                    var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
                    xmlDoc.async=false;
                    xmlDoc.loadXML(textXML);
                }   
                var rows = xmlDoc.getElementsByTagName("row");
                mybody=document.getElementsByTagName("body")[0];
                mytable=document.createElement("table");
                mytablebody=document.createElement("tbody");
                //alert(cols.length);
                for(var i=0; i<rows.length; i++)
                {   mycurrent_row=document.createElement("tr"); 
                    var cols=rows[i].getElementsByTagName("colunm");

                   for(var j=0; j<cols.length; j++) 
                    {     mycurrentcell=document.createElement("td");  
                          currenttext=document.createTextNode(cols[j].getAttribute("colvalue"))
                          mycurrentcell.appendChild(currenttext);
                          mycurrent_row.appendChild(mycurrentcell);  
                     }  
                                     mycurrent_row.id=cols[0].getAttribute("colvalue");
                         mybutoncell = document.createElement("td");
                         but=document.createElement("input");
                         but.type="button";
                         mybutoncell.appendChild(but);                                      
                         mycurrent_row.appendChild(mybutoncell); 
                         mytablebody.appendChild(mycurrent_row);
                         but.onclick=ajaxDBDelete;
                }
                mytable.appendChild(mytablebody);
                mybody.appendChild(mytable);
                 mytable.setAttribute("border","2");    
            }
        }
        xmlhttp.open("GET","http://127.0.0.1:8080/axis2/services/database_tmm_man/SelectFromDB?",true);
        xmlhttp.send();

    }       
    function ajaxDBInsert ()
    { 
     var textvalue= document.getElementById('txbegn').value
       var xml="<university><student>";
       xml+="<egn>" +textvalue+ "</egn>";
       xml+="<fname>" + document.getElementById('txbfname').value + "</fname>";
       xml+="<mname>" + document.getElementById('txbmidname').value + "</mname>" ;
       xml+="<lname>" + document.getElementById('txblname').value + "</lname>";
       xml+="<telnum>" + document.getElementById ('txbtelnum').value + "</telnum>";
       xml+="</student></university>";

    alert(xml);
    var tranXml = escape(xml); 
    alert(tranXml);

        var url = "localhost/services/database_tmm_man/InsertInto?";
        var params = "data=" + tranXml;
        xmlhttp.open("POST", url, true);
        xmlhttp.send(params);   


    }

    function ajaxDBDelete()
    {
          var par=this.parentNode;
          while(par.nodeName.toLowerCase()!='tr')
          {
              par=par.parentNode;
          }
          alert (par.id);
         var delid="<university><student>";
         delid+= "<egn>" + par.id + "</egn>";
         delid+= "</student></university>";
         alert(delid);
         var tranDelid = escape(delid);
         alert (tranDelid);

        var url = "http://127.0.0.1:8080/axis2/services/database_tmm_man/DeleteFromDB?";
        var params = "deleteID=" + tranDelid;
        xmlhttp.open("POST", url, true);
        xmlhttp.send(params);   
    }
    </script>
</head>

<body onload="loadXMLDoc();">

EGN:                  <input type="text" id="txbegn"/><br>
FIRST NAME:           <input type="text" id="txbfname"/><br>
MIDDLE NAME:          <input type="text" id="txbmidname"/><br>
LAST NAME:            <input type="text" id="txblname"/><br>
Telephone Number:     <input type="text" id="txbtelnum"/><br>
<br>
<br>
<button type="button" name="btnInsert" onClick="ajaaclxDBInsert();">INSERT</button>
<br>
<br>
</body>
</html>

这是我的服务的代码

package gismo;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.net.URLDecoder;
import java.sql.*;

import javax.xml.parsers.*; 

import org.w3c.dom.*;
import org.xml.sax.InputSource;


public class database_tmm_man 

{

    Connection conn= null;
    String strEgnin;
    String strFname;
    String strMname;
    String strLname;
    String strNumtel;
    String strEgndel;

public String SelectFromDB()
{

    String strXml = "<response>";


        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try 
        {
            conn= DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/plovdivbizlocator", "postgres", "********");
        } 

        catch (SQLException ex) 
        {

            ex.printStackTrace();
        }

        Statement mystmt = null;
        String selectQuery = "SELECT egn, firstname, middlename, lastname,telnumber FROM tanya_students";
        try 
        {
            mystmt = conn.createStatement();
            ResultSet mysr = mystmt.executeQuery(selectQuery);  

            ResultSetMetaData rsmd = mysr.getMetaData(); 
            int colCount = rsmd.getColumnCount();
                int numberrow=0;

            strXml += "<rows>";
            while (mysr.next())
            {  
                strXml += "<row id= '"+numberrow +"'>";
                for (int i=1;i<=colCount;i++) 
                {
                    String elementname = rsmd.getColumnName(i);
                    String elementValue=mysr.getString(i);
                    strXml += "<colunm colname='"+elementname+"' colvalue='"+elementValue+"'/>";
                }                   
                numberrow++;
                strXml +="</row>";
            }
            strXml += "</rows>";                

        }
        catch(Exception ex)
        {}
        strXml+="</response>";

        return strXml;


}


public String InsertIntoDB (String data) throws ClassNotFoundException
{

    try
        {   String strDecodedData = URLDecoder.decode(data);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
            Document doc = dbBuilder.parse(new InputSource(new ByteArrayInputStream(strDecodedData.getBytes("utf-8"))));
            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName("student");
            int num=nList.getLength();
            for (int i=0;i<num;i++)
                        {       
                            Element section=(Element)nList.item(i);                     
                            strEgnin = section.getElementsByTagName("egn").item(0).getChildNodes().item(0).getNodeValue();              
                            strFname = section.getElementsByTagName("fname").item(0).getChildNodes().item(0).getNodeValue();             
                            strMname = section.getElementsByTagName("mname").item(0).getChildNodes().item(0).getNodeValue();            
                            strLname = section.getElementsByTagName("lname").item(0).getChildNodes().item(0).getNodeValue();                
                            strNumtel =section.getElementsByTagName("telnum").item(0).getChildNodes().item(0).getNodeValue();

                        }
        }

    catch (Exception ex1)
         {

         }

    Connection conn= null;

    Class.forName("org.postgresql.Driver");
    try 
    {
        conn= DriverManager.getConnection("jdbc:postgresql:/127.0.0.1:5432/plovdivbizlocator", "postgres", "********");
    } 

    catch (SQLException ex) 
        {

            ex.printStackTrace();
        }

    PreparedStatement pstmt = null;
    String insertQuery= "insert into tanya_students (egn, firstname, middlename, lastname, telnumber)";
    insertQuery +="values (?, ?, ?, ?, ?)";
    try
        {
            pstmt=conn.prepareStatement(insertQuery);
            pstmt.setString(1,strEgnin);
            pstmt.setString(2,strFname);
            pstmt.setString(3,strMname);
            pstmt.setString(4,strLname);
            pstmt.setString(5,strNumtel);
            pstmt.executeUpdate();
        }

    catch (Exception ex)
        {

        }

    return "" ;
}
public String DeleteFromDB(String deleteID) 
{
    try
    {   String strDecodeddelID = URLDecoder.decode(deleteID);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
        Document doc = dbBuilder.parse(new InputSource(new ByteArrayInputStream(strDecodeddelID.getBytes("utf-8"))));
        doc.getDocumentElement().normalize();

        NodeList nList = doc.getElementsByTagName("student");
        int num=nList.getLength();
        for (int i=0;i<num;i++)
                    {       
                        Element section=(Element)nList.item(i);                     
                        strEgndel = section.getElementsByTagName("egn").item(0).getChildNodes().item(0).getNodeValue();                 


                    }
    }

catch (Exception ex1)
     {

     }






  Connection conn= null;

    try 
    {
        Class.forName("org.postgresql.Driver");
    } 

    catch (ClassNotFoundException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try 
    {
        conn= DriverManager.getConnection("jdbc:postgresql:/127.0.0.1:5432/plovdivbizlocator", "postgres", "********");
    } 

    catch (SQLException ex) 
        {

            ex.printStackTrace();
        }

    PreparedStatement delstmt = null;
    String delQuery= "DELETE FROM tanya_students WHERE egn= ?";
    try
        {
            delstmt=conn.prepareStatement(delQuery);
            delstmt.setString(1,strEgndel);
            delstmt.executeUpdate();
        }

    catch (Exception ex)
        {

        }

    return "" ;

}




}

就在我单击每行末尾的按钮时尝试删除记录时,什么也没有发生。提及我为我的 row.id 选择数据库的第一列 - egn 是唯一的(没有人具有相同的 egn)可能很有用。这就是为什么我将这个egn发送到服务器和我的网络服务在无效的主程序中一切正常工作poerfect@\所以我认为不知何故我没有正确地将数据发送到我的服务器

与插入相同-当我在要插入数据库的文本框中键入一些数据时,当我单击插入按钮时,没有任何反应,感谢您提前提供帮助

4

1 回答 1

0

你确定你的 Ajax 调用是正确的还是根本没有?由于您使用原始 Javascript 构建调用,您是否在多个浏览器中对其进行了测试?有一些很好的 Chrome 扩展允许您手动构建 REST 调用,以测试 Web 服务是否在浏览器文档上下文之外正常工作。(我使用了一个名为 Graphic HHTP 客户端的程序,但我认为它仅适用于 Mac)。

这样做至少可以帮助您确定问题是服务器端还是客户端。

于 2013-01-02T14:07:48.117 回答