Skip to main content

JDBC Demo Login using Netbeans and SQL Server step by step

 

1.First Create Database and Table in SQL Server

  create database JavaTest
  create table Login
  (
    username varchar(20),
    userpass varchar(20)
  )

insert into Login values('dilip','password')

2. Create DSN Name using ODBC

Start –>Control Panel->Administrative Tools->Data Sources (ODBC)

1

2

3

4

5

6

7

8

3. Open Netbeans

9

10

11

12

4.Create a class for Database connection name DBC

  1: package javaapplication13;
  2: 
  3: /**
  4:  *
  5:  * @author Dilip
  6:  */
  7: import java.sql.*;
  8: import java.util.logging.Level;
  9: import java.util.logging.Logger;
 10: public class DBC {
 11:    static Connection con;
 12:     public static Connection getcon()
 13:     {
 14:         try {
 15:             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 16:             con = DriverManager.getConnection("jdbc:odbc:javadsn","sa","niitbpld95");
 17:             return con;
 18:         } catch (ClassNotFoundException ex) {
 19:             Logger.getLogger(DBC.class.getName()).log(Level.SEVERE, null, ex);
 20:         }
 21:         catch(SQLException ex)
 22:         {
 23:             System.out.print("Error"+ex);
 24:         }
 25:         return con;
 26: 
 27:     }
 28: }
 29: 




5. Create a Frame name Login


13


6. Now right click on Login button –>event->Action->ActionPerformed


14

  1: package javaapplication13;
  2: 
  3: /**
  4:  *
  5:  * @author Dilip
  6:  */
  7: import java.sql.*;
  8: import javax.swing.JOptionPane;
  9: public class Login extends javax.swing.JFrame {
 10:     static Connection con;
 11:     Statement stat;
 12:   PreparedStatement st;
 13:     ResultSet rs;
 14: 
 15:     /** Creates new form Login */
 16:     public Login() {
 17:         initComponents();
 18:     }
 19: 
 20:    
 21:     private void btnloginActionPerformed(java.awt.event.ActionEvent evt) {                                         
 22:        try {
 23:             String username=txtuser.getText();
 24:             String password=txtpass.getText();
 25:             String str="SELECT * FROM Login";
 26:             con=DBC.getcon();
 27:             if(con!=null)
 28:             {
 29: 
 30:             stat=con.createStatement();
 31:             rs=stat.executeQuery(str);
 32:             rs.next();
 33:             String s=rs.getString("username");
 34:             String p=rs.getString("userpass");
 35:                 if ((username.equals(s)) && (password.equals(p)))
 36:                 {
 37:                     JOptionPane.showMessageDialog(this,"User Id and Password is correct");
 38:                 }
 39:                 else
 40:                 {
 41:                     JOptionPane.showMessageDialog(this,"Invalid User Name and Password");
 42:                 }
 43:             }
 44:             else
 45:             {
 46:                  JOptionPane.showMessageDialog(this, "Connection failed");
 47:             }
 48:         }
 49: 
 50:         catch (SQLException ex) {
 51:             ex.printStackTrace();
 52: 
 53:         }        // TODO add your handling code here:
 54: 
 55:     } 
 56: }
 57:  
 58:                                       
 59: 
 60:    
7. Now build the project
 
15



8. Now run the Login frame


16


9.Enter the user name and password


output


Comments

Popular posts from this blog

WAP to calculate the monthly telephone bills as per the following rule: Minimum Rs. 200 for upto 100 calls. Plus Rs. 0.60 per call for next 50 calls. Plus Rs. 0.50 per call for next 50 calls. Plus Rs. 0.40 per call for any call beyond 200 calls.

  #include<iostream.h> #include<conio.h> void main() { int calls; float bill; cout<<" Enter number of calls : "; cin>>calls; if (calls<=100) bill=200; else if (calls>100 && calls<=150) { calls=calls-100; bill=200+(0.60*calls); } else if (calls>150 && calls<=200) { calls=calls-150; bill=200+(0.60*50)+(0.50*calls); } else { calls=calls-200; bill=200+(0.60*50)+(0.50*50)+(0.40*calls); } cout<<" Your bill is Rs. "<<bill; getch(); }   Output: Enter number of calls : 190 Your bill is Rs.250

C++ Program to define a Class BOOK and accessing member function using its object.

  #include<iostream.h> #include<stdio.h> #include<conio.h> class BOOK { int BOOKNO; char BOOKTITLE[20]; float PRICE; void TOTAL_COST( int N) { float tcost; tcost=PRICE*N; cout<<tcost; } public : void INPUT() { cout<<" Enter Book Number "; cin>>BOOKNO; cout<<" Enter Book Title "; gets(BOOKTITLE); cout<<" Enter price per copy "; cin>>PRICE; } void PURCHASE() { int n; cout<<" Enter number of copies to purchase "; cin>>n; cout<<" Total cost is "; TOTAL_COST(n); } }; void main() { BOOK obj; obj.INPUT(); obj.PURCHASE(); getch(); }

Addition of Two Number using servlet and jsp

index.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > < html > < head > < meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" > < title > JSP Page </ title > </ head > < body bgcolor ="yellow" > < center >< h1 > Addition of Two Number </ h1 ></ center > < form action =" additionservlet " method ="post" > < table border ="0" width ="100" align ="center" > < tr > < td > First Number </ td > < td >< input type ="text" name ="txtnum1" value ="" /></ td > ...