Home > Back-end >  Java EE database connection using "rs" is null, please?
Java EE database connection using "rs" is null, please?

Time:05-13

Java. Lang. NullPointerException: always invoke a "Java. SQL. The ResultSet. Next ()" because "rs" is null
 & lt; % @ page language="Java" contentType="text/HTML. Charset=ISO - 8859-1 "
PageEncoding="ISO - 8859-1" % & gt;
<% @ page import="Java. SQL. *" % & gt;
<% @ page import="qwer. DbBean" % & gt;



Verify the page & lt;/title> <br/><meta HTTP - equiv="content-type" Content="text/HTML. Charset=utf-8 "& gt; <br/><meta charset="ISO - 8859-1" & gt; <br/><title> Insert the title here

<body>
<%
The String with username1 is=request. The getParameter (" username ");
String password1=request. The getParameter (" password ");
Boolean validated=false;
Boolean validated1=false;
The Connection conn=null;
The Statement Statement=null;
The ResultSet rs=null;

Try {
DbBean myDbBean=new DbBean ();
MyDbBean. OpenConnection ();
String SQL="select * from excel";
//if (myDbBean. Conn==null) out. Print (" no!" );
Rs=myDbBean. ExecuteQuery (SQL);
} the catch (Exception e) {
e.printStackTrace();
}
While (rs), next ()) {
Out. Print (rs. Get string (" id ") + "- & gt;" );
Out. Print (rs. Get string (" username ") + "- & gt;" );
Out. Print (rs. Get string (" password "));

}
/* the try {

Class.forName("com.mysql.jdbc.Driver");
The String url="JDBC: mysql://127.0.0.1:3306/wwy? ServerTimezone=UTC ";
String user="root";
String password="wwy112358";

Conn=DriverManager. GetConnection (url, user, password);
The statement=conn. CreateStatement ();
If (conn==null) out. Println (" NO connect!
" );

String SQL="select * from excel";
Rs=statement. ExecuteQuery (SQL);
While (rs), next ()) {
Out.println (rs. Get string (" id ") + "- & gt;" );
Out.println (rs. Get string (" username ") + "- & gt;" );
Out.println (rs. Get string (" password "));
}

Out.println (" success!
" );
} the catch (Exception e) {
//TODO Auto - generated the catch block,
e.printStackTrace();
} the finally {
Try {
If (rs!=null) rs. The close ();
} the catch (SQLException e) {
e.printStackTrace();
} the finally {
Try {
If (the statement!=null) statement. The close ();
} the catch (SQLException e) {
e.printStackTrace();
} the finally {
Try {
If (conn!=null) conn. Close ();
} the catch (SQLException e) {
e.printStackTrace();
}
}
}
}
*/
%>


This is a JSP file

 package qwer; 
Import the Java. SQL. *;
Public class DbBean {

Public Statement STMT=null;
Public Connection conn=null;
Public ResultSet rs=null;
Public DbBean () {}

Public void openConnection () {
Try {
Class.forname (". Com. Mysql. JDBC Driver ");
The String url="JDBC: mysql://127.0.0.1:3306/wwy? ServerTimezone=UTC& UseSSL=false ";
Conn=DriverManager. GetConnection (url, "root", "wwy112358");
//STMT=conn. CreateStatement ();
//return conn.
}
The catch (a ClassNotFoundException e) {
System. Err. Println (" openConn: "+ um participant etMessage ());
}
The catch (SQLException e) {
System. Err. Println (" openConn: "+ um participant etMessage ());
}
The catch (Exception e) {
System. Err. Println (" openConn: "+ um participant etMessage ());
}
//return conn.

}

Public ResultSet executeQuery (String SQL) {
Try {
STMT=conn. CreateStatement ();
Rs=STMT. ExecuteQuery (SQL);
}
The catch (SQLException e) {
System. Err. Println (" executeQuery: "+ um participant etMessage ());
}
return rs;
}

Public int executeUpdate (String SQL) {
Int n=0;
Try {
STMT=conn. CreateStatement ();
N=STMT. ExecuteUpdate (SQL);
}
The catch (Exception e) {
System. The out. Print (e. oString ());
}
Return n.
}
Public void closeConnection () {
Try {
If (rs!=null)
rs.close();
}
The catch (SQLException e) {
System. Err. Println (" closeRs: "+ um participant etMessage ());
}
Try {
If (STMT!=null)
STMT. Close ();
}
The catch (SQLException e) {
System. Err. Println (" closeStmt: "+ um participant etMessage ());
}
Try {
If (conn!=null)
Conn. Close ();
}
The catch (SQLException e) {
System. Err. Println (" closeConn: "+ um participant etMessage ());
}
}
/* public static void main (String [] args) {
Boolean validated=false;
Boolean validated1=false;
The Connection conn=null;
The Statement Statement=null;
The ResultSet rs=null;
Try {
DbBean myDbBean=new DbBean ();
//conn=myDbBean openConnection ();
String SQL="select * from excel";
Rs=myDbBean. ExecuteQuery (SQL);
} the catch (Exception e) {
e.printStackTrace();
}

Try {
While (rs), next ()) {
System. The out. Print (rs. Get string (" id ") + "- & gt;" );
System. The out. Print (rs. Get string (" username ") + "- & gt;" );
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related