Java JDBC类的端口): protectedvoid doGet(HttpServletRequest请求,HttpServletResponse响应) 抛出ServletException、IOException{ String DB_URL=“jdbc:mysql://localhost:3306/salesteam"; 字符串USER=“root”; 字符串PASS=“1234”; Sql2o Sql2o=新Sql2o(DB_URL、用户、通行证); 字符串sql= 选择empID,fName+ “来自销售团队”+ “其中empID=1”; 列出人员名单; try(连接con=sql2o.open()){ people=con.createQuery(sql).executeAndFetch(Person.class); } //在这里生成json的代码。。。 }
您的代码是普通jdbc和sql2o代码的混合体。使用sql2o时,不应手动读取结果集。事实上,您的代码在尝试从ResultSet读取时会抛出NullPointerException 它不编译的原因是,您已经导入了java.sql.Connection,而Sql2o.open()方法返回org.Sql2o.Connection。我认为,如果只删除每个引用JDBC类,对您来说会更容易 您的方法应该如下所示(请记住删除所有JDBC类的导入):Java JDBC类的端口): protectedvoid doGet(HttpServletRequest请求,HttpServletResponse响应) 抛出ServletException、IOException{ String DB_URL=“jdbc:mysql://localhost:3306/salesteam"; 字符串USER=“root”; 字符串PASS=“1234”; Sql2o Sql2o=新Sql2o(DB_URL、用户、通行证); 字符串sql= 选择empID,fName+ “来自销售团队”+ “其中empID=1”; 列出人员名单; try(连接con=sql2o.open()){ people=con.createQuery(sql).executeAndFetch(Person.class); } //在这里生成json的代码。。。 },java,servlets,jdbc,import,sql2o,Java,Servlets,Jdbc,Import,Sql2o,您的代码是普通jdbc和sql2o代码的混合体。使用sql2o时,不应手动读取结果集。事实上,您的代码在尝试从ResultSet读取时会抛出NullPointerException 它不编译的原因是,您已经导入了java.sql.Connection,而Sql2o.open()方法返回org.Sql2o.Connection。我认为,如果只删除每个引用JDBC类,对您来说会更容易 您的方法应该如下所示(请记住删除所有JDBC类的导入): protectedvoid doGet(HttpServl
protectedvoid doGet(HttpServletRequest请求,HttpServletResponse响应)
抛出ServletException、IOException{
String DB_URL=“jdbc:mysql://localhost:3306/salesteam";
字符串USER=“root”;
字符串PASS=“1234”;
Sql2o Sql2o=新Sql2o(DB_URL、用户、通行证);
字符串sql=
选择empID,fName+
“来自销售团队”+
“其中empID=1”;
列出人员名单;
try(连接con=sql2o.open()){
people=con.createQuery(sql).executeAndFetch(Person.class);
}
//在这里生成json的代码。。。
}
我需要花一些时间学习Javadoc,谢谢你的更新。我需要花一些时间学习Javadoc,谢谢你的更新。
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.sql2o.Sql2o;
import com.google.gson.Gson;
import com.models.Person;
import com.tools.DBUtil;
import com.tools.DataTable;
/**
* Servlet implementation class SalesTeam
*/
@WebServlet(name = "SalesTeam_Table", urlPatterns = { "/json/table/salesteam" })
public class Table_SalesTeam extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Table_SalesTeam() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Creating an arraylist based on the Person model in com.models
ArrayList<Person> people = new ArrayList<Person>();
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
String DB_URL = "jdbc:mysql://localhost:3306/salesteam";
String USER = "root";
String PASS = "1234 ";
Sql2o sql2o = new Sql2o(DB_URL, USER, PASS);
String sql =
"SELECT empID, fName " +
"FROM salesteam " +
"WHERE empID = 1";
//issue is here...
try (Connection con = sql2o.open()){
people = con.createQuery(sql).executeAndFetch(Person.class);
////////////////////////
//Selecting every record in sales_team table
//pst = conn.prepareStatement("select f_Name, l_Name, email, contactNum from sales_team ORDER BY f_Name ASC ");
//rs = pst.executeQuery();
while (rs.next()) {
//Create a person object and fill fields
Person p = new Person();
p.setfName(rs.getString("f_Name"));
p.setlName(rs.getString("l_Name"));
p.setEmail(rs.getString("email"));
p.setContact(rs.getString("contactNum"));
//Add person to people array list
people.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
//Must close the database objects
DBUtil.close(conn, pst, rs);
}
//Gson Library was added to WEB-INF/lib
//Creating a new gson object to hold json
Gson gson = new Gson();
//Create a custom DataTable object that takes an ArrayList<Object> and makes an object
//Used to create correct datatable json formatting
DataTable table = new DataTable();
table.setData(people);
//Creating string by converting people arraylist to json string with gson object
//Read up on gson library for json at http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
String json = gson.toJson(table);
//Uncomment line below and look in console for what json looks like
System.out.println(json);
//Write json string to response
PrintWriter out = response.getWriter();
out.print(json);
out.flush();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String DB_URL = "jdbc:mysql://localhost:3306/salesteam";
String USER = "root";
String PASS = "1234 ";
Sql2o sql2o = new Sql2o(DB_URL, USER, PASS);
String sql =
"SELECT empID, fName " +
"FROM salesteam " +
"WHERE empID = 1";
List<Person> people;
try (Connection con = sql2o.open()){
people = con.createQuery(sql).executeAndFetch(Person.class);
}
// code to generate json here...
}