sql server - Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException -


i'm trying connect java application sql server 2012, giving me error:

exception in thread "awt-eventqueue-0" java.lang.classcastexception:  com.microsoft.sqlserver.jdbc.sqlserverconnection cannot cast com.mysql.jdbc.connection 

can can me please?

thank much.

code of connection:

 import java.sql.connection;  import java.sql.drivermanager;  import java.sql.sqlexception;    public class criaconexao {   public static connection getconexao()throws sqlexception{     try{          class.forname("com.microsoft.sqlserver.jdbc.sqlserverdriver");        string dburl = "jdbc:sqlserver://brgdb:1433;database=db_sql;integratedsecurity=true";             connection conexao = drivermanager.getconnection(dburl);             return conexao;      }catch(exception e) {         e.printstacktrace();         return null;     }  }    } 

error:

exception in thread "awt-eventqueue-0" java.lang.classcastexception: com.microsoft.sqlserver.jdbc.sqlserverconnection cannot cast com.mysql.jdbc.connection     @ sensores.forms.jtloginconsulta.<init>(jtloginconsulta.java:71)     @ sensores.forms.jtloginconsulta$4.run(jtloginconsulta.java:448)     @ java.awt.event.invocationevent.dispatch(invocationevent.java:311)     @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:744)     @ java.awt.eventqueue.access$400(eventqueue.java:97)     @ java.awt.eventqueue$3.run(eventqueue.java:697)     @ java.awt.eventqueue$3.run(eventqueue.java:691)     @ java.security.accesscontroller.doprivileged(native method)     @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:75)     @ java.awt.eventqueue.dispatchevent(eventqueue.java:714)     @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:201)     @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:116)     @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:105)     @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:101)     @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:93)     @ java.awt.eventdispatchthread.run(eventdispatchthread.java:82) 

jtloginconsulta imports:

import com.mysql.jdbc.connection; import com.mysql.jdbc.preparedstatement; import java.awt.*; import java.sql.resultset; import java.sql.sqlexception; import javax.swing.*; import org.openide.util.exceptions; import sensores.basedados.criaconexao; import sensores.forms.jtmainmenu; import com.mysql.jdbc.preparedstatement; import java.awt.color; import java.awt.toolkit; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; import javax.swing.joptionpane; import java.awt.*; import java.sql.*; import java.awt.event.windowevent; import java.io.bufferedreader; import java.io.file; import java.io.filereader; import java.io.ioexception; import java.io.printwriter; import java.text.dateformat; import java.text.simpledateformat; import java.util.stringtokenizer; import java.util.timertask; import javax.swing.*; import sensores.basedados.criaconexao; import sensores.logica.logins; import java.util.timer; import java.util.timertask; import java.util.*; import java.util.list; import sensores.logica.alarmes; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; import javax.mail.address; import javax.mail.message.recipienttype; 

line 71 error occurs:

68    public jtloginconsulta() throws sqlexception { 69          initcomponents(); 70        seticon(); 71      conexao=(connection) criaconexao.getconexao(); 72     73       } 

check import connection object

connection conexao = drivermanager.getconnection(dburl); 

it should java.sql.connection. seems have imported wrongly com.mysql.jdbc.connection

edit

as per edited question:

 conexao=(connection) criaconexao.getconexao(); 

criaconexo returning com.microsoft.sqlserver.jdbc.sqlserverconnection , casting wrongly com.mysql.jdbc.connection. correct import java.sql.connection , remove cast well.

hope helps.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -