This week's giveaway is in the Java/Jakarta EE forum.
We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line!
See welcome thread
Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

database connectivity in JSP  RSS feed

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......

/* Setup the connection */
Connection connection = DriverManager.getConnection ("jdbc dbc:emp");

/*set auto commit to 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...
Posts: 42975
  • 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.
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!