简体   繁体   English

如何从宁静的Web服务获取一些数据并将其保存到数据库?

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

I have written some code to save some data into database by restful web services, utilizing SOAPUI as user interface. 我编写了一些代码,以利用SOAPUI作为用户界面,通过宁静的Web服务将一些数据保存到数据库中。 I use @put to do that. 我使用@put来做到这一点。 Here is the steps of running: 这是运行步骤:

1- run the code on Tomcat 9.0 server. 1-在Tomcat 9.0服务器上运行代码。 2- use the URL in SOAPUI to PUT some data. 2-使用SOAPUI中的URL放置一些数据。

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. 但是,当我在SOAPUI中使用PUT时,给出first和last的字符串值并运行它,这些值不会添加到我的数据库中。 Hoewer, the json file got the right values Hoewer,json文件的值正确

{
   "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 java.lang.ClassNotFoundException:com.mysql.jdbc.Driver java.lang.ClassNotFoundException:com.mysql.jdbc.Driver java.lang.NullPointerException插入完成

How can I connect the web service to database properly to save some records in database? 如何将Web服务正确连接到数据库以在数据库中保存一些记录? Thank you so much for helping me. 非常感谢您对我的帮助。

Just download and add the mysql connector jar to your project build path. 只需下载并将mysql连接器 jar添加到您的项目构建路径即可。

Or 要么

Add the jar file to your WEB-INF/lib folder. 将jar文件添加到您的WEB-INF / lib文件夹中。 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. 在Eclipse中右键单击您的项目,然后转到“构建路径>配置构建路径”。添加“ Web App Libraries”库。这将确保所有WEB-INF / lib jar都包含在类路径中。

Or the jdbc mysql maven dependency if you use maven : 或jdbc mysql maven依赖项(如果使用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" 编辑:右键单击项目=>构建路径=>配置构建路径=>选项卡“库”,然后单击“添加外部JAR”,然后指向文件“ mysql-connector-java-5.1.17-bin”。罐”

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


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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