473,569 Members | 2,844 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How I call a java class from my jsp.. please

1 New Member
I have a big problem, y need to print a jasper report, but it must to be with jsp.
this is my jsp


please help me

Expand|Select|Wrap|Line Numbers
  1. <%@ page import="java.io.*,java.util.*,java.net.*,java.sql.*,imprimir.imprimir" %>
  2.  
  3. <%
  4.  
  5.  
  6. if(request.getParameter("GRABAR") != null)
  7.  
  8. {
  9.  
  10. // objetos de enlace
  11.  
  12. Connection canal = null;
  13.  
  14. ResultSet tabla= null;
  15.  
  16. Statement instruccion=null;
  17.  
  18.  
  19. // abriendo canal o enlace en su propio try-catch
  20.  
  21. try {
  22.  
  23. Class.forName("com.mysql.jdbc.Driver").newInstance();
  24.  
  25. canal=DriverManager.getConnection("jdbc:mysql:///cablevision_atc", "root", "");
  26.  
  27. instruccion = canal.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  28.  
  29. ResultSet.CONCUR_UPDATABLE);
  30.  
  31. } catch(java.lang.ClassNotFoundException e){} catch(SQLException e) {};
  32.  
  33. //cargando los campos a grabar
  34.  
  35. // excepto clave porque en mysql es de tipo auto-increment
  36.  
  37. int estado = 0;
  38.  
  39.  
  40. String q="insert into ticket(estado_ticket) values(\"" +estado+"\"); ";
  41.  
  42. try {
  43.  
  44. // agregando renglon (insert)
  45.  
  46. int n=instruccion.executeUpdate(q);
  47.  
  48. //avisando que se hizo la instruccion
  49.  
  50. out.println("REGISTRO INSERTADO");
  51.  
  52. imprimir print = new imprimir();
  53.  
  54.  
  55.  
  56.  
  57. } catch(SQLException e) {out.println(e);};
  58.  
  59. try{
  60.  
  61. // tabla.close();
  62.  
  63. instruccion.close();
  64.  
  65. canal.close();
  66.  
  67. } catch(SQLException e) {out.println(e);};
  68.  
  69. };
  70.  
  71.  
  72.  
  73.  
  74. // construyendo forma dinamica
  75.  
  76. out.println("<FORM ACTION=index.jsp METHOD=post>");
  77.  
  78. //out.println("NOMBRE :<INPUT TYPE=TEXT NAME=NOMBRE><BR>");
  79.  
  80. //out.println("EDAD :<INPUT TYPE=TEXT NAME=EDAD><BR>");
  81.  
  82. //out.println("ESTATURA:<INPUT TYPE=TEXT NAME=ESTATURA><BR>");
  83.  
  84. out.println("<INPUT TYPE=SUBMIT NAME=GRABAR VALUE=INSERTAR ><BR>");
  85.  
  86. out.println("</FORM>");
  87.  
  88. %>
  89.  
and this is my class

Expand|Select|Wrap|Line Numbers
  1. package imprimir;
  2.  
  3. import java.sql.*;
  4. import net.sf.jasperreports.engine.xml.JRXmlLoader;
  5. import net.sf.jasperreports.engine.JasperCompileManager;
  6. import net.sf.jasperreports.engine.JasperFillManager;
  7. import net.sf.jasperreports.engine.JasperPrint;
  8. import net.sf.jasperreports.engine.design.JasperDesign;
  9. import net.sf.jasperreports.engine.JasperReport;
  10. import net.sf.jasperreports.view.JasperViewer;
  11.  
  12.  
  13.  
  14. public class imprimir {
  15.  
  16.  
  17.     /**
  18.      * Constructor for ReportDriver
  19.      */
  20.     public imprimir() {
  21.  
  22.     }
  23.  
  24.  
  25.     public static Connection connectDB(String databaseName, String userName, String password) {
  26.         Connection jdbcConnection = null;
  27.         try{
  28.             Class.forName("com.mysql.jdbc.Driver").newInstance ();
  29.             jdbcConnection = DriverManager.getConnection("jdbc:mysql:///cablevision_atc", "root", "");
  30.         }catch(Exception ex) {
  31.             String connectMsg = "Could not connect to the database: " + ex.getMessage() + " " + ex.getLocalizedMessage();
  32.             System.out.println(connectMsg);
  33.         }
  34.         return jdbcConnection;
  35.     }
  36.  
  37.  
  38.     public static void runReport(String databaseName, String userName, String password,String reportFile) {
  39.         try{
  40.             JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
  41.             JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
  42.             Connection jdbcConnection = connectDB(databaseName, userName, password);
  43.             JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, jdbcConnection);
  44.             JasperViewer.viewReport(jasperPrint);
  45.             //JasperPrintManager.printPages(jasperPrint,0,jasperPrint.getPages().size()-1,true);
  46.  
  47.  
  48.         }catch(Exception ex) {
  49.             String connectMsg = "Could not create the report " + ex.getMessage() + " " + ex.getLocalizedMessage();
  50.             System.out.println(connectMsg);
  51.         }
  52.     }
  53.  
  54.  
  55.  
  56.     public static void main(String[] args) {
  57.  
  58.             String databaseName = "cablevision_atc";
  59.             String userName = "root";
  60.             String password = "";
  61.             String reportFile = "C:\\wamp\\www\\Cablevision\\JAVA\\ticket.jrxml";
  62.  
  63.             runReport(databaseName, userName, password, reportFile);
  64.  
  65.         return;
  66.  
  67.     } }
  68.  
Dec 29 '09 #1
0 2069

Sign in to post your reply or Sign up for a free account.

Similar topics

6
4700
by: Eugene A | last post by:
Hello. In this call from a Java prog to a Oracle SP I am trying to pass along some varchars and a cursor, but apperently the syntax of the call to the stor proc is incorrect. What am I doing wrong? Please help. Thanks,
4
3235
by: Jean-Marc Blaise | last post by:
Dear all, I have simulated the windows MULTI application with a java program calling the SQLTP1DL proc referenced as DB2DARI application, on Linux Intel or ZLinux. If the proc is NOT FENCED, there is no pb and the program works fine. If either the proc is FENCED, or FENCED THREADSAFE, I get a SQL1042C, or the instance crashes on ZLinux...
6
2018
by: Samuel | last post by:
New Prizes for October Jrobots Programming Challenge. Jrobots is programming game hosted at <http://www.cfxweb.net/~jrobots> CFXweb. You play by programming in basic Java your own robot to fight other players robots.Use your algorithms to predict opponent movement and position. For the third time in the history of Jrobots , there are...
12
2049
by: karen | last post by:
Hi all : this is going to be a long post. So i apologize in advance :) i am converting a java program in VB right now. I am a java programmer by trade. so i am no expert in this department. I have written the following three class: 1.icoCORE_test 2.icoMINDB_test
2
2583
by: mhust6 | last post by:
I am trying to call a java method from within my Javascript, but cannot seem to get it to work. All the examples I have found online and in the forums are using Java applets. I have a method that I want to call that does a search, and produces a message dialog displaying the results. Also, the class file lives next to the html file. How...
1
10103
by: Radhakrishnans | last post by:
hai friends i am new to java and mysql.i have created stored procedures in mysql.Now i want to call that stored procedures in java.All works fine.but it show the following error. unreported exception java.lang.InstantiationException; must be caught or declared to be thrown Class.forName ("com.mysql.jdbc.Driver").newInstance(); I need...
46
3789
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or property bequeathed to another by will. 2. Something handed down from an ancestor or a predecessor or from the past: a legacy of religious freedom....
11
11991
by: BigZero | last post by:
Hello, can i call Java method.That is java code from php,here what i m looking for i have class called DB in this class all the DB related things ,like DB connection DB insert,DB read etc Methods are defined. "All i m looking for just call the DB method from PHP that Read data from DB".
2
1753
thatos
by: thatos | last post by:
I have a java class exe.class I would like to call this program using c. I tried the following methods none of them seems to work if you know where the problem is please help. main(){ System("java C:/Users/sthato/workspace/NumThe/bin/exe"); execlp("java","C:/Users/sthato/workspace/NumThe/bin/exe"); execlp("C:/Program...
0
7695
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.