连接到SQLServer时无法在java中运行restfull webservice
我试图调用一个web服务,该服务以json格式从我的SQLServer数据库返回数据 这是我从SQLServer获取数据并将其转换为json的代码。 这里,函数连接到SQLServer时无法在java中运行restfull webservice,java,sql-server,json,web-services,jdbc,Java,Sql Server,Json,Web Services,Jdbc,我试图调用一个web服务,该服务以json格式从我的SQLServer数据库返回数据 这是我从SQLServer获取数据并将其转换为json的代码。 这里,函数getAllDataJson()返回结果的字符串值。 当我调用它显示为时,它工作正常 SqlDatabase db = new SqlDatabase(); System.out.println(db.getAllDataJson); 但是当我从一个webservice调用它时,它不工作(这个webservice配置也很好,我使用这个w
getAllDataJson()
返回结果的字符串值。
当我调用它显示为时,它工作正常
SqlDatabase db = new SqlDatabase();
System.out.println(db.getAllDataJson);
但是当我从一个webservice调用它时,它不工作(这个webservice配置也很好,我使用这个webservice返回json字符串,然后它工作正常)
如果我将这两者结合起来,则函数getAllData()
中会显示错误(在下面的返回代码中)
在生产线上:
rs = stmt.executeQuery("select * from persons");
它正在显示nullPointerException
如果将其作为java应用程序运行,则不会出现相同的错误,只有在webservice上运行时才会出现
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
public class SqlDatabase {
private static final String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
"databaseName=FIRST;integratedSecurity=true;";
Connection con = null;
Statement stmt = null;
private void connectToDb(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
stmt = con.createStatement();
} catch (SQLException e) {
System.out.println("error occured at Database Connection");
} catch (ClassNotFoundException e) {
System.out.println("Class not found");
}
}
void closeDb(){
try{
if(con != null){con.close();}
if(stmt!=null){stmt.close();}
}catch(SQLException e){
System.out.println("error occured while closing Database Connection");
}
}
public ResultSet getAllData(){
ResultSet rs = null;
connectToDb();
try {
rs = stmt.executeQuery("select * from persons");
} catch (SQLException e) {
System.out.println("error occured while getting data");
}
return rs;
}
public String getAllDataJson() throws JSONException{
ResultSet rs = getAllData();
if(rs == null){return null;}
JSONArray jArray = new JSONArray();
JSONObject json = null;
//data to json
try {
while(rs.next()){
for(int i = 1;i<=rs.getMetaData().getColumnCount();i++){
json = new JSONObject();
json.put(rs.getMetaData().getColumnName(i), rs.getString(i));
}
jArray.put(json);
}
} catch (SQLException e) {
System.out.println("near Json");
}
return jArray.toString();
}
}
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.sql.Statement;
导入org.codehaus.jettison.json.JSONArray;
导入org.codehaus.jettison.json.JSONException;
导入org.codehaus.jettison.json.JSONObject;
公共类SqlDatabase{
私有静态最终字符串connectionUrl=“jdbc:sqlserver://localhost:1433;" +
“databaseName=FIRST;integratedSecurity=true;”;
连接con=null;
语句stmt=null;
私有void connectToDb(){
试一试{
Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);
con=DriverManager.getConnection(connectionUrl);
stmt=con.createStatement();
}捕获(SQLE异常){
System.out.println(“数据库连接时出错”);
}catch(classnotfounde异常){
System.out.println(“未找到类”);
}
}
void closeDb(){
试一试{
如果(con!=null){con.close();}
如果(stmt!=null){stmt.close();}
}捕获(SQLE异常){
System.out.println(“关闭数据库连接时出错”);
}
}
公共结果集getAllData(){
结果集rs=null;
connectToDb();
试一试{
rs=标准执行程序(“从人员中选择*);
}捕获(SQLE异常){
System.out.println(“获取数据时出错”);
}
返回rs;
}
公共字符串getAllDataJson()抛出JSONException{
结果集rs=getAllData();
如果(rs==null){returnnull;}
JSONArray jArray=新的JSONArray();
JSONObject json=null;
//将数据转换为json
试一试{
while(rs.next()){
对于(inti=1;i请参见数据库主机URLdbc:sqlserver://localhost:1433
它表示localhost
。这意味着它将始终检查安装在执行它的同一台机器上的DB server
检查数据库服务器和web服务器是否安装在同一台计算机上。如果不是,请改用localhost,最好使用IP地址。将localhost替换为正在运行的数据库服务器的IP
假设运行DB server的系统的IP为182.10.10.45
dbc:sqlserver://182.10.10.45:1433
您正在吞咽异常,并且仍然尝试使用连接。至少记录发生的SQLException
(或使用e.printStackTrace()
),以便我们知道出了什么问题。