How to get some data from a restful web service, and save it to the database?

nastaran :

I have written some code to save some data into database by restful web services, utilizing SOAPUI as user interface. I use @put to do that. Here is the steps of running:

1- run the code on Tomcat 9.0 server. 2- use the URL in SOAPUI to PUT some data.

But when I use PUT in SOAPUI, give the string values of first and last, and run it, the values are not added to my database. Hoewer, the json file got the right values

{
   "first": "Jon",
   "last": "Snow"
}

Here is the important pieces of my code:

package tomcat.rest.eclipse.database;

public class Score {

    public static String first, last;
}

public class ScoreService {
@PUT
    @Path("/score")
    @Produces("application/json")
    public String update(@QueryParam("first") String first, 
                             @QueryParam("last") String last) {
                    Score.first = first;
                    Score.last = last;

                    final String var1 = Score.first;
                    final String var2 = Score.last;
                    database.insert(var1, var2);

                    String pattern = "{ \"first\":\"%s\", \"last\":\"%s\"}";
                    return String.format(pattern, Score.first, Score.last);
    }
}

And this is my connection :

public class database {

public static Connection getConnection() {

            try {
                String driver = "com.mysql.jdbc.Driver";
                String url = "jdbc:mysql://localhost:3306/testdb";
                String username = "root";
                String password = "00000";
                Class.forName(driver);
                Connection conn = DriverManager.getConnection(url, username, password);
                System.out.println("Connected");
                return conn;
            } catch(Exception e) {System.out.println(e);}

            return null;    
            }

public static void insert(final String var1, final String var2) {

        try {
            Connection con = getConnection();
            PreparedStatement posted = con.prepareStatement("INSERT INTO student (first, last) VALUES ('"+var1+"', '"+var2+"')");
            posted.executeUpdate();
        } catch(Exception e) {System.out.println(e);}
        finally {
            System.out.println("Insert completed");
        }
    }
}

The output on console is:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver java.lang.ClassNotFoundException: com.mysql.jdbc.Driver java.lang.NullPointerException Insert completed

How can I connect the web service to database properly to save some records in database? Thank you so much for helping me.

Medo :

Just download and add the mysql connector jar to your project build path.

Or

Add the jar file to your WEB-INF/lib folder. Right-click your project in Eclipse, and go to "Build Path > Configure Build Path" Add the "Web App Libraries" library This will ensure all WEB-INF/lib jars are included on the classpath.

Or the jdbc mysql maven dependency if you use maven :

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
</dependency>

Edit: right click on your project => Build Path => Configure Build Path => tab "Libraries" and you click on "Add External JARs" and you point to your file "mysql-connector-java-5.1.17-bin.jar"

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.EmptyStackException;

public class SingletonConnection   {


    private static Connection connection = null ;
    static 
    {
        try{
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection
                    ("jdbc:mysql://localhost:3306/dbnameX","root","");
        }catch(Exception ex)
        {

        }

    }
    public static Connection getConnection() throws Exception {
        return connection;
    }


}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=162576&siteId=1