
How to use properties file to connect jsp code with database ..........

Here is a jsp code that connects to database using properties file.
<%@page import="java.io.*"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%
Properties prop = new Properties();
prop.load(new FileInputStream("data.properties"));
String user = prop.getProperty("username");
String pass = prop.getProperty("password");
String port = prop.getProperty("port");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:"
+ port + "/test", user, pass);
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("Select * from data");
while (rs.next()) {
out.println(rs.getString(1) + " " + rs.getString(2)+"<br>");
}
%>
Here is a data.properties file.
username=root password=root port=3306

Thank for giving your valuable response ...
We can also use as..
db.properties filedriver=sun.jdbc.odbc.JdbcOdbcDriver
connectionurl=jdbc:odbc:pro
username=PRS_ACC
password=prssave the file.
check.jsp page<%@page import="java.sql.*"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.FileNotFoundException"%>
<%@page import="java.io.IOException"%>
<%@page import="java.util.Properties"%>//for use properties
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<h1>welcome user</h1>
<%
final Properties myresources = new Properties();
try {
FileInputStream in = new FileInputStream(
"C:\\Program Files\\Apache Software Foundation\\Tomcat 5.5\\webapps\\CustomerInfoSystem\\WEB-INF\\classes\\ApplicationResources.properties");//path of the properties file
try {
myresources.load(in);
}
catch (Exception e) {
e.printStackTrace();
}
}
catch (Exception e) {
e.printStackTrace();
}
try
{
RequestDispatcher rd=null;
Connection con=null;
ResultSet rs=null;
String drvr=myresources.getProperty("driver");
String url=myresources.getProperty("connectionurl");
String user_name=myresources.getProperty("username");
String passw=myresources.getProperty("password");
Class.forName(drvr);//for driver
con=DriverManager.getConnection(url,user_name,passw);//for url and user name and password.
String name1=request.getParameter("user");
String pass=request.getParameter("pass");
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//con=DriverManager.getConnection("jdbc:odbc:pro","PRS_ACC","prs");
Statement st=con.createStatement();
String sql="select name from user_profile where name='"+name1+"' and password='"+pass+"'" ;
out.println("query:::"+sql);
rs=st.executeQuery(sql);
while(rs.next())
{
String aa=rs.getString(1);
out.println(">>>>>>>"+aa);
if(name1.equals(aa))
{
response.sendRedirect("Welcome.jsp");
session.setAttribute("username", name1);
}
}
rd=request.getRequestDispatcher("Login.jsp");
rd.forward(request, response);
st.close();
con.close();
}
catch(Exception e)
{
out.println(" error"+e);
}
%>
</body>
</html>
It must work...
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.