buat file java dengan nama Database.java di package dbase.
package dbase;
import java.sql.Connection;
import java.sql.DriverManager;
public class Database {
String url = "jdbc:mysql://localhost/latihan";
String user = "root";
String pass = "";
Connection conn;
public Connection getConnection() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(url,user,pass);
return conn;
}
}
Buat form di index.jsp
Buat servlet dengan nama Login.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Database db= new Database();
String getUser= request.getParameter("userID");
String getPass= request.getParameter("passID");
String sql="select * from login";
Statement st=null;
ResultSet rs=null;
try {
st= db.getConnection().createStatement();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
try {
rs=st.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
String user;
String pass;
int index=1;
try {
while(rs.next()){
user=rs.getString("username");
pass= rs.getString("password");
if(getUser.equalsIgnoreCase(user) && getPass.equals(pass)){
RequestDispatcher dis= request.getRequestDispatcher("admin.jsp");
dis.forward(request, response);
}else{
RequestDispatcher dis= request.getRequestDispatcher("index.jsp");
dis.forward(request, response);
}
index++;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
No comments:
Post a Comment