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



Home Answers Viewqa JSF-Questions Java Connecting to database

Have Programming Question? Ask it here!
 
 
 


shivani
Java Connecting to database
1 Answer(s)      6 years and 5 months ago
Posted in : Java Server Faces Questions


Need an example code for Connecting application to database.


Advertisement
View Answers

March 17, 2008 at 8:52 PM


Java - Connecting to database
Hi, here I am providing you java example code that will help you to connect to database and you can insert values into two tables simultaneously.


Servletform.html

<html>
<head>
<title>registration form</title>
</head>
<body>
<table border="1" width="50%" cellpadding="0" cellspacing="0">
<tr>
<td width="100%">
<h2>Registration form</h2>
<form method="GET" action="DataInsertServlet">
<table border="1" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td><b>Name:</b></td>
<td><input type="text" name="firstname" size="30"></td>
</tr>
<tr><td><b>Address:</b></td>
<td><input type="text" name="address" size="50"></td>
</tr>
<tr>
<td><b>E-mail:<b></td>
<td><input type="text" name="email" size="30"></td>
</tr>
<tr><td><b>Contect No:</b></td>
<td><input type="text" name="contect" size="30"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="Submit" name="B1"></td>
</tr>
</table>
</form>
<p>&nbsp;</p>
</tr>
</td>
</table>
</body>

</html>


DataInsertServlet.java

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DataInsertServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)throws
ServletException, IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";;
String db = "register";
String driver = "com.mysql.jdbc.Driver";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,"root","root");
try{
Statement st = con.createStatement();
String firstname=request.getParameter("firstname");
String address=request.getParameter("address");
String email=request.getParameter("email");
String contect=request.getParameter("contect");
int val = st.executeUpdate("insert regiform values('"+firstname+"','"+address+"','"+email+"','"+contect+"')");
con.close();
out.println("Successfully insert data in database");
}
catch (SQLException ex){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}

}
}

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">; -->

<web-app>

<servlet>
<servlet-name>DataInsert</servlet-name>
<servlet-class>DataInsertServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DataInsert</servlet-name>
<url-pattern>/DataInsertServlet</url-pattern>
</servlet-mapping>
</web-app>
- - - - - - - - - - - - -

;) Meeya


Related Tutorials/Questions & Answers:
Connecting to MYSQL Database in Java
Connecting to MYSQL Database in Java  I've tried executing the code... to the database"); conn.close(); System.out.println("Disconnected from database"); } catch (Exception e) { System.out.println("Error
Problems connecting to a database. Java/SQLite
Problems connecting to a database. Java/SQLite  `print("try { con = DriverManager.getConnection("jdbc:sqlite:db/Freepark.sqlite"); } catch... on an SQL database but i am having problems connecting to it, I think the problem
Advertisements
Connecting to the Database Using JDBC and Pure Java driver
Connecting to the Database JDBC Driver In our search engine we are using MySQL database server and MM.MySQL Driver for connecting our application to the database. MM.MySQL Driver
Connecting to a database through the Proxy.
Connecting to a database through the Proxy.  Connecting to a database through the Proxy I want to connect to remote database using a program that is running in the local network behind the proxy. Is that possible
connecting to database - Struts
connecting to database  Hi I am having problems with connection to MS SQL Server 2005 database. My first is what do i write in struts... information via the database in my web page. Thanks Tayo  Hi friend
Connecting code of reset password to database
Connecting code of reset password to database  connecting code of reset password to database
connecting with database - Struts
connecting with database  I am creating an application where when jsp page is displayed, it contains the combo box where data is populated from the database.it has 3 buttons and the functionality for all buttons is different
connecting to access database
connecting to access database  print("code sample");Hi I Write java... this there is no error but my data is not going to my Acess Database. There is working... Access Driver(*.mdb) Select database name and Create the DSN name (e.g access
Connecting JTable to database - JDBC
Connecting JTable to database  Hi.. I am doing a project on Project Management System for which i created the user interfaces.. I have a user... to store this JTable content in my database table.. This is a very important
Connecting Oracle database with struts - Struts
Connecting Oracle database with struts  Can anyone please provide me some solutions on Connection between Oracle database and struts
Connecting to a MySQL Database in Java
Connecting to a MySQL Database in Java   ... the data in the database with the help of java,  instead of going to database... will learn how to connect the MySQL database with the Java file. Firstly, we need
java connecting frames
java connecting frames  how to connect one frame to another frame by using awt or swings?`print("code sample
connecting to a database dynamically - JSP-Servlet
connecting to a database dynamically   abc.html :- abc.jsp :- Above code gives the following... with database dynamically. Plz debug the code and explain the reasons for the exception
code for connecting reset password code to database.
code for connecting reset password code to database.  code for connecting reset password code to database.   Hello Friend, Do you want to change your password and update the password to database
Null pointer exceptation-Java Servlet web application,Problem connecting with MYSQL database
Null pointer exceptation-Java Servlet web application,Problem connecting.... T_T I have develop my java web application in window Environment and now i... system won't be able to connect to database in pooling environment. i try follow some
java connecting to oracle db
java connecting to oracle db  PLZ SAY ME HOW TO INSERT THE VALUES INTO ORACLE THIS IS THE CODE: import java.io.*; import java.sql.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; import
Connecting to Hardware - Java Beginners
Connecting to Hardware  I'm currently using netbean for my Final Year Project system that involve some hardware like electronic weighihng scale. Can you please provide me at least the idea if not the coding on how the system can
Connecting to Database from a hyperlink in JSP - JSP-Servlet
Connecting to Database from a hyperlink in JSP  How can I connect to database by clicking on a hyperlink in a JSP Page.Can you please give me sample... which is connect to database using jdbc database
jsp -sevlet connecting to database using dropdown
jsp -sevlet connecting to database using dropdown  How can I get my dropdown list from oracle database and then submit it to another table in JSP. I... to the database and fetches an array of strings from a database table and then sends
java connecting to oracle db - JDBC
java connecting to oracle db  how to connect oracle data base with java application?  Hi Friend, Follow these steps: 1) Import the following packages in your java file:*********** import java.sql.*; import
java jdbc connecting probelm - JDBC
java jdbc connecting probelm  import javax.servlet.*; import javax.servlet.http.*; import javax.sql.*; import java.io.*; public class ECom extends HttpServlet { String name,pass; public void doPost
Connecting to Unix through Java - JavaMail
Connecting to Unix through Java  Could you please tell a sample code, where i connect to the unix server and run a script and write the results in a file and mail that file back to me
java code for threading example for connecting data base
java code for threading example for connecting data base  Write a program that has two threads First thread queries the database and fetches all the employee records from the emp table. Stores the employee objects
Connecting different Servers - Java Server Faces Questions
Connecting different Servers  How can i connect two different tomcat server in an application
Connecting to MySQL database and retrieving and displaying data in JSP page
Connecting to MySQL database and retrieving and displaying data in JSP page...;org.gjt.mm.mysql.Driver").newInstance(); Connecting to your database To connect...; This tutorial shows you how to connect to MySQL database and retrieve the data
Error in connecting to the mySQL database in TOMCAT using more than one PC (database connection pooling)
Error in connecting to the mySQL database in TOMCAT using more than one PC (database connection pooling)  how do i implement connection pooling... String strDataSource = "java:comp/env/jdbc/HTHLogDB"; ic = new InitialContext
how to draw a network with nodes and lines connecting them - Java Beginners
how to draw a network with nodes and lines connecting them   HI, can anyone tell me, how to draw a network with nodes connected with lines in java....( say for 3 nodes). Actually iam doing a project. In that, i need to show
connecting databases
connecting databases  I need to connect mysql on 2 or more remote pc'c. how can i giv the ip address for 2 or more systems. is it possible to connect to the required systems by user specifying the database and table name my
JDBC access database
is a Java Database Connectivity. The JDBC Connectivity provides API classes and interfaces for connecting the front end in Java application with database connections... end of the Java application to the backend database. In case
connecting to timesten in web application
connecting to timesten in web application  Please give me the steps connecting to timesten in web application. Am using jsp
connecting jsp to mysql - JSP-Servlet
connecting jsp to mysql  Hi, i am working on 'Web application... to the mysql database through jsp. After downloading the mysql-connector-java-5.0   One jar file is needed to connect java with mysql data base. That can
Connecting jboss with sql 2005 - Struts
Connecting jboss with sql 2005  Hai, i have project that was developed in struts,backed is sqlserver 2005 and and i am using jboss. I want to connect another database (sqlserver 2005) with my project.could u please help me
java database
java database  help me with this...i never learn build java database... to design and implement objects needed for a program that maintains a database.../teacher information, search it, alter it, etc. The database is maintained as a list
Database,Java
Database,Java  help me for the following things if one customer... me the how to write the query in MySQL database and i want coding for this one in Java.because i am using Java as the front end.   1)page.jsp: <
java and database
java and database  sir i want to prepare a sign in form and i want to check that user name is exist or not in database if it is exist in database then return it is allready exist otherwise it is registered in data base thank u
connecting servlet to db2 - JSP-Servlet
connecting servlet to db2  Hello sir, Iam new to db2.so I would like... the configuration we need to be done before connecting to the db2(such as what path we have to set and all).  If u are using the oracle Database 9i
Connection to database table in Oracle with Example
Connection to database table in Oracle with Example In this Section, we will discuss about connecting Java frontend with OracleDatabase. Step1: First... database must be correct. Step3: Compile and Run your Program
JAVA DATABASE CONNECTION WITH JTABLE
JAVA DATABASE CONNECTION WITH JTABLE  HOw To Load Database Contents From Access Database to JTable without using Vector
Map java Object to database.
Map java Object to database.  How do you map Java Objects with Database tables
doubt in connecting mysql in flex - XML
doubt in connecting mysql in flex  The ?Create application from database? is a Flex 3 feature that enable you to create simple applications in few... of all you have tosetupyourMySql datasource by creating a database and a table
doubt in connecting to mysql in flex - XML
doubt in connecting to mysql in flex  The ?Create application from database? is a Flex 3 feature that enable you to create simple applications in few... of all you have tosetupyourMySql datasource by creating a database and a table
Database programming - Java Beginners
Database programming  How to do a Database programing with Balanced Multiway Tree(B+ Tree,not Binary Tree) in java
problem in connecting clients - Development process
problem in connecting clients   i had coded programe to establish the lan based chat application but the server listening to only one client at a time it not receiving the messages form allclients would tell me the solution  
java and database - Java Beginners
java and database  code to read a csv file  Hi Friend, We have used POI Library to read the csv file in java: import java.io.*; import org.apache.poi.hpsf.*; import org.apache.poi.hssf.usermodel.*; import
java database connection - JDBC
java database connection  sir i want to join my project with MS access database. i am making my project in netbeans.please tell me the coding to do... the data into the MS Access database and retrieve the data from the database table
Connecting to remote mysql server using jdbc.
Connecting to remote mysql server using jdbc.  How to Connect to remote mysql server using jdbc
java database connection
java database connection  package com.tsi.constants; public class Constant { public static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; public static final String DBURL = "jdbc:oracle:thin
java database connection
java database connection  package com.tsi.constants; public class Constant { public static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; public static final String DBURL = "jdbc:oracle:thin
 

 

 

DMCA.com