Вы находитесь на странице: 1из 3

import java.sql.

*;

import java.util.Scanner;
import java.util.Random;

public class RandomPasswordGenerator {

private static final String ALPHA_CAPS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

private static final String ALPHA = "abcdefghijklmnopqrstuvwxyz";

private static final String NUM = "0123456789";

private static final String SPL_CHARS = "!@#$%^&*_=+-/";

public static char[] generatePswd(int minLen, int maxLen, int noOfCAPSAlpha,

int noOfDigits, int noOfSplChars) {

if(minLen > maxLen)

throw new IllegalArgumentException("Min. Length > Max. Length!");

if( (noOfCAPSAlpha + noOfDigits + noOfSplChars) > minLen )

throw new IllegalArgumentException

("Min. Length should be atleast sum of (CAPS, DIGITS, SPL CHARS)


Length!");

Random rnd = new Random();

int len = rnd.nextInt(maxLen - minLen + 1) + minLen;

char[] pswd = new char[len];

int index = 0;

for (int i = 0; i < noOfCAPSAlpha; i++) {

index = getNextIndex(rnd, len, pswd);

pswd[index] = ALPHA_CAPS.charAt(rnd.nextInt(ALPHA_CAPS.length()));

for (int i = 0; i < noOfDigits; i++) {

index = getNextIndex(rnd, len, pswd);

pswd[index] = NUM.charAt(rnd.nextInt(NUM.length()));

for (int i = 0; i < noOfSplChars; i++) {


index = getNextIndex(rnd, len, pswd);

pswd[index] = SPL_CHARS.charAt(rnd.nextInt(SPL_CHARS.length()));

for(int i = 0; i < len; i++) {

if(pswd[i] == 0) {

pswd[i] = ALPHA.charAt(rnd.nextInt(ALPHA.length()));

return pswd;

private static int getNextIndex(Random rnd, int len, char[] pswd) {

int index = rnd.nextInt(len);

while(pswd[index = rnd.nextInt(len)] != 0);

return index;

public class JDBCExample {

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";


static final String DB_URL = "jdbc:mysql://localhost/BANK";

static final String USER = "root";


static final String PASS = "";

public static void main(String[] args) {


Connection conn = null;
//Statement stmt = null;
PreparedStatement stmnt = null;
String sql=null;
Scanner s= new Scanner(System.in);
int opt;
try{

Class.forName(JDBC_DRIVER);
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
do
{
System.out.print("\n user unique. \n2.forgot or reset
password\n3.acknowledgement. \n4.EXIT\nEnter your choice:");
opt=s.nextInt();
switch(opt)
{
case 1:
System.out.print("Enter your unique id:\t");
v1=s.nextInt(); s.nextLine();
System.out.print("Enter your FIRST NAME:\t");
v2=s.nextLine();
System.out.print("Enter your LAST NAME:\t");
v3=s.nextLine();
sql = "INSERT INTO customers (acc_num,f_name,l_name,address,open_dep)
VALUES(?,?,?,?,?)" ;
stmnt = conn.prepareStatement(sql);
stmnt.setInt(1, v1);
stmnt.setString(2, v2);
stmnt.setString(3, v3);
stmnt.setString(4, v4);
stmnt.setInt(5, v5);
stmnt.executeUpdate();

}}while(opt<=5);
}catch(SQLException se){

se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmnt!=null)
stmnt.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("\nHave a nice day ahead!");
}
}

Вам также может понравиться