0

There is no error while compiling, but I'm getting this error on running my program:

java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver

Here is my code for inserting values in table in SQL Server.

import javax. swing.*;
        import java.awt.*;
        import java.awt.event.*;
        import java.sql.*;

        class login  implements ActionListener

        {
        JFrame loginframe;
        JPanel loginpanel, loginpanel1;
        JLabel name, password, loginhead, loginstatus;
        JTextField nametxt;
        JPasswordField passwordtxt;
        JButton loginsubmit, loginreset;

        public login()
        {
        loginframe=new JFrame();
        loginframe.setSize(300,300);
        loginframe.setDefaultCloseOperation(loginframe.EXIT_ON_CLOSE);
        loginframe.setVisible(true);

        loginpanel=new JPanel();
        loginpanel1=new JPanel(new GridLayout(4,2));

        loginhead=new JLabel("Login");
        loginstatus=new JLabel("");
        name=new JLabel("Name");
        password=new JLabel("Password");

        nametxt=new JTextField(10);
        passwordtxt=new JPasswordField(10);


        loginsubmit=new JButton("submit");
        loginsubmit.addActionListener(this);
        loginreset=new JButton("Reset");
        loginreset.addActionListener(this);



        loginpanel1.add(loginhead);
        loginpanel1.add(loginstatus);
        loginpanel1.add(name);
        loginpanel1.add(nametxt);
        loginpanel1.add(password);
        loginpanel1.add(passwordtxt);
        loginpanel1.add(loginsubmit);
        loginpanel1.add(loginreset);


        loginpanel.add(loginpanel1);

        loginframe.add(loginpanel);

        }
        public void actionPerformed(ActionEvent ae)
        {
        String name=nametxt.getText();
        String pass=passwordtxt.getText();
        if(ae.getSource()==loginsubmit)
        {
        String url1="jdbc:odbc:"+"login";
        try{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection c=DriverManager.getConnection(url1);
        Statement s=c.createStatement();
        PreparedStatement ps=c.prepareStatement("insert into signup values(?,?)");
        s.executeUpdate("create table signup(First_name varchar(20), Password varchar(20))");
        System.out.println("Basic information table created");
        ps.setString(1,name);
        ps.setString(2,pass);
        ps.execute();
        System.out.println("Values inserted");
        s.close();
        c.close();
        }
        catch(Exception e)
        {
        System.out.println(e);
        }
        }


        }

        public static void main(String args[])
        {
        login l=new login();
        }
        }
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Allan
  • 25
  • 2
  • 8

2 Answers2

0

The JDBC-ODBC Bridge has been removed from Java 8. In order to connect to the SQL Server database you will need to use a real JDBC driver. The one offered by Microsoft is:

Microsoft JDBC Driver for SQL Server

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
0

I had same problem when i had to connect my java code to MSAccess. yes The JDBC-ODBC Bridge is removed from Java 8 and later versions. Now if you want to connect java code to database either use jdk7 you can get it here

register and login here for downloading jdk 7.

connect database with traditional method by going through Control Panel-Administrative tool-ODBC etc.

  • If you have jdk 7 it is actually possible to copy the jdbc.odbc-bridge over to jdk 8. (https://support.migration-center.com/hc/en-us/articles/360012716534-How-to-enable-JDBC-ODBC-bridge-for-Java-8-) – jon martin solaas Oct 12 '22 at 22:12