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

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

本文介绍了使用java将参数从flex传递到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从flex(前端)插入值到数据库(后端)时,我遇到了一个问题。blazeds连接没有问题。我想问题是与java code.Anyway粘贴java和flex代码。请让我知道如何将值插入到数据库中。我在java中不太好,所以我尝试了很多解决这个问题。



我的Flex代码是:

 < fx:Script> 
import mx.controls.Alert;
导入mx.rpc.events.FaultEvent;
导入mx.rpc.events.ResultEvent;
导入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:声明>
destination =helloworldname
result =result(event)
fault =fault(event)/>
< / fx:声明>

y =112
width =414
height =325
title = FeedBack
borderColor =#008040
fontWeight =bold
fontSize =13>

$ b id =senfeddback
click =processSendFeedback()
x =178
y =246
height =22
width =86
cornerRadius =12/>

< s:标签x =50y =142text =Feedback/>

< s:TextInput id =number_txtx =119y =100width =260/>

< / s:面板>


我的Java代码是:

  public class HelloWorldName 
{

public static void main(String [] argv)
{
System.out.println(-------- PostgreSQL+
JDBC连接测试----------);
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 = DriverManager.getConnection(
jdbc:postgresql://+ host +:+ port +/+ dbName,postgres ,admin);

System.out.println(数据库已连接);
String strSQL =insert into feedback(name,mobile_num,mail,feedback)values('+ aName +','+ aNumber +','+ aMail +','+ aFeedback + ');
语句st = connection.createStatement();
int a = st.executeUpdate(strSQL);
System.out.println(hi,query executed);

catch(Exception e)
{
System.out.println();
}
返回Hello from Java,+ aName +,+ aMail +,+ aNumber +,+ aFeedback +;




$ b我想我在Main中发送null值代码。但是,如果我尝试添加aName,aNumber,aMail,afeedback中的空值错误的地方显示。是否有任何方法来解决这个问题,或有任何网站,以帮助插入值到数据库使用java,blazeds 。请帮助我。



在此先感谢。

解决方案

根据定义


$ b 远程处理服务允许客户端应用程序访问 Java / oops 静态方法中, 关联到 Object /实例它依赖于/关联到类

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

pre $ public $ 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



希望工作

I am facing one problem when am trying to insert values from flex(frontend) to database(backend).No problem in blazeds connection.I think problem is with java code.Anyway am pasting both java and flex code.Please let me know how to insert values into database.I am not well in java.So i tried a lot to solve this issue.

My Flex code is:

<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>

My Java Code is:

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 +"";
}
}

I think i am sending null values in Main in java code.But if i try to add aName,aNumber,aMail,aFeedback in the place of null values error is showing.Is there any way to get solve this issue or is there any site to help to insert values into db using java,blazeds.Please help me.

Thanks in Advance.

解决方案

I think problem is JAVA static method according to Remoting Service definition

The Remoting Service lets a client application access the methods of server-side Java objects

and in java/oops static methods are not associated to Object/instance its depends-upon/associated to class

your method should be like this to accept call from flex

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

and to call it in main(java main) use following lines

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

Here is Flash-Builder BlazeDS-Remoting sample

hopes that works

这篇关于使用java将参数从flex传递到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 21:14