This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

database connectivity in JSP  RSS feed

 
shailendra birthare
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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...
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!