Avatar of ivoicenetworks

asked on 

Needed A jsp code to pull a value from mysql table

Dear experts can some one help me with the code in jsp page to fetch data from a table coulm in mysql  according to the variable i passed through jsp page

suppose if i browse http://mywesite.com/getvalue?user=test  then
 it will run mysql query  like  SELECT user_data FROM userslist WHERE user_id=test

and dispaly the value in browser .

Waiting for some one reply with the code for jsp and class needed

Thank you
JavaScriptJava EEMySQL Server

Avatar of undefined
Last Comment
Avatar of Loganathan Natarajan
Loganathan Natarajan
Flag of India image

Avatar of ivoicenetworks


dear logudotcom  i have gone through the same links yesterday ,  . I posted this question expecting some one can reply with exact code . Those links explain how to connect mysqldb to java and making logging page etc.,  but i need to pass a variable through URL and fetch the data from the table . there is no authentication needed . But to run the query backend there must be servlet to connect to DB in which mysql root password is hardcoded ( sorry as i am  completely new to java world , i dont know what do you call it) ..

Hope some one can reply with the jsp page i need to put in webserver .
Avatar of SANDY_SK
Flag of India image

If it is complete jsp code, it is one thing, if you are looking at jsp and servlet and all that, it is a web-application which has to be deployed as a war file(or folder) on the server,

Are you aware on how to build a war file and what are the components of the same?

Because even if we give you a jsp and a servlet which does this, do you know how to build it and deploy it?
Avatar of ivoicenetworks


Dear sandy_sk yeah please give me that file . I have installed mysql and apache-tomcat , I will copy to webapps directory and check .
Avatar of SANDY_SK
Flag of India image

Check this out, save this as Sample.jsp and put it in the webapps folder and access it as follows http://mywesite.com/Sample.jsp?user=test 

PS : I have not executed this code, so check to see if there are any syntax errors and fix them

<%@ page import="import java.sql.Connection" %>
<%@ page import="import java.sql.PreparedStatement" %>
<%@ page import="import java.sql.ResultSet" %>
<%@ page import="import java.sql.DriverManager" %>
<%@ page import="import java.sql.SQLException" %>


String strServer = ""; //Give your database server name here
String strPort = ""; //Give your database port here
String strDBName = ""; //Give your database name here
String strUser = ""; //Give your database user name here
String strPassword = ""; //Give your database password here
String strUser = null;
String strSQL = "SELECT user_data FROM userslist WHERE user_id=? ";

Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;


	strUser = (String) request.getParameter("user"); //Getting the value of user from the request

	con = DriverManager.getConnection("jdbc:mysql://"+strServer+":"+strPort+"/"+strDBName, strUser, strPassword);

	ps = con.prepareStatement(strSQL);
	ps.setString(1, strUser);

	rs = ps.executeQuery();


		out.println("<H1>User Data : "+rs.getString("user_data")+"</H1>");

	} else {
		out.println("<H1>User Data : No data found for user"+strUser+"</H1>");
} catch(Exception ex){
	out.println("Exception occurred while retrieving data");
} finally {


Open in new window

Avatar of ivoicenetworks


Dear SANDY_SK  is it ok to put db password  and user name credentials in JSP file ?

I am going to test this . Thank you
Avatar of ivoicenetworks


Hi SANDY_SK , it doesn't work . I passed user_id  value to url http://localhost/test.jsp?user=test

It supposed to show up test user_data .

It says In the code , strUser string is duplicated ( mentioned in 2 places).

Even i changed the second string to strUser2 but still it doesn't work .

Please help
Avatar of SANDY_SK
Flag of India image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of ivoicenetworks


Thank you SANDY_SK

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo