Assignment-1
package R100212049.AbhinavKhurana.ques1;
import java.io.*;
import java.sql.*;
import java.util.*;
public class q1 {
public static void main(String[] args) throws Exception {
String h = null;
StringBuffer sb;
Class.forName("com.mysql.jdbc.Driver");
Properties prop=new Properties();
String url="jdbc:mysql://localhost:3306/db1";
prop.setProperty("user", "root");
prop.setProperty("password", "admin");
Connection conn= DriverManager.getConnection(url, prop);
Statement stmt=conn.createStatement();
// stmt.executeUpdate("create table INFO(name varchar(30),pass varchar(30))");
do{
System.out.println("LOGIN SYSTEM");
System.out.println("Enter your choice");
System.out.println("1.Register");
System.out.println("2.Login");
System.out.println("3.View Users");
System.out.println("4.Show Database State");
Scanner sc= new Scanner(System.in);
int i=sc.nextInt();
switch (i) {
case 1:
sb=new StringBuffer();
System.out.println("Enter username");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String fname=br.readLine();
for(int l=0;l<fname.length();l++)
{
char ch=fname.charAt(l);
ch=(char) (ch+1);
sb.append(ch);
}
fname=new String(sb);
System.out.println("Enter the password");
String pwd=br.readLine();
System.out.println("ReEnter your password ");
String rpwd=br.readLine();
sb=new StringBuffer();
if(pwd.equals(rpwd))
{
for(int l=0;l<pwd.length();l++)
{
char r=pwd.charAt(l);
r=(char)(r+1);
sb.append(r);
}
pwd=new String(sb);
System.out.println("Account Sucessfully created");
PreparedStatement pst= conn.prepareStatement("insert into INFO
values(?,?)");
pst.setString(1,fname);
pst.setString(2,pwd);
pst.executeUpdate();
}
else
{
System.out.println("Password doesn't match");
}
break;
case 2:
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Username");
String lname=b.readLine();
System.out.println("Enter Password");
String lpwd=b.readLine();
sb=new StringBuffer();
for(int l=0;l<lname.length();l++)
{
char ch1=lname.charAt(l);
ch1=(char) (ch1+1);
sb.append(ch1);
}
lname=new String(sb);
PreparedStatement st=conn.prepareStatement("select pass from INFO where
name=?");
st.setString(1,lname);
ResultSet rs=st.executeQuery();
sb=new StringBuffer();
while(rs.next())
{
h=rs.getString(1);
for(int l=0;l<h.length();l++)
{
char r=h.charAt(l);
r=(char)(r-1);
sb.append(r);
}
h=new String(sb);
if(h.equals(lpwd))
{
System.out.println("Login successfully");
}
else
{
System.out.println("Wrong Password");
}
}
break;
case 3:
ResultSet ro=stmt.executeQuery("select name from INFO");
while(ro.next())
{
sb=new StringBuffer();
h=ro.getString(1);
for(int m=0;m<h.length();m++)
{
char r=h.charAt(m);
r=(char)(r-1);
sb.append(r);
}
h=new String(sb);
System.out.println(h);
}
break;
case 4:
ResultSet r=stmt.executeQuery("select * from INFO");
while(r.next())
{
System.out.println(r.getString(1)+" "+r.getString(2));
}
break;
default:
System.out.println("Wrong choice");
break;
}
}while (true);
}
}

Java assgn

  • 1.
    Assignment-1 package R100212049.AbhinavKhurana.ques1; import java.io.*; importjava.sql.*; import java.util.*; public class q1 { public static void main(String[] args) throws Exception { String h = null; StringBuffer sb; Class.forName("com.mysql.jdbc.Driver"); Properties prop=new Properties(); String url="jdbc:mysql://localhost:3306/db1"; prop.setProperty("user", "root"); prop.setProperty("password", "admin"); Connection conn= DriverManager.getConnection(url, prop); Statement stmt=conn.createStatement(); // stmt.executeUpdate("create table INFO(name varchar(30),pass varchar(30))"); do{ System.out.println("LOGIN SYSTEM"); System.out.println("Enter your choice"); System.out.println("1.Register"); System.out.println("2.Login"); System.out.println("3.View Users"); System.out.println("4.Show Database State"); Scanner sc= new Scanner(System.in); int i=sc.nextInt(); switch (i) { case 1: sb=new StringBuffer(); System.out.println("Enter username"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String fname=br.readLine(); for(int l=0;l<fname.length();l++) { char ch=fname.charAt(l); ch=(char) (ch+1); sb.append(ch); } fname=new String(sb);
  • 2.
    System.out.println("Enter the password"); Stringpwd=br.readLine(); System.out.println("ReEnter your password "); String rpwd=br.readLine(); sb=new StringBuffer(); if(pwd.equals(rpwd)) { for(int l=0;l<pwd.length();l++) { char r=pwd.charAt(l); r=(char)(r+1); sb.append(r); } pwd=new String(sb); System.out.println("Account Sucessfully created"); PreparedStatement pst= conn.prepareStatement("insert into INFO values(?,?)"); pst.setString(1,fname); pst.setString(2,pwd); pst.executeUpdate(); } else { System.out.println("Password doesn't match"); } break; case 2: BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter Username"); String lname=b.readLine(); System.out.println("Enter Password"); String lpwd=b.readLine(); sb=new StringBuffer(); for(int l=0;l<lname.length();l++) { char ch1=lname.charAt(l); ch1=(char) (ch1+1); sb.append(ch1); } lname=new String(sb); PreparedStatement st=conn.prepareStatement("select pass from INFO where name=?"); st.setString(1,lname); ResultSet rs=st.executeQuery(); sb=new StringBuffer(); while(rs.next()) { h=rs.getString(1); for(int l=0;l<h.length();l++) {
  • 3.
    char r=h.charAt(l); r=(char)(r-1); sb.append(r); } h=new String(sb); if(h.equals(lpwd)) { System.out.println("Loginsuccessfully"); } else { System.out.println("Wrong Password"); } } break; case 3: ResultSet ro=stmt.executeQuery("select name from INFO"); while(ro.next()) { sb=new StringBuffer(); h=ro.getString(1); for(int m=0;m<h.length();m++) { char r=h.charAt(m); r=(char)(r-1); sb.append(r); } h=new String(sb); System.out.println(h); } break; case 4: ResultSet r=stmt.executeQuery("select * from INFO"); while(r.next()) { System.out.println(r.getString(1)+" "+r.getString(2)); } break; default: System.out.println("Wrong choice"); break; } }while (true); } }