i can not connect to database in servlet

i can not connect to database in servlet

View Answers

October 11, 2008 at 12:47 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... code to connect to database: import java.sql.*; public class ConnetToDatabase
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 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 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 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.ADS_TO_REPLACE_1 Thanks
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
connec to database - JSP-Servlet
connec to database  Need code to connect the application to database. I have developed the application JSP and Servlet
connect database with javascript
connect database with javascript  can you please tell me how to connect database with javascript code. [email protected] this is my mail id thanks in advance
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
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
connect a web page to a database
connect a web page to a database  how to connect a web page to a database
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
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 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
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
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 connect two page
how i connect two page  strong textlink text
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
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
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 to connect with database
How to connect with database  jdbc connection
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
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
How to connect to MySQL from command prompt?
to a database you can use the following command: mysql> connect mysql; Please.... After this you will able to connect to MySQL database from command prompt... learned How to connect to MySQL database from command prompt. Check all
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
Exception "Couldn't load database driver: com.mysql.jdbc.Driver". I
Exception "Couldn't load database driver: com.mysql.jdbc.Driver". I  I m trying to connect to mysql from netbeans 6.9 but i m getting this exception "Couldn't load database driver: com.mysql.jdbc.Driver". I ve added the connector
JDBC Connection code to connect servlet with oracle.
JDBC Connection code to connect servlet with oracle.  JDBC Connection code to connect servlet with oracle
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
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
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
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
how i conditional access the data from database using combo box. - JSP-Servlet
how i conditional access the data from database using combo box.   i have combox box named class when i select its value 11 or 12, another combo box... access the data from database when i select class 11 0r 12. Here is JSP file
error in accessing database - JSP-Servlet
error in accessing database  hiiii im tanushri im tryng to connect my database to the servlet i hv succeeded in connectivity but im stuck to nother error called Got minus one from read call although i hv feeded data to my
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
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
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
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
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

Ads