0

An SQL EXCEPTION is being caught saying "java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified". I have used the same connecting code in swings, in which the code was executing perfectly including SQL statement execution. Please help me if any changes can be made to the code.

    import javax.servlet.*;
    import java.io.*;
    import java.sql.*;
    import javax.servlet.http.*;

    public class forwardeg23 extends HttpServlet
    {
        Statement st;
        public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
        {
            PrintWriter out = res.getWriter();
            out.println("this is from forwardeg23");        
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:mysqllatest","root","tiger");
            out.println("Atleast Connection Established");
            st = con.createStatement();
         }
         catch(Exception ef){out.println(ef+"\n Error in creating connection");}

            try{
             st.executeUpdate("INSERT INTO student(Name,Marks) VALUES ('ABC',85)");
            }
            catch(Exception af){out.println("Exception HERE");}
         }


         }
6
  • 2
    Does your Swing app work on the same host? Is the ODBC data source really available on the servlet container's host? Have you tried to use a native JDBC driver to connect to MySQL (I would not use the JDBC/ODBC bridge)? Commented Sep 27, 2013 at 8:12
  • please can you elaborate on "is the odbc data source really available on the servlet container's host" @Beryllium Commented Sep 27, 2013 at 8:34
  • @Beryllium . Yes my swing app works on the same host. Is using the JDBC/ODBC bridge the reason for the program not to work. Commented Sep 27, 2013 at 8:37
  • AFAIK the ODBC data source must be defined somewhere in Windows (this is required as well if you use Access etc.). The Swing app does work, and the servlet fails (same host, same JDBC/ODBC bridge, same data source name)? Commented Sep 27, 2013 at 9:16
  • @Beryllium. The ODBC datasource has been created in my system. It is working well when i use it in my swing application but when i try to use it in my J2EE application it is not able to connect to the database. Is the problem due to usage of type 1 driver rather than 100% pure java driver Commented Sep 27, 2013 at 10:36

1 Answer 1

1

Problem is pretty straightforward java.sql.SQLException

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified  

It means that you have not created any DSN.

You need to create DSN in windows 7 using following steps

  1. click on "Start" button -> click on "Control Panel"
  2. change view in control panel -> view by : small icon
  3. click on "Administrative Tool".
  4. Duble Click on "Data Sources(ODBC)"
  5. Select "User DSN" tab. -> Select "Access database" option -> click on "ADD" button -> click on "Select" buttton -> select "Microsoft Access Driver(*.mdb) -> click on "Finish"
  6. new window open. -> type Data Source Name = CustomeDSN (Note: Please type same to same) -> type Description= CustomeDSN -> click on "Select" button (note: select path from :c:/program file/smart solution/weighning management/system/WehingDB.mdb) (note: if WehingDB.mdb file not apear then please this location and copy provide admin right and share it. if again not apear then open explorer then cut that file on desktop then reastart computer and past it again samr location.)
  7. click on "Finish"

Also, Note that if your first try block throws an exception then it will execute next try block which is bad practice.

Disavantages of Type1 driver

Use Type 4 Driver - Database-Protocol Driver(Pure Java Driver) as it is 100% pure Java.

Useful links

  1. Types of Drivers
  2. Type 4 driver with mysql database tutorial
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, but i have already created a DSN connecting to a MySql database and it is working perfectly when i use it in a SWINGS application. There is only a problem when i am trying to connect in J2EE.
I ran your code on my local machine, Code is running perfectly fine even for servlet. Are you still getting same exception?
Yes, did you use the first type of driver??
i used the software : mysql-connector-odbc-5.2.2 and tomcat for deployement of servlet
Yes same code copy paste, just user name and password different.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.