java - 使用java将参数从flex传递到数据库

标签 java apache-flex postgresql flex4 blazeds

当我尝试将值从flex(前端)插入到数据库(后端)时,我遇到一个问题。blazeds连接没有问题。我认为问题出在java代码上。无论如何,我都粘贴java和flex代码。请让我知道如何将值插入数据库。我不太擅长java。所以我尝试了很多方法来解决这个问题。

我的 Flex 代码是:

<fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.rpc.events.FaultEvent;
        import mx.rpc.events.ResultEvent;
        import mx.utils.ObjectUtil;
        private function processSendFeedback():void
        {
            ro.getHelloByName(name_txt.text,email_txt.text,number_txt.text,fb_txt.text);
        }
        private function result(event:ResultEvent):void
        {
            Alert.show(ObjectUtil.toString(event.result));
        }
        private function fault(event:FaultEvent):void
        {
            Alert.show(ObjectUtil.toString(event.fault));
        }
    ]]>
</fx:Script>

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
    <s:RemoteObject id="ro"
                    destination="helloworldname"
                    result="result(event)"
                    fault="fault(event)"/>
</fx:Declarations>

<s:Panel x="176"
         y="112"
         width="414"
         height="325"
         title="FeedBack"
         borderColor="#008040"
         fontWeight="bold"
         fontSize="13">


    <s:Button label="Submit"
              id="senfeddback"
              click="processSendFeedback()" 
              x="178" 
              y="246" 
              height="22" 
              width="86" 
              cornerRadius="12"/>

    <s:Label x="61" y="38" text="Name"/>
    <s:Label x="61" y="72" text="Email"/>
    <s:Label x="61" y="105" text="Number"/>
    <s:Label x="50" y="142" text="Feedback"/>

    <s:TextInput id="name_txt" x="119" y="30" width="260"/>
    <s:TextInput id="email_txt" x="119" y="65" width="260"/>
    <s:TextInput id="number_txt" x="119" y="100" width="260"/>
    <s:TextInput id="fb_txt" x="119" y="134" width="260" height="104"/>

</s:Panel>

我的Java代码是:

public class HelloWorldName 
{ 

public static void main(String[] argv) 
{
    System.out.println("-------- PostgreSQL " +
            "JDBC Connection Testing ----------");
    getHelloByName(null, null, null, null);
}
public static String getHelloByName(String aName,String aMail,String aNumber,String aFeedback)
{
        String host = "localhost";
        String port = "1234";
        String dbName = "test";
        Connection connection = null;
    try 
    {
        connection = DriverManager.getConnection(
                "jdbc:postgresql://" + host + ":" + port + "/" + dbName,"postgres", "admin");

        System.out.println("Database is connected");    
     String strSQL = "insert into feedback(name,mobile_num,mail,feedback) values ('" + aName + "','" + aNumber +"','" + aMail +"','" + aFeedback +"')";
     Statement st = connection.createStatement();
     int a=st.executeUpdate(strSQL);
     System.out.println("hi,query executed");
    }
    catch(Exception e)
    {
        System.out.println();
    }
    return "Hello from Java, " + aName + "," + aMail +"," + aNumber +"," + aFeedback +"";
}
}

我认为我在java代码中的Main中发送空值。但是如果我尝试在空值的位置添加aName,aNumber,aMail,aFeedback,则会显示错误。有什么方法可以解决这个问题吗?有任何网站可以帮助使用 java、blazeds 将值插入数据库。请帮助我。

提前致谢。

最佳答案

我认为问题是JAVA静态方法,根据Remoting Service定义

远程服务允许客户端应用程序访问服务器端 Java 对象的方法

并且在java/oops中静态方法关联到对象/实例,其依赖于/与类关联

你的方法应该像这样接受来自flex的调用

public String getHelloByName(String aName,String aMail,String aNumber,String aFeedback)

并在 main(java main) 中调用它,使用以下行

HelloWorldName helloWorldName = new HelloWorldName();
helloWorldName.getHelloByName(null, null, null, null);

这是 Flash-Builder BlazeDS-Remoting sample

希望有效

关于java - 使用java将参数从flex传递到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6042076/

相关文章:

apache-flex - 不支持加密本地存储

java - 使用csv数据作为字符串java

java - 在Java中包装Selenium wait.until函数,函数作为参数

java - 当 quartz 作业触发时,它是一个新的作业类实例吗?

apache-flex - 使用 ActionScript 3 修剪字符串

android - 启动时的 Flex Mobile Android 全屏应用程序

php - 向表中插入表单数据

postgresql - 将 Postgresql 表迁移到 timescale 数据库

c# - NpgSQL 抛出异常

Java并行流使用与否