Shakir Ali
Shakir Ali

Reputation: 55

How to connect Oracle Database Using JSP

I want to connect my Oracle database with web page using JSP. But data is not saving into database. if you have any idea or the code have any error please comment and show me the way to correct the the following code. Please this is the project of my final semester. The code is as follows. Thanks in advance.

<%@page import="java.sql.*"%>
<%@page import="oracle.jdbc.driver.*;" %>
<%@page import="oracle.sql.*;" %>
<%@page import="oracle.jdbc.driver.OracleDriver;"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<title>Auto Refresh Header Example</title>
</head>
<body>
<form>
<center>
<h1>Program Start</h1>
FirstName:<input type ="text" name ="firstName" id="firstName"> <br>

LastName:<input type="text" name ="lastname" id="lastname"> <br>

<input type="submit" value="Submit">
</form>
<%  
String username=request.getParameter("username");
String password =request.getParameter("username");

try {


Connection con = null;
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "hr", "hr");
Statement st = null;
PreparedStatement ps=null;
ResultSet rs =null;
String query = "insert into Test (username, username)values           ('"+username+"','"+password+"')";

ps.executeUpdate(query);

} catch(Exception ex){

} 

%>
</center>
</body>

Upvotes: 0

Views: 24193

Answers (4)

quickly get a close look at you values you have assigned two wrong parameters

first is "String password =request.getParameter("username");" which won't give you an error but data posted will be not that genuine, please change "username to "password"

Second is

String query = "insert into Test (username, username)values('"+username+"','"+password+"')";

which will be a runtime error hence the database will be waiting for a username and a password but you are supplying it with a username and a username. last try to us this connection string

Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr"); with the Class.forName being Class.forName("oracle.jdbc.OracleDriver");

Upvotes: 1

srujan
srujan

Reputation: 1

import java.sql.*;  
class InsertPrepared{  
public static void main(String args[]){  
try{  
Class.forName("oracle.jdbc.driver.OracleDriver");  

Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  

PreparedStatement stmt=con.prepareStatement("insert into Emp values(?,?)");  
stmt.setInt(1,101);//1 specifies the first parameter in the query  
stmt.setString(2,"Ratan");  

int i=stmt.executeUpdate();  
System.out.println(i+" records inserted");  

con.close();  

}catch(Exception e){ System.out.println(e);}  

}  
}  

Upvotes: 0

Carl Oliver
Carl Oliver

Reputation: 21

Within the try block I had to create statement for the connection and it appeared PreparedStatement wasn't necessary:

Connection con = null;
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "hr", "hr");
Statement st = con.createStatement();
String query = "insert into Test (username, username) values ('" + username + "','" + password + "')";

ps.executeUpdate(query);

Upvotes: 2

sai
sai

Reputation: 16

try to open your server in admin mode,so that it can connect to database,hope it helps you

Upvotes: 0

Related Questions