如何从一个restful web服务中获取一些数据,并将其保存到数据库中?

我已经编写了一些代码,使用 SOAPUI 作为用户界面,通过宁静的 Web 服务将一些数据保存到数据库中。我使用@put 来做到这一点。下面是运行步骤:


1- 在 Tomcat 9.0 服务器上运行代码。2- 使用 SOAPUI 中的 URL 来放置一些数据。


但是当我在 SOAPUI 中使用 PUT 时,给出 first 和 last 的字符串值并运行它,这些值不会添加到我的数据库中。Hoewer,json 文件获得了正确的值


{

   "first": "Jon",

   "last": "Snow"

}

这是我的代码的重要部分:


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

    }

}

这是我的连接:


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;    

            }


控制台上的输出是:


java.lang.ClassNotFoundException: com.mysql.jdbc.Driver java.lang.ClassNotFoundException: com.mysql.jdbc.Driver java.lang.NullPointerException 插入完成


如何将 Web 服务正确连接到数据库以将一些记录保存在数据库中?非常感谢你帮助我。


杨魅力
浏览 96回答 1
1回答

慕斯王

只需下载mysql 连接器jar并将其添加到您的项目构建路径。或者将 jar 文件添加到您的 WEB-INF/lib 文件夹中。在 Eclipse 中右键单击您的项目,然后转到“Build Path > Configure Build Path” 添加“Web App Libraries”库 这将确保所有 WEB-INF/lib jar 都包含在类路径中。或者 jdbc mysql maven 依赖项,如果你使用 maven :<dependency> &nbsp;&nbsp;&nbsp;&nbsp;<groupId>mysql</groupId> &nbsp;&nbsp;&nbsp;&nbsp;<artifactId>mysql-connector-java</artifactId> &nbsp;&nbsp;&nbsp;&nbsp;<version>5.1.6</version> </dependency>编辑:右键单击您的项目 => 构建路径 => 配置构建路径 => 选项卡“库”,然后单击“添加外部 JAR”并指向文件“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&nbsp; &nbsp;{&nbsp; &nbsp; private static Connection connection = null ;&nbsp; &nbsp; static&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Class.forName("com.mysql.jdbc.Driver");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; connection = DriverManager.getConnection&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ("jdbc:mysql://localhost:3306/dbnameX","root","");&nbsp; &nbsp; &nbsp; &nbsp; }catch(Exception ex)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static Connection getConnection() throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; return connection;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java