I could not connect my jsp page with ms access
it says
"java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"
I have created a dsn but still the problem is same..
I searched the net extensively but couldn't rectify....
DriverManager.getConnection("jdbc:odbc:students");
This line is causing exception....
I selected the data source in the DNS still too.... I'm using Access....
Here is everything:
<%#page import="java.sql.PreparedStatement"%>
<%#page import="java.sql.DriverManager"%>
<%#page import="java.sql.Connection"%>
<%#page contentType="text/html" pageEncoding="UTF-8" language="java"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
PreparedStatement pstm;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//connection object created using DriverManager class
//employee is the name of the database
Connection connect =DriverManager.getConnection("jdbc:odbc:employee");
//creating prepared statement object pstm so that query can be sent to database
pstm=connect.prepareStatement("insert into employee values(?,?,?,?)");
pstm.setString(1,"5");
pstm.setString(2,"parth");
pstm.setString(3,"parth");
pstm.setString(4,"10000");
//execute method to execute the query
pstm.executeUpdate();
out.println("Record Added Successfully");
pstm.close();
connect.close();
%>
</body>
</html>
you can test your datasource config by creating a new empty file (pretty much anywhere, desktop will work fine) with any name you like and changing the extension to .udl
When you change the extension the icon changes. Then double click it and you should see students datasource in the first combo (Use data source name), select it and click Test Connection.
If everything works out up to this point then your datasource is ok, and the problem is with your code, which should be something like this http://www.kodejava.org/examples/151.html
Related
I am trying to just set up a very basic jsp file that takes in a query from a url and displays it. I have the following test.jsp file that I run on the server:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test jsp</title>
</head>
<body>
<% String firstName = (String) request.getAttribute("firstName");
out.println("Hello :" + firstName);%>
</body>
</html>
However, when I type in the following URL, I still get a "null" result (even after refreshing): see Picture.
Note: my ultimate goal is to do have an event at some point that sends a POST request from a java file and display its result in the jsp page. If I understood well from my research, I would have to do it via a Servlet with a DispacherRequest forwarding method. But I first want to understand why the simple aforementioned code isnt working).
Thank you for your help !
You mention that you're trying to display a query parameter from the URL. A query or request parameter is not the same as a request attribute.
To get a query parameter, you would have to use the getParameter() method:
String firstName = request.getParameter("firstName")
I am trying to integrate my report in JSP page. Report created using jaspersoft studio. But i am getting null pointer exception on runReportToPdf line. I am totally new to this report and web application. So please someone help to resolve this and get excepted result. Quick support will be very much appreciated. Thanks.
<%# page import="java.io.*"%>
<%# page import="java.sql.Connection"%>
<%# page import="java.sql.DriverManager"%>
<%# page import="java.util.HashMap"%>
<%# page import="java.util.Map"%>
<%# page import="net.sf.jasperreports.engine.*"%>
<%# page trimDirectiveWhitespaces="true" %>
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Report!</h2>
<%
Connection con = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:#192.168.0.248:1521:incomingqc", "inqc", "megaWIN123$");
System.out.println("i AM IN CONNECTION");
}catch (Exception ex) {
System.out.println("i AM ex para");
ex.printStackTrace();
}
File reportFile = new File(application.getRealPath("Blank_A4_Landscape.jasper"));//your report_name.jasper file
Map parameters = new HashMap();
byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parameters, con);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream outStream = response.getOutputStream();
outStream.write(bytes, 0, bytes.length);
outStream.flush();
outStream.close();
%>
</body>
</html>
PDF and HTML content cannot be mixed. When the JSF page generates the HTML, it writes to a temporary buffer. That temporary buffer is sent to the browser once JSF is told that the response is complete.
In the given code, here is a rough idea of what the web browser will receive:
<html><head></head><body><h2>Report!</h2>%PDFNΘs*̶͑̾̾̅ͫ͏̙̤g͇̫͛͆̾ͫ̑͆l͖͉̗̩̳̟̍ͫͥͨe̠̅s...</body></html>
For the browser to display the report there are a few options:
Generate an HTML version of the report and use <h:outputFormat> to inject the report content into the existing web page.
Generate a PDF version of the report and use <h:commandLink> or <h:commandButton> with an action that writes the PDF to the browser, much as you've already done.
Mixing both approaches as shown in the question, however, will not produce the desired result.
See also:
How to provide a file download from a JSF backing bean?
Send a PDF to browser via JSF
http://www.jroller.com/hakan/entry/jasperreports_and_jsf_integration
This question already has answers here:
How do I pass get request parameters to my Velocity Template to send mails
(2 answers)
Closed 9 years ago.
Can anyone tell me how to pass value from JSP file to velocity template.
Maybe you should try to get someting simple to work. Consider a simple dynamic web project with a jsp like the following:
<%#page import="java.io.File"%>
<%#page import="org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"%>
<%#page import="org.apache.velocity.runtime.RuntimeConstants"%>
<%#page import="java.io.StringWriter"%>
<%#page import="org.apache.velocity.VelocityContext"%>
<%#page import="org.apache.velocity.Template"%>
<%#page import="org.apache.velocity.app.VelocityEngine"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String s = request.getParameter("test");
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, "/");
velocityEngine.init();
Template t = velocityEngine.getTemplate("/template.vm");
VelocityContext context = new VelocityContext();
context.put("example",
s);
StringWriter w = new StringWriter();
t.merge(context, w);
%>
<p><%out.println(w.getBuffer().toString()); %> </p>
</body>
</html>
It just loads a template from the root of your server and prints it out in the current site with a GET/POST parameter.
In the WEB-INF/lib folder of your project you put the velocity.jar, the common-collections.jar and the common-lang.jar.
Important: In your server root directory you put a simple textfile template.vm with the following template
<b>Test $example<b>
You can also have a template directory or something similar but I tell you when you wanna deploy your templates with the webapp(loading vm-files from WEB-INF directory for example)-> that's a challenge!
Load it with yourproject/example.jsp?test=getparamh and I'm sure it work
But if you try this - admitdettly - simple example, it should work and you can maybe derive your targets from it.
I'm learning about Servlets/jsps and have some test classes written. Everything seems to work as expected, the only problem I'm running into is being able to compile a simple Java class. This is the class:
package ilya.model;
public class DatabaseConnection {
public String getConnection()
{
String result;
try {
Class.forName("org.postgresql.Driver");
System.out.println("found the driver");
result = "Connection established!";
}
catch (ClassNotFoundException e)
{
System.out.println("No driver");
result = "No Connection";
}
return result;
}
}
The jsp trying to access it is pretty simple and I don't think it has anything to do with it. If anyone wants me to post it let me know.
Here's the Exception I receive when class is initialised for the first time:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 15 in the generated java file
Only a type can be imported. ilya.model.DatabaseConnection resolves to a package
This compile fine in a regular Java project. Any ideas?
Update
Here is the JSP file. It's actually working now. I tried the same project on a different machine and everything worked. Must be something wrong with Eclipse.
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# page import="ilya.model.BeerSuggestor, ilya.model.DatabaseConnection" %>
<%!
int count=0;
String connect;
public void jspInit() {
ServletConfig sconfig = getServletConfig();
String lname = sconfig.getInitParameter("lastName");
ServletContext context = sconfig.getServletContext();
context.setAttribute("lastName", lname);
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%-- DatabaseConnection intialized here --%>
<%
DatabaseConnection db = new DatabaseConnection();
connect = db.getConnection();
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>The count is: <%= this.count++ %></p>
<p>The count is: <%= 500 %></p>
<p>The count is: <%= config.getInitParameter("lastName") %></p>
<%-- Value of connect printed here --%>
<p>The connection result is: <%=" " + connect %>
</body>
</html>
You cannot have plain Java in JSP files as you show here..
Create a separate class in the appropriate source folder inside Eclipse for this.
Agreed with previous comments. It looks like, you are trying to establish a database connection directly from your JSP.
Please check the line in JSP, specially where you are setting the driver in jsp.
You can try this from your jsp as well.
<%
Class.forName("org.postgresql.Driver");
Connection myConn=DriverManager.getConnection("jdbcostgresql://localhost/db_name?user=db_user&password=db_pwd");
%>
i am creating a web project using JSP, and is trying to implement a simple search for users from my database using jquery autocomplete, however i am having trouble understanding how it works. i have little to no knowledge on jquery and ajax just to let you know. i have done the following code and am stuck.
<%#page contentType="text/html" pageEncoding="UTF-8" import="ewa.dbConnect,ewa.sendEmail,ewa.pwGen,ewa.hashPw,java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/jquery.autocomplete.css" />
<script src="js/jquery.autocomplete.js"></script>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<input type="text" id="search" name="search"/>
<script>
$("#search").autocomplete("getdata.jsp");
</script>
</body>
</html>
getdata.jsp
<%#page contentType="text/html" pageEncoding="UTF-8" import="ewa.dbConnect,java.sql.*" %>
<%! dbConnect db = new dbConnect(); %>
<%
String query = request.getParameter("q");
db.connect();
Statement stmt = db.getConnection().createStatement();
ResultSet rs = stmt.executeQuery("SELECT username FROM created_accounts WHERE username LIKE "+query);
while(rs.next())
{
out.println(rs.getString("username"));
}
db.disconnect
%>
if i am not wrong i read from a website, the parameter q is default and is just there, however how do i display the data? how do i pass the values from getdata.jsp into the autocomplete?
You're calling the autocomplete script tag before jQuery has been included. So, not having jQuery to latch onto (as the jQuery object hasn't been defined), nothing from the jQuery autocomplete plugin will load.
You have
<script src="js/jquery.autocomplete.js"></script>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
It should be
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="js/jquery.autocomplete.js"></script>
Reverse the order, and the Firebug errors you mentioned should disappear; I'm not sure it'll solve everything, but nothing will work until that's resolved.
I don't see jQuery UI being included (that one provides the autocomplete functionality)
http://jqueryui.com/demos/autocomplete/
So you need to include jquery.ui.autocomplete.js
(Or are you using the plugin autocomplete? if so, move to the jquery UI version)
Could also be that the data from getdata.jsp is malformed for the use in autocomplete.
How you tried debugging the javascript in a browser such as chrome or in firefox(with firebug)
I usual give (for jquery UI autocomplete) a JSON formatted answer, while I see your answer loop give a CR delimited list.
In getdata.jsp instead of produce:
jim<cr>
jack>cr>
jhon<cr>
try to return:
[{label: 'jim', value: 'jim'}, {label:
'jack', value: 'jack'}, {label:
'jhon', value: 'jhon'}]