• Post Reply Bookmark Topic Watch Topic
  • New Topic

sql error

 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i declare my database through servlets???

Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/e_store");
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That will work.
You might also want to look into whatever your app server offeres for connection pooling and JNDI datasources.
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually it doesn't work
i compile it with jcreator and the error is :

incompatible types

found java.sql.Connection
required Connection

any idea \

thnx
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Aris Doxakis:
actually it doesn't work
i compile it with jcreator and the error is :

incompatible types

found java.sql.Connection
required Connection



can you put complete code !! I dont find the reason for fail in compiling.



are you sure that con is java.sql.Connection Type

Shailesh
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, I received an error similar to this in the past. The problem was in my import statements. I imported the same Class from different packages. For example



Not knowing, I imported the the same class from two different packages.

Could this be your problem.
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my test project so i can open a database connection

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

public class Connection extends HttpServlet
{
Connection con;
public void init()
{
try
{
Class.forName("org.gjt.mm.mysql.Driver");
String conne = "jdbc:mysql://localhost/e_store";
con = DriverManager.getConnection(conne);
}
catch (ClassNotFoundException e)
{
log("Couldn't find mm.mysql driver");
}

catch (SQLException e)
{
log("Sql Exception while connecting to MySQL",e);
}
}

public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
response.setContentType("text/html;charset=ISO-8859-7");
PrintWriter out = response.getWriter();

if (con!=null)
{
out.println("Sindethika epituxos");
out.println("<br> Onomata stin Basi Dedomenod:");

try
{
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("show tables;");
while (rs.next())
{
String tableName = rs.getString(1);
out.println("<br> + tableName");
}
}

catch (SQLException e)
{
log("SQL Exception",e);
}
}
else
{
out.println("Den mporesa na syndetho me tin BD");
}
}

public void destroy()
{
if (con!=null)
{
try
{
con.close();
}

catch (SQLException e)
{
log("Couldn't close Connection",e);
}
}
}
}
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change the name of your class, it is Connection!
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Carol Enderlin:
Change the name of your class, it is Connection!


Aris,

Carol gave you the solution

one NOT recommended approach would be that declare you con variable as

java.sql.Connection con;

but again I insist on what carol said "Change the name of class"
[ April 06, 2005: Message edited by: Shailesh Chandra ]
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe that will work thnx
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it did work actually

one more question(im new so be easy on me)

how can i see my servlet running on internet explorer???

with tomcat

in which folder should i put it and so on

thnx
 
Saket Barve
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The complied servlet needs to go to the classes folder inside of your context's WEB-INF.

You might want to refer to the docs before proceeding further.

Regards,
Saket
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!