use properties file to connect to the database in jsp..

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

View Answers

September 3, 2012 at 3:58 PM

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

September 4, 2012 at 1:29 AM

Thank for giving your valuable response ...

We can also use as..

db.properties file

driver=sun.jdbc.odbc.JdbcOdbcDriver

connectionurl=jdbc:odbc:pro

username=PRS_ACC

password=prs

save 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...









Related Tutorials/Questions & Answers:
use properties file to connect to the database in jsp..
use properties file to connect to the database in jsp..  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
connect to the database from JSP
connect to the database from JSP  How do you connect to the database from JSP?   A Connection to a database can be established from a jsp page by writing the code to establish a connection using a jsp scriptlets
Advertisements
reading dropdown values from properties file in jsp
reading dropdown values from properties file in jsp  reading dropdown values from properties file in jsp
how to connect jsp with sql database by netbeans in a login page?
how to connect jsp with sql database by netbeans in a login page?  how to connect jsp with sql database by netbeans in a login page
how to connect jsp with sql database by netbeans in a login page?
how to connect jsp with sql database by netbeans in a login page?  how to connect jsp with sql database by netbeans in a login page
how to connect jsp with sql database by netbeans in a login page?
how to connect jsp with sql database by netbeans in a login page?  how to connect jsp with sql database by netbeans in a login page
How to write .properties file from jsp
it in my jsp but Iam unble to modify/write the .properties file and Iam not getting... to implement/write .properties file from jsp. Regards, Venkatesh Gurram.ADS...How to write .properties file from jsp  Hi i new to java i stuck here
common database jsp file for all the jsp files in the application
common database jsp file for all the jsp files in the application  hi, i have written jspcode to connect to mysqldatabase . i need the code to make use of this single jsp file while connecting to database rather than writing
Unable to connect servet, jsp to oracle 10g database.. Unable to retrieve data..
Unable to connect servet, jsp to oracle 10g database.. Unable to retrieve data.... If I use it also, unable to connect to backend oracle database. Let me... properties file JNDI_NAME=java:com/env/Oracle/jndi db.login= db.password
how to connect to MS access database in JSP?
how to connect to MS access database in JSP?  how to connect to MS access database in JSP? Any seetings/drivers need to be set or installed before... DSN will get created. 6) Restart your server and run your jsp code. Here is your
i can not connect to database in servlet - JSP-Servlet
i can not connect to database in servlet  Hi I am following... tested HelloWorld sample code and it was ok,but the tutorials which use database... offer .   Hi friend, Code to connect to database in servlet
i can not connect to database in servlet - JSP-Servlet
i can not connect to database in servlet  Hi I am following... tested HelloWorld sample code and it was ok,but the tutorials which use database... offer .   Hi friend, Code to connect to database in servlet
To insert attachment file in database in JSP.
To insert attachment file in database in JSP.  I am doing project in JSP. How to insert attachment file in mysql database? Please suggest some solution. Your inputs is valuable to me.   Hi Friend, Visit HereADS
Using javabeans to connect mySQL database on a jsp page - JSP-Interview Questions
Using javabeans to connect mySQL database on a jsp page  Hi, Am doing my project and I don't know how to connect a jsp page to a mySQl database...; Connecting a JSP page to MYSQL Database using JavaBean File Name: beancode.java
Connect to the database
Connect to the database  how to connect to the database in the iphone
Connect from database using JSP Bean file
Connect from database using JSP Bean file... database and then we have made the properties setter and getter for "msgid"... methods of this bean file we have to make entry for bean class in jsp file
How to read and display data from a .properties file from a jsp page
How to read and display data from a .properties file from a jsp page  I have a .properties file. I have to create a jsp page such that it reads the data from this .properties file and display it in table format. Ex:by using
To insert attachment file in database in JSP.
To insert attachment file in database in JSP.  I am doing project in JSP. How to insert attachment file in mysql database? Please suggest some... file upload form to the user</TITLE></HEAD> <BODY> <FORM
how to connect the database using hibernet through servlet/jsp through form
how to connect the database using hibernet through servlet/jsp through form  plz give me the reply   Hi Friend, Please visit the following link:ADS_TO_REPLACE_1 http://www.roseindia.net/jsf/myfacesspring
How to connect to MySQL in JSP?
, You can use the Java JDBC code to connect to MySQL database in JSP page. Check the tutorial: Connect JSP with mysql Thanks...How to connect to MySQL in JSP?  How to connect to MySQL in JSP
file upload and insert name into database - JSP-Servlet
file upload and insert name into database  Hi, I just joined as a fresher and was given task to upload a file and insert its name into database...  HIread for more information,http://www.roseindia.net/jsp/file_upload
How I Upload File and Store that file name in Database using JSP
How I Upload File and Store that file name in Database using JSP  Hi All, I m the beginner in JSP and I want to upload the file and store that file and some other form data in MySQL database. Ex. There is one employee detail
connect a web page to a database
connect a web page to a database  how to connect a web page to a database
How to use an object properties in jsp page
How to use an object properties in jsp page  I have a arraylist of objects 'User' populated using struts 2 action class while loading that jsp page. In that JSP I have included another jsp using following tag: Here instead
how to display data from jsp file into database
how to display data from jsp file into database  this is a jsp file . look at the line with code-> Statement st=con.createStatement...+",'"+email+"')"); out.println("Data is successfully inserted into database
TO connect multiple database
TO connect multiple database  How to connect several database from a single application in java depending upon the user input it will connect to that database
How to browse excel file and stored the contents into the database using jsp/servlet?
How to browse excel file and stored the contents into the database using jsp/servlet?  Hi.. I want to browse excel file and stored the file data into the My-sql database using jsp/servlet
connect database with javascript
connect database with javascript  can you please tell me how to connect database with javascript code. balaji.sivaramgari@gmail.com this is my mail id thanks in advance
Connect JSP with mysql
how to connect to MySQL database from your JSP code. First, you need to create database and then write jsp code to connect jsp to database.ADS_TO_REPLACE_1...) Here is the video tutorial of "How to connect to MySQL Database from JSP
downloading file through a file's path stored in database in jsp
downloading file through a file's path stored in database in jsp  hi want to download a file from a directory where the file location is stored in a database,i have done the coding for retrieving the path from database to a html
Read data from excel file and update database using jsp
Read data from excel file and update database using jsp  read data from excel file and update database using jsp Hi, I am using a MySQL database... upload excel file and update database using JSP ? Thanks in Advance
Data needs to be gathered in XML file from the database (MySql) using JSP
in XML file from the database (MySql) using appropriate JSP/Java Bean functions...Data needs to be gathered in XML file from the database (MySql) using JSP ... files. using following database create database music; use music; CREATE
how to upload multiple files in jsp and saving the path in database and the file in folder
how to upload multiple files in jsp and saving the path in database and the file in folder  how to upload multiple files in jsp and saving the path in database and the file in folder I have created a form for the upload of files
Connect database in Access to Netbean?
Connect database in Access to Netbean?  how to connect database in micrsoft access to Netbean?i know it can be connected by using JDBC-ODBC bridge, can i know the steps in connecting the database? Is there any source code
applicationcontext.xml properties file
applicationcontext.xml properties file In this tutorial I will explain how you can use the properties defined in .properties file in your spring application... in the applicationContext.xml file you can add the following code to use the properties
cannot connect to database - JDBC
cannot connect to database  Iam using eclipse in my system ,when connecting the database mysql version 5.0 to the eclipse iam getting an error as ""Creating connection to mysql has encountered a problem.Could not connect to mysql
How i upload file and save that record in database using JSP?
How i upload file and save that record in database using JSP?  Hi All, I m the beginner in JSP and I want to upload the file and store that file and some other form data in MySQL database. Ex. There is one employee detail form
How to connect mysql with jsp
How to connect mysql with jsp  how to connect jsp with mysql while using apache tomcat
Properties file in Java
the properties file to store the configurable values like database connection string... Properties file in Java       In this section, you will know about the properties file
How to Upload a file directly to Oracle database using JSP or Servlets?
How to Upload a file directly to Oracle database using JSP or Servlets?  Hi I want to upload a file(csv or excel) to Oracle 10g Database using JSP... FileItem file = (FileItem) items.get(2); // Connect to Oracle
struts2 properties file
struts2 properties file  How to set properties file in struts 2 ?   Struts 2 Format Examples Struts 2 Tutorial
Java Properties File Example
  Using Properties files Java   Stets to use a properties files... the properties file into the program.ADS_TO_REPLACE_2 4. Finally get the text using the key of the properties file. An example of using the properties file
connect jsp with tomcat - JSP-Servlet
connect jsp with tomcat  hello friends, i have severe problem in making connection between tomcat 6.0.18 and jsp pages.my system configuration... web.xml file.. or put your jsp prg folder in jsp-examples only, you can send
unable to connect database in java
unable to connect database in java  Hello Everyone! i was trying to connect database with my application by using java but i am unable to connect... i was using this code.... try { Driver d=(Driver)Class.forName
Connect JSP with mysql
Connect JSP with mysql : Now in the following jsp code, you will see how to connect with the database. First you need to copy my sql connector jar file... Connect JSP with mysql   
neo4j-server.properties file location
neo4j-server.properties file location  Hi, Where is the location of neo4j-server.properties file in Linux? Thanks   Hi, Location of neo4j-server.properties file: This file is present in /etc/neo4j directory of linux
Using Network Address To Connect to a Database
.style1 { text-align: center; } How To Use Network Address To Connect To The Database If a database being connecte over a network then you need to get... to connect a MySql database with your application over a network then you must load
open pdf file in same jsp page and the pdf file should retrieved from database
open pdf file in same jsp page and the pdf file should retrieved from database  Hai all, I need code to open a pdf file in same jsp page(browser) while click on hyperlink And the file was located in database table. Can any
How to connect with database
How to connect with database  jdbc connection
passing .properties file as a parameter to another function
passing .properties file as a parameter to another function  passing .properties file as a parameter to another function in an jsp file