无法在Java动态Web项目(Eclipse,Tomcat)中更新Mysql数据库

Nadeesha Thilakarathne

这是我的FeedbackListner类。我需要使用用户发送给系统的反馈信息来更新“测试”表。该表包含4列。名称,位置,工资和电话mtSmsReq.setMessage("Thank you for your feedback, Hope to see you again");。它在模拟器中执行,还显示反馈信息。但是数据库没有更新,我的代码有错误吗?

import hms.kite.samples.api.SdpException;
import hms.kite.samples.api.sms.MoSmsListener;
import hms.kite.samples.api.sms.SmsRequestSender;
import hms.kite.samples.api.sms.messages.MoSmsReq;
import hms.kite.samples.api.sms.messages.MtSmsReq;
import hms.kite.samples.api.sms.messages.MtSmsResp;

import java.net.MalformedURLException;
import java.net.URL;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Arrays;

import com.mysql.jdbc.Connection;

public class FeedbackListner implements MoSmsListener {

    @Override
    public void init() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onReceivedSms(MoSmsReq moSmsReq) {

        String message = moSmsReq.getMessage();
        String sourceAddress = moSmsReq.getSourceAddress();

        FeedbackService.addFeedback(sourceAddress, message);

        try {
            sendResponse(moSmsReq);

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

    }

    private void sendResponse(MoSmsReq moSmsReq) throws SdpException {

        try {
             Connection conn = null;
              String url = "jdbc:mysql://localhost:3306/";
              String dbName = "tadhack";
              String driver = "com.mysql.jdbc.Driver";
              String userName = "root"; 
              String password = "";

              String string =  moSmsReq.getMessage();
              String[] parts = string.split("/");
              String Name = parts[0]; 
              String Location = parts[1];
              String Wage = parts[2];


              String Telephone =moSmsReq.getSourceAddress();;


                  conn = (Connection) DriverManager.getConnection(url+dbName,userName,password);
                  try {



                      Class.forName(driver).newInstance();

                      PreparedStatement ps = conn
                                .prepareStatement("insert into test values(?,?,?,?)");

                        ps.setString(1, Name);
                        ps.setString(2, Location);
                        ps.setString(3, Wage);
                        ps.setString(4, Telephone);



                        int i = ps.executeUpdate();
                        if (i > 0)
                           System. out.print("You are successfully registered...");

                    } catch (Exception e2) {
                        System.out.println(e2);
                    }


                  conn.close();
              } catch (Exception e) {
                  e.printStackTrace();
              }
                    MtSmsReq mtSmsReq = new MtSmsReq();
                    mtSmsReq.setDestinationAddresses(Arrays.asList(moSmsReq.getSourceAddress()));
                    mtSmsReq.setMessage("Thank you for your feedback, Hope to see you again");
                    mtSmsReq.setApplicationId("App_0001");
                    mtSmsReq.setPassword("password");


            SmsRequestSender requestSender = null;
            try {
                requestSender = new SmsRequestSender(new URL("http://localhost:7000/sms/send"));
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            MtSmsResp smsResp = requestSender.sendSmsRequest(mtSmsReq);

            System.out.println("Response " + smsResp);



}}
Nadeesha Thilakarathne
 conn = (Connection) DriverManager.getConnection(url+dbName,userName,password);
              try {
                   Class.forName(driver).newInstance();

我必须以这种方式更改代码。

              try {
                   Class.forName(driver).newInstance();
                   conn = (Connection)DriverManager.getConnection(url+dbName,userName,password);

因为我应该在获得连接之前创建一个实例:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档