Advertisement
Guest User

Untitled

a guest
Mar 11th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. public class Login {
  2. // JDBC driver name and database URL
  3. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  4. static final String DB_URL = "jdbc:mysql://localhost/CNVS";
  5.  
  6. // Database credentials
  7. static final String USER = "root";
  8. static final String PASS = "root";
  9.  
  10. public static void main(String[] args) {
  11. String userName;
  12. String password;
  13. Connection conn = null;
  14. Statement stmt = null;
  15. Scanner in = new Scanner(System.in);
  16.  
  17. System.out.println("Enter the username");
  18. userName = in.nextLine();
  19. System.out.println("Enter the password");
  20. password = in.nextLine();
  21.  
  22. try {
  23. // STEP 2: Register JDBC driver
  24. Class.forName("com.mysql.jdbc.Driver");
  25.  
  26. // STEP 3: Open a connection
  27. // System.out.println("Connecting to database...");
  28. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  29.  
  30. // STEP 4: Execute a query
  31. // System.out.println("Creating statement...");
  32. stmt = conn.createStatement();
  33. String sql;
  34. sql = "SELECT USER_NAME, PASSWORD FROM USER_INFO WHERE USER_NAME = '" + userName + "'";
  35. ResultSet rs = stmt.executeQuery(sql);
  36. // STEP 5: Extract data from result set
  37.  
  38. while (rs.next()) {
  39. // Retrieve by column name
  40. String dbUserName = rs.getString("USER_NAME");
  41. String dbPassword = rs.getString("PASSWORD");
  42.  
  43. if (userName.equals(dbUserName) && password.equals(dbPassword)) {
  44. System.out.println("Successfully Logged in ! ");
  45. } else {
  46. System.out.println("Please enter the valid login credentials");
  47. }
  48. }
  49.  
  50. // STEP 6: Clean-up environment
  51. rs.close();
  52. stmt.close();
  53. conn.close();
  54. } catch (SQLException se) {
  55. // Handle errors for JDBC
  56. se.printStackTrace();
  57. } catch (Exception e) {
  58. // Handle errors for Class.forName
  59. e.printStackTrace();
  60. } finally {
  61. // finally block used to close resources
  62. try {
  63. if (stmt != null)
  64. stmt.close();
  65. } catch (SQLException se2) {
  66. }
  67. try {
  68. if (conn != null)
  69. conn.close();
  70. } catch (SQLException se) {
  71. se.printStackTrace();
  72. }
  73. }
  74. System.out.println("Goodbye!");
  75. }
  76.  
  77. public class JavaCallJasperReport {
  78. public static void main(String[] args) throws JRException,
  79. ClassNotFoundException, SQLException {
  80.  
  81. String reportSrcFile = "C:/Users/stephenjebaraj_b/Test_Report.jrxml";
  82. // First, compile jrxml file.
  83. JasperReport jasperReport = JasperCompileManager.compileReport(reportSrcFile);
  84.  
  85. Connection conn = MySQLConnUtils.getMySQLConnection();
  86.  
  87. // Parameters for report
  88. Map<String, Object> parameters = new HashMap<String, Object>();
  89. parameterMap.put(USER_NAME, dbUserName);
  90. parameterMap.put(PASSWORD, dbPassword);
  91.  
  92. JasperPrint print = JasperFillManager.fillReport(jasperReport,
  93. parameters, conn);
  94.  
  95. // Make sure the output directory exists.
  96. File outDir = new File("C:/JasperReport_Test");
  97. outDir.mkdirs();
  98.  
  99. // PDF Exportor.
  100. JRPdfExporter exporter = new JRPdfExporter();
  101.  
  102. ExporterInput exporterInput = new SimpleExporterInput(print);
  103. // ExporterInput
  104. exporter.setExporterInput(exporterInput);
  105.  
  106. // ExporterOutput
  107. OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(
  108. "C:/JasperReport_Test/TestJasper.pdf");
  109. // Output
  110. exporter.setExporterOutput(exporterOutput);
  111.  
  112. //
  113. SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
  114. exporter.setConfiguration(configuration);
  115. exporter.exportReport();
  116.  
  117. System.out.print("Done!");
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement