Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet i can not connect to database in servlet

Have Programming Question? Ask it here!
 
 
 


saeed
i can not connect to database in servlet
1 Answer(s)      5 years and 11 months ago
Posted in : JSP-Servlet



Advertisement
View Answers

October 11, 2008 at 12:49 PM


Hi friend,


Code to connect to database in servlet

import java.io.*;
import java.util.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ServletConnection extends HttpServlet {
public void service(HttpServletRequest request,HttpServletResponse response)

throws IOException, ServletException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet JDBC</title></head>");
out.println("<body>");
out.println("<h1>Servlet JDBC</h1>");
out.println("</body></html>");
// connecting to database
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con =DriverManager.getConnection ("jdbc:mysql://192.168.10.59:3306/example";;,

"root", "root");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM servlet");
// displaying records
while(rs.next()){
out.print(rs.getObject(1).toString());
out.print("\t\t\t");
out.print(rs.getObject(2).toString());
out.print("<br>");
}
} catch (SQLException e) {
throw new ServletException("Servlet Could not display records.", e);
} catch (ClassNotFoundException e) {
throw new ServletException("JDBC Driver not found.", e);
} finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(con != null) {
con.close();
con = null;
}
} catch (SQLException e) {}
}
out.close();
}
}

For information on Servlet visit to :

http://www.roseindia.net/servlets/

Thanks


Related Tutorials/Questions & Answers:
i can not connect to database in servlet - JSP-Servlet
i can not connect to database in servlet  Hi I am following the tutorial in this site on servlet and JDBC. I did all the proccedure for connecting... 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 the tutorial in this site on servlet and JDBC. I did all the proccedure for connecting... offer .   Hi friend, Code to connect to database in servlet
Advertisements
How can I connect my database to my application ?
How can I connect my database to my application ?  How can I connect my database to my application?   Hi, You can use JDBC API to connect to database from your Java application. Here is the sample code to connect
how can i create a mysql database to connect to this code - JDBC
how can i create a mysql database to connect to this code  i need help creating a mysql database for this code. code is import java.awt....";, "root", "root"); Statement st=con.createStatement(); int i
how can i create a mysql database to connect to this code - JDBC
how can i create a mysql database to connect to this code  i need help creating a mysql database for this code. code is import java.awt....";, "root", "root"); Statement st=con.createStatement(); int i
How can i use Facebook connect button
How can i use Facebook connect button  Please to meet you all guys I wonder how can i use this Connect to facebook for me to post in a particular... http://likekhevy4.blogspot.com/ How can i apply this kind of comment with "Connect
Connect to the database
Connect to the database  how to connect to the database in the iphone
How can I to my database to my application
How can I to my database to my application  How can I to my database to my application   Hi, Please see the JDBC discussion thread. Thanks
How can I protect my database password ?
How can I protect my database password ?   How can I protect my... a database over the internet. I have concerns about the security of the database... in as plain text. What can I do to protect my passwords
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: http://www.roseindia.net/jsf/myfacesspring/loginRegistration.shtml Hope
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
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
connect a web page to a database
connect a web page to a database  how to connect a web page to a 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
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
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 can i store text box values as it is in database table
how can i store text box values as it is in database table  CUSTOMER DESCRIPTION
How to connect to MySql Database from Servlet?
; font-weight: bold; } How to connect to MySql Database from Servlet... will show you how to connect to MySQL database and perform select operation. You will learn the JDBC steps necessary to connect to the MySQL Database and execute
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
How to connect with database
How to connect with database  jdbc connection
how to connect program to database
how to connect program to database  Sir i want ur help in my program. I want to save the data in the database. When i'll click on save button the entered data must be save in the database. I want an action performed method
connect with Database - SQL
connect with Database  Hi, when ever i m trying to connect with 10g ..i m getting error .. Failed to get a connection... service is started or not?   Hi! Even I encountered the same
How can I call a Servlet that is declare on web.xml with spring framework
How can I call a Servlet that is declare on web.xml with spring framework  here is my problem, I have spring framework and Crystal Report... is declare on web.xml, but the spring dispacher don't find it so how I can make
How can I access databse through JSP. I am using postgresql-8.4.4-1-windows as database and jboss-4.0.5.GA as server.
netbeans-6.1-ml-windows.How can I set up connection to database? Please help me...How can I access databse through JSP. I am using postgresql-8.4.4-1-windows as database and jboss-4.0.5.GA as server.  I am using postgresql-8.4.4-1
can i change the logo of the web site on the fly - JSP-Servlet
can i change the logo of the web site on the fly  Sir, will you please let me know if i can change the logo of the web site on the fly. What will i have to enable logo changing on the fly. Mine is a web application.Please let me
How can I get the full URL with the attached parameters? - JSP-Servlet
How can I get the full URL with the attached parameters?  Hi, I'm trying to figure out how to get the current url and use it in my jsp page. I...:8080/MesEduSchoolsProject/displayMenu.jsp How can I get the full URL with the attached parameters
how i can add an horizontal scrollbar at my PdfAnnotation ? - JSP-Servlet
how i can add an horizontal scrollbar at my PdfAnnotation ?  How to add the an horizontal scrollbar at my PdfAnnotation ? for example document d=new document(0. document.open
connect database without specifying dsn name in java
connect database without specifying dsn name in java  How can i connect the database with my application without specifying the dsn name by using...(); } but this is not working. can i do
what is ment by jdbc and how to connect with database?
what is ment by jdbc and how to connect with database?  i want answer for this question
HOW TO CREATE COMMENT IN WEBSITE AND CONNECT TO DATABASE?
HOW TO CREATE COMMENT IN WEBSITE AND CONNECT TO DATABASE?  HOW TO CREATE COMMENT IN WEBSITE AND CONNECT TO DATABASE
HOW TO CREATE COMMENT IN WEBSITE AND CONNECT TO DATABASE?
HOW TO CREATE COMMENT IN WEBSITE AND CONNECT TO DATABASE?  HOW TO CREATE COMMENT IN WEBSITE AND CONNECT TO DATABASE
how to connect to database in php using mysql
how to connect to database in php using mysql  how to connect to database in php using mysql
i want to protect mysql database table which user can not view to table and can not copy my table till then us
i want to protect mysql database table which user can not view to table and can not copy my table till then us  i want to protect mysql database table which user can not view to table and can not copy my table till then user do
User Registration Form Using JSP(JspBeans) after that how i can insert in database
User Registration Form Using JSP(JspBeans) after that how i can insert in database   User Registration Form Using JSP(JspBeans) after that how i can insert in database
i want to protect mysql database table which user can not view to table and can not copy my table till then us
i want to protect mysql database table which user can not view to table and can not copy my table till then us  i want to protect mysql database table which user can not view to table and can not copy my table till then user do
JDBC Connection code to connect servlet with oracle.
JDBC Connection code to connect servlet with oracle.  JDBC Connection code to connect servlet with oracle
unable to connect to server - JSP-Servlet
unable to connect to server  thank you for the program code for inserting and reteriving the image but i am unable to deploy these two programs in weblogic/apache servers can any one help me how to do this ?   Hi Friend
connec to database - JSP-Servlet
connec to database  Need code to connect the application to database. I have developed the application JSP and Servlet
Can I pass a
Can I pass a   Suppose I have servlet named Name.java and there is a variable String name holding a value "Roseindia". I have collected the value to Getname.jsp via ajax. Now how can I pass this value to scriplet without page
connect jsp with tomcat - JSP-Servlet
connect jsp with tomcat  hello friends, i have severe problem... already set) ->tomcat 6.0.18 (c:/tomcat/tomcat6.0/) I have my site folder... and sample.jsp files.under bpml folder i have created folder WEB-INF and under WEB
How can I paginate a table which has shown in a div through Ajax in client side without using database
How can I paginate a table which has shown in a div through Ajax in client side without using database   The table is shown by calculation in a PHP script file so The data not store any where so please give me the way to paginate
How to connect to the database to my application if my database is made up in oracle
How to connect to the database to my application if my database is made up in oracle   How to connect to the database to my application if my database is made up in oracle
Using Network Address To Connect to a Database
to connect a MySql database with your application over a network then you must load... is "root" An example of using network address to connect to database... .style1 { text-align: center; } How To Use Network Address To Connect
pls provide steps to connect mysql database in flex - XML
pls provide steps to connect mysql database in flex  hi,i am new to flex and doing project. i have doubt in how to connect MYSQL database in flex .pls provide the steps . i would be really thankful 2 u. i am loooking
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
How to connect to database to my application if the database is made up in oracle
How to connect to database to my application if the database is made up in oracle  How to connect to database to my application if the database... Class.forName("oracle.jdbc.driver.OracleDriver"); 3) Connect to database
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 it? Please tell what needs to be done after creating a table with an example
how can i use one dbase conection in serveral pages - JSP-Servlet
how can i use one dbase conection in serveral pages  Hi! Thanks for sending code... But my actual requirement is In my project i am writing dbase connections many times whereever i need in several pages. How can I avoid
How To Connect MS ACCESS 2003 Database in C Program with Graphics.
How To Connect MS ACCESS 2003 Database in C Program with Graphics.  How To Connect MS ACCESS 2003 Database in C Program with Graphics
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
 

 

 

DMCA.com