如何从 restful 网络服务中获取一些数据,并将其保存到数据库中?
How to get some data from a restful web service, and save it to the database?
我已经编写了一些代码,通过 restful Web 服务将一些数据保存到数据库中,使用 SOAPUI 作为用户界面。我使用@put 来做到这一点。下面是运行ning的步骤:
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;
}
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");
}
}
}
控制台上的输出是:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
java.lang.NullPointerException
Insert completed
如何将网络服务正确连接到数据库以在数据库中保存一些记录?非常感谢你帮助我。
只需下载 mysql connector jar 并将其添加到您的项目构建路径。
或
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.
或者 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"
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;
}
}
我已经编写了一些代码,通过 restful Web 服务将一些数据保存到数据库中,使用 SOAPUI 作为用户界面。我使用@put 来做到这一点。下面是运行ning的步骤:
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;
}
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");
}
}
}
控制台上的输出是:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver java.lang.ClassNotFoundException: com.mysql.jdbc.Driver java.lang.NullPointerException Insert completed
如何将网络服务正确连接到数据库以在数据库中保存一些记录?非常感谢你帮助我。
只需下载 mysql connector jar 并将其添加到您的项目构建路径。
或
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.
或者 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"
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;
}
}