It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

No Suitable Driver , is coming for the following code....

No Suitable Driver , is coming for the following code....

Postby sasi on Wed May 02, 2007 6:12 am

Code: Select all
import javax.swing.JFrame;
import java.sql.*;
import java.io.*;
import java.util.*;
/*
* sasi.java
*
* Created on April 30, 2007, 9:49 AM
*/

/**
*
* @author  sasikrishna1
*/
public class sasi extends javax.swing.JFrame {
   // private Connection cons;
    /** Creates new form sasi */
    public sasi() {

   /*   String url = "jdbc:Microsoft:sqlserver:http://localhost;databaseName:sms";
      String uname = "sa";
      String pwd = "";

      try{
          Class.forName("com.microsoft.sqlserver.jdbc.SqlServerDriver");
            con = DriverManager.getConnection(url,uname,pwd);
          }
         catch(ClassNotFoundException cnfe){
            System.out.println("class not found");
         }
         catch(SQLException e)
      {
            System.out.println("unable to connnect");
      }*/
   
       
        initComponents();
       
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                         
    private void initComponents() {
        jDialog1 = new javax.swing.JDialog();
        jLabel3 = new javax.swing.JLabel();
        jDialog2 = new javax.swing.JDialog();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
      //jTextField2.setEchoChar('*');


        jLabel3.setText("You had clicked the Save Button");

        org.jdesktop.layout.GroupLayout jDialog1Layout = new org.jdesktop.layout.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jDialog1Layout.createSequentialGroup()
                .add(35, 35, 35)
                .add(jLabel3)
                .addContainerGap(211, Short.MAX_VALUE))
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jDialog1Layout.createSequentialGroup()
                .add(35, 35, 35)
                .add(jLabel3)
                .addContainerGap(251, Short.MAX_VALUE))
        );
        jLabel4.setText("You had clicked the Delete button");

        org.jdesktop.layout.GroupLayout jDialog2Layout = new org.jdesktop.layout.GroupLayout(jDialog2.getContentPane());
        jDialog2.getContentPane().setLayout(jDialog2Layout);
        jDialog2Layout.setHorizontalGroup(
            jDialog2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jDialog2Layout.createSequentialGroup()
                .add(29, 29, 29)
                .add(jLabel4)
                .addContainerGap(210, Short.MAX_VALUE))
        );
        jDialog2Layout.setVerticalGroup(
            jDialog2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jDialog2Layout.createSequentialGroup()
                .add(39, 39, 39)
                .add(jLabel4)
                .addContainerGap(247, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Welcome to GotMailer");
      setLocation(240,180);
        jButton1.setText("Save");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Delete");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setText("User Name:");

        jLabel2.setText("Password:");
      

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(48, 48, 48)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(jLabel2))
                        .add(26, 26, 26))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(86, Short.MAX_VALUE)
                        .add(jButton1)
                        .add(35, 35, 35)))
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jButton2)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jTextField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                        .add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)))
                .add(125, 125, 125))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(63, 63, 63)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(40, 40, 40)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel2)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 59, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton2)
                    .add(jButton1))
                .add(75, 75, 75))
        );
        pack();
    }// </editor-fold>                       

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
// TODO add your handling code here:
        //String a = JTextField1.getText();
    }                                           

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
        /* jDialog2.setVisible(true);
         jDialog2.setLocation(120,140);
         jDialog2.setSize(200,200);*/
         String uname = jTextField1.getText();
       System.out.println("the uname is:"+uname);
         String pwd = jTextField2.getText();
       System.out.println("the pwd is:"+pwd);
       Connection con = DB.getConnection();
      
          try{
          Statement stmt = con.createStatement();
          String query = "select * from userinfo";
          ResultSet rs = stmt.executeQuery(query);
          while(rs.next()){
                       if(rs.getString(1).trim().equalsIgnoreCase(uname.trim())){   
                       if(rs.getString(2).trim().equalsIgnoreCase(pwd.trim())){
                           System.out.println("valid username and password");
                       //uname.setText("");
                      // pwd.setText("");
                       }
                       else{
                       System.out.println("Invalid username and passsword");
                       //uname.setText("");
                       //pwd.setText("");
                       }
                  }
                  }
       }
       catch(Exception e){
       }

    }                                       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
         /* jDialog1.setVisible(true);
          jDialog1.setLocation(120,140);
          jDialog1.setSize(200,200);*/
        String uname = jTextField1.getText();
        System.out.println("the uname is:"+uname);
          String pwd = jTextField2.getText();
        System.out.println("the pwd is:"+pwd);
         
    }                                       
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new sasi().setVisible(true);
               
             
            }
        });
    }
   
   
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JDialog jDialog2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;

   //private Connection cons;
   /*private Statement stmt;
   private ResultSet rs;
   private String query;*/
    // End of variables declaration                   
   
}
class DB
{
       public static Connection con;
       public static Connection getConnection(){
        String url = "jdbc:Microsoft:sqlserver:http://localhost;databaseName=sms";
      String username = "sa";
      String password = "";
         try{
      
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
      con = DriverManager.getConnection(url,username,password);
        System.out.println("Database connected");
         }
         catch(ClassNotFoundException e){
            e.printStackTrace();
            //System.err.println("Failed to load JDBC Driver"+e.getMessage());
         }
            catch(SQLException ed){
            System.out.println(ed.getMessage());
         }

         return con;
   }
      public static void closeDB(Connection con)
      {
         if(con != null)
         {
               try{
               con.close();
            }
            catch(SQLException e)
            {
               System.out.println(e.getMessage());
            }
            con = null;
         }
      }
   }

Well i dont know why it is showing that no driver found from the above code, kindly help me on this issue.


regards,
sasi.
sasi
 
Posts: 13
Joined: Mon Apr 23, 2007 1:10 pm

Postby marecki6000 on Sat May 05, 2007 3:55 pm

Ensure yourself, that you have database driver .jar library in your classpath.

Regards
Marek
marecki6000
 
Posts: 6
Joined: Thu Apr 26, 2007 10:18 pm

Postby achilles on Mon May 07, 2007 7:40 pm

... And also ensure that you have the latest Service Pack for MS SQL Server. It is Service pack 4 for SQL Servre 2000 --as of now.

create an Environment Variable (RightClick on My Computer->Properties->Advanced->Environment Variables) named "CLASSPATH" with a value like this: ".;D:\Java\SQLJDBC\sqljdbc.jar;" and also add the sqljdbc.jar to your project's libraries.

I don't exactly know why it's like this, but I did the above things to have it work for me!

and by the way, stick to SQL Server!!! Don't even try to install Oracle! ;) I've been trying to install Oracle 10g for the last 12 hours and i'd experienced no success till now!

good luck! ;)
achilles
 
Posts: 7
Joined: Thu Apr 19, 2007 3:38 pm
Location: Tehran-Iran


Who is online

Users browsing this forum: No registered users and 0 guests