我正在尝试将我的java应用程序与sql server 2012连接,但是却给了我这个错误:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: com.microsoft.sqlserver.jdbc.SQLServerConnection cannot be cast to com.mysql.jdbc.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;
}
}
}
错误:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: com.microsoft.sqlserver.jdbc.SQLServerConnection cannot be cast to com.mysql.jdbc.Connection
at sensores.forms.jTLoginConsulta.<init>(jTLoginConsulta.java:71)
at sensores.forms.jTLoginConsulta$4.run(jTLoginConsulta.java:448)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
jTLoginConsulta
导入:
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;
发生错误的第71行:
68 public jTLoginConsulta() throws SQLException {
69 initComponents();
70 setIcon();
71 conexao=(Connection) CriaConexao.getConexao();
72
73 }
答案 0 :(得分:2)
检查此Connection对象的导入
Connection conexao = DriverManager.getConnection(dbURL);
它应该是java.sql.Connection
。您似乎错误地导入了com.mysql.jdbc.Connection
修改强>
根据编辑问题:
conexao=(Connection) CriaConexao.getConexao();
CriaConexo正在返回com.microsoft.sqlserver.jdbc.SQLServerConnection
,你错误地将它投射到com.mysql.jdbc.Connection
。只需将导入更正为java.sql.Connection
并删除演员表。
希望它有所帮助。