• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

database connectivity in JSP

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am newbie to Java & JSP.

i'm using the following code in java file....can we use the same code in jsp file in scriptlet or as javascript......




Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
/* Setup the connection */
Connection connection = DriverManager.getConnection ("jdbc dbc:emp");


/*set auto commit to false*/
connection.setAutoCommit(false);
/*create statement*/
Statement statement = connection.createStatement();


/* String str = "insert into EmployeeData values '1','kirti','A',23100,'rtm','prakash')";*/


String str = "select * from EmployeeData where EmpID = '" + emp.getJ_empID() +"'";


/* Store no. of rows updated in result */
ResultSet rs = statement.executeQuery(str);




i'm trying to use it in javascript and for using that i'm impoting following in page directive as


<%@ page language="java" import="java.sql.Connection, java.sql.DriverManager, java.sql.ResultSet, java.sql.SQLException, java.sql.Statement" %>


but it is not working is there any other way to use this...


My objective is to get connect to database in JSP....

Please suggest other ways

Thanks in ADV...
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Shailendra-

Welcome to JavaRanch.

I'm not sure why you're trying to connect from JavaScript (which is a client-side technology), if your objective is to connect from JSP (which is a server-side technology), but that doesn't really matter, because JavaScript has nothing to do with Java. You can't embed your Java code in JavaScript for various reasons, which you can read more about in this article.

It is possible to embed the code in a JSP page as a scriptlet, although you should keep two things in mind. First, it is considered bad design to have Java code in JSP pages or in servlets. You should separate that out into its own class. Second, the JDBC/ODBC bridge driver you're using is
not suitable for multi-threaded accesses, like they are possible in a web application environment. It may work if there are no concurrent accesses, but there may be problems if more than one user is active at a time. That's especially true if the DB in question is Access.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic