简体   繁体   中英

Unable to update Mysql Database in java dynamic web project (Eclipse,Tomcat)

This is my FeedbackListner Class.I need to update my "test " table by using the information user send as the feedback to the system.Table contains 4 columns.Name,Location,Wage and Telephone.It execute mtSmsReq.setMessage("Thank you for your feedback, Hope to see you again"); In the simulator,Also shows the Feedback message. But database is not updating.Is there any error in my code?

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



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

I have to change the code in this way..

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

Because I should create an instance before get connection :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM