Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Login {
- // JDBC driver name and database URL
- static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- static final String DB_URL = "jdbc:mysql://localhost/CNVS";
- // Database credentials
- static final String USER = "root";
- static final String PASS = "root";
- public static void main(String[] args) {
- String userName;
- String password;
- Connection conn = null;
- Statement stmt = null;
- Scanner in = new Scanner(System.in);
- System.out.println("Enter the username");
- userName = in.nextLine();
- System.out.println("Enter the password");
- password = in.nextLine();
- try {
- // STEP 2: Register JDBC driver
- Class.forName("com.mysql.jdbc.Driver");
- // STEP 3: Open a connection
- // System.out.println("Connecting to database...");
- conn = DriverManager.getConnection(DB_URL, USER, PASS);
- // STEP 4: Execute a query
- // System.out.println("Creating statement...");
- stmt = conn.createStatement();
- String sql;
- sql = "SELECT USER_NAME, PASSWORD FROM USER_INFO WHERE USER_NAME = '" + userName + "'";
- ResultSet rs = stmt.executeQuery(sql);
- // STEP 5: Extract data from result set
- while (rs.next()) {
- // Retrieve by column name
- String dbUserName = rs.getString("USER_NAME");
- String dbPassword = rs.getString("PASSWORD");
- if (userName.equals(dbUserName) && password.equals(dbPassword)) {
- System.out.println("Successfully Logged in ! ");
- } else {
- System.out.println("Please enter the valid login credentials");
- }
- }
- // STEP 6: Clean-up environment
- rs.close();
- stmt.close();
- conn.close();
- } catch (SQLException se) {
- // Handle errors for JDBC
- se.printStackTrace();
- } catch (Exception e) {
- // Handle errors for Class.forName
- e.printStackTrace();
- } finally {
- // finally block used to close resources
- try {
- if (stmt != null)
- stmt.close();
- } catch (SQLException se2) {
- }
- try {
- if (conn != null)
- conn.close();
- } catch (SQLException se) {
- se.printStackTrace();
- }
- }
- System.out.println("Goodbye!");
- }
- public class JavaCallJasperReport {
- public static void main(String[] args) throws JRException,
- ClassNotFoundException, SQLException {
- String reportSrcFile = "C:/Users/stephenjebaraj_b/Test_Report.jrxml";
- // First, compile jrxml file.
- JasperReport jasperReport = JasperCompileManager.compileReport(reportSrcFile);
- Connection conn = MySQLConnUtils.getMySQLConnection();
- // Parameters for report
- Map<String, Object> parameters = new HashMap<String, Object>();
- parameterMap.put(USER_NAME, dbUserName);
- parameterMap.put(PASSWORD, dbPassword);
- JasperPrint print = JasperFillManager.fillReport(jasperReport,
- parameters, conn);
- // Make sure the output directory exists.
- File outDir = new File("C:/JasperReport_Test");
- outDir.mkdirs();
- // PDF Exportor.
- JRPdfExporter exporter = new JRPdfExporter();
- ExporterInput exporterInput = new SimpleExporterInput(print);
- // ExporterInput
- exporter.setExporterInput(exporterInput);
- // ExporterOutput
- OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(
- "C:/JasperReport_Test/TestJasper.pdf");
- // Output
- exporter.setExporterOutput(exporterOutput);
- //
- SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
- exporter.setConfiguration(configuration);
- exporter.exportReport();
- System.out.print("Done!");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement