Switch to full style
What's behind JSP & Servlets
Post a reply

Unable to connect to JavaDB Database

Tue Oct 21, 2008 8:08 pm

I created a database in derby (with ij tool). I wanted to connect
this database through JSP page from Tomcat 6 server.

I started the Network Server from Command Window(Server Shell)

Server started (used 1527 port)

I connected the created database by typing:

ij> connect 'jdbc:derby://localhost:1527/assetsdb;create=true';

Can I access the database from JSP page this way?
//Sign.jsp

Code:
<html>
<head>
<title>Sign Up</title>
</head>
<body>
<%@ page language="java" import="java.sql.*" %>
<%
Connection conn;
conn=null;
ResultSet rs;
rs=null;
String empID=request.getParameter("empID");
String pwd1=request.getParameter("pwd1");
String pwd2=request.getParameter("pwd2");

int flag=0;

String driver="org.apache.derby.jdbc.ClientDriver";
String dbName="assetsdb";
String
connURL="jdbc:derby://localhost:1527/"+dbName+";create=true";
String selQuery="SELECT * FROM Employees where ID=?";
String inQuery="INSERT INTO Viewers values(?,?)";
try
{
Class.forName(driver);
conn=DriverManager.getConnection(connURL);
PreparedStatement stat=conn.prepareStatement
(selQuery);
stat.setString(1,empID);
rs=stat.executeQuery();
if(!rs.next())
{
flag=1;
%>
<script type="text/javascript">
alert("Employee ID Not Found.");
location.href="NewViewer.htm";
</script>
<%
conn.close();
}
}
catch(Exception e)
{
out.println("Mistake:"+e);
}
if(flag==0)
{
try
{
Class.forName(driver);
conn=DriverManager.getConnection(connURL);
PreparedStatement stat1=conn.prepareStatement
(inQuery);
stat1.setString(1,empID);
stat1.setString(2,pwd1);
stat1.executeUpdate();
flag=0;
response.sendRedirect("login.htm");
}
catch(Exception e)
{
out.println("Mistake inserting values:"+e);
}
finally
{
rs.close();
conn.close();
}
}
%>
</body>
</html>
------------
Where am I wrong in connecting to the database? I placed the
Sign.jsp page in Tomcat's ROOT/JSPs/Sign.jsp and the database in
E:\IT Assets\assetsdb



Re: Unable to connect to JavaDB Database

Tue Oct 21, 2008 8:09 pm

URL should be like this jdbc:derby://localhost:1527/vir
i think u have not provided username and password
Connection string is "URL","<username>",<password>

Post a reply
  Related Posts  to : Unable to connect to JavaDB Database
 How to connect database with Applets     -  
 How do I connect my form tomy database     -  
 How do I connect my form tomy database     -  
 Connect to database Microsoft access within jsp servlet     -  
 How to connect two computers and connect them with Internet?     -  
 Unable to open OST file in C# [.net]     -  
 Unable to find javax.servlet     -  
 Connect Dots     -  
 connect to MYSQL from ASP     -  
 How to Connect to MySql Server     -