我创建了一个处理数据库并从中选择数据的代码,它可以正常工作并获取数据 这个代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection conn = null;
Statement statement = null;
String SelectQuiry = "SELECT `name`, `email`, `birtdate` FROM `table1` WHERE id = 1";
try{
conn = DriverManager.getConnection(Con_String, Username, Passsword);
statement = conn.createStatement(); // create statement
//statement.executeQuery(SelectQuiry); // execute statement
ResultSet RS = statement.executeQuery(SelectQuiry); // execute statement
while(RS.next())
{
String name = RS.getString("name");
String email = RS.getString("email");
String birthday = RS.getString("birtdate");
);
}
RS.close();
}catch(Exception e)
{
}
finally
{
if(statement != null)
{conn.close();}
}
PrintWriter out = response.getWriter();
out.println(HTML_START + "<h2>Hi There!</h2><br/>"
+ "<h3>Name "+ name +"</h3> <br/>"
+ "<h3>email "+ email +"</h3> <br/>"
+ "<h3>Ubd "+ birthday +"</h3> <br/>"
+HTML_END);
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
但是当我将这些代码执行到方法中并在doget中调用此方法时,它总是返回null 这是返回null的代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DbMangement dbmangement = new DbMangement();
try {
String[] person = dbmangement.SelectData();
Username = person[0];
email = person[1];
birthday = person[2];
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintWriter out = response.getWriter();
out.println(HTML_START + "<h2>Hi There!</h2><br/>"
+ "<h3>Name "+ Username +"</h3> <br/>"
+ "<h3>email "+ email +"</h3> <br/>"
+ "<h3>Ubd "+ birthday +"</h3> <br/>"
+HTML_END);
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
这是另一个班级
public class DbMangement {
private static final String Username = "root";
private static final String Passsword = "";
private static final String Con_String ="jdbc:mysql://localhost/testdb";
public String[] SelectData() throws SQLException
{
Connection conn = null;
Statement statement = null;
String[] Person = null;
//String SqlQuiry = "INSERT INTO table1 VALUES (1 , 'ahmed' , 'mohamed' , '12/2/2014') ";
String SelectQuiry = "SELECT `name`, `email`, `birtdate` FROM `table1` WHERE id = 1";
try{
conn = DriverManager.getConnection(Con_String, Username, Passsword);
//System.out.println("connected");
statement = conn.createStatement(); // create statement
//statement.executeQuery(SelectQuiry); // execute statement
ResultSet RS = statement.executeQuery(SelectQuiry); // execute statement
//System.out.println("Executed !!");
while(RS.next())
{
Person[0] = RS.getString("name");
Person[1] = RS.getString("email");
Person[2] = RS.getString("birtdate");
//System.out.print("Name is "+UUsername +" Email Is : " +Uemail+ "bd is :" + Ubirthday);
}
RS.close();
}catch(Exception e)
{
System.err.println(e);
return null;
}
finally
{
if(statement != null)
{conn.close();
return Person;
}
}
return Person;
}
有任何建议请解决这个问题吗?
由于
答案 0 :(得分:0)
看起来Person对象永远不会被初始化(例如:Person = new String[3]
)。您只需将Person赋值为null,然后将其返回:
String[] Person = null;
...
while(RS.next()) {
Person[0] = RS.getString("name");
Person[1] = RS.getString("email");
Person[2] = RS.getString("birtdate");
//System.out.print("Name is "+UUsername +" Email Is : " +Uemail+ "bd is :" + Ubirthday);
}
...
return Person;