import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.*;
import java.util.Vector;
public class Conexao
{
private Connection conn;
public Vector linha;
public Conexao() throws SQLException
{
try {
//Class.forName("oracle.jdbc.OracleDriver");
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver read");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("Conexao quase completa");
//String user = "c##devKamil";
//String pass = "pass123";
String user = "SYS AS SYSDBA";
String pass = "bd2.isctem";
String database = "jdbc:mysql://localhost:1521/orcl";
System.out.println("Conexao quase quase completa");
conn = DriverManager.getConnection(database,user,pass);
System.out.println("Conexao completa");
}
public Connection getConnection() { return conn; }
public String msg()
{
String s = JOptionPane.showInputDialog(null, "Introduza a query que voce deseja:");
return s;
}
public void pesquisa()
{
try
{
//String sql = msg();
String sql = "SELECT * FROM Cliente";
Statement st= conn.createStatement();
ResultSet rs = st.executeQuery(sql);
ResultSetMetaData md = rs.getMetaData();
int colunas = md.getColumnCount();
Vector nomeColuna= new Vector();
Vector dados = new Vector();
linha = new Vector(colunas);
for (int i =1; i<= colunas; i++)
{
nomeColuna.addElement(md.getColumnName(i));
}
while(rs.next())
{
for(int i=1; i<= colunas;i++)
{
linha.addElement(rs.getObject(i));
}
}
String l="";
for (int i=0; i<linha.size();i++)
{
l += linha.elementAt(i) + "\n";
}
JOptionPane.showMessageDialog(null, l);
st.close();
conn.close();
rs.close();
}
catch(Exception io)
{
JOptionPane.showMessageDialog(null, io.getMessage());
}
}
public void criarTabela(String query) throws Exception
{
try {
new Conexao();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Statement st = null;
try {
st = conn.createStatement();
st.executeUpdate(query);
st.close();
JOptionPane.showMessageDialog(null, "Tabelas criada com sucesso!");
conn.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
public static void main(String[] args) throws Exception {
new Conexao();
}
}
代码在消息“Conexao quase quase completa”处停止,并给出以下错误:
Exception in thread "main" com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at [email protected]/com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
at [email protected]/com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
at [email protected]/com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:815)
at [email protected]/com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:438)
at [email protected]/com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
at [email protected]/com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:189)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:681)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229)
at Conexao.<init>(Conexao.java:35)
at Conexao.main(Conexao.java:121)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at [email protected]/com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
at [email protected]/com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:104)
at [email protected]/com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:149)
at [email protected]/com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:165)
at [email protected]/com.mysql.cj.protocol.a.NativeProtocol.readMessage(NativeProtocol.java:582)
at [email protected]/com.mysql.cj.protocol.a.NativeProtocol.readServerCapabilities(NativeProtocol.java:537)
at [email protected]/com.mysql.cj.protocol.a.NativeProtocol.beforeHandshake(NativeProtocol.java:425)
at [email protected]/com.mysql.cj.protocol.a.NativeProtocol.connect(NativeProtocol.java:1426)
at [email protected]/com.mysql.cj.NativeSession.connect(NativeSession.java:133)
at [email protected]/com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:935)
at [email protected]/com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:805)
... 7 more
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
at [email protected]/com.mysql.cj.protocol.FullReadInputStream.readFully(FullReadInputStream.java:67)
at [email protected]/com.mysql.cj.protocol.a.SimplePacketReader.readHeaderLocal(SimplePacketReader.java:81)
at [email protected]/com.mysql.cj.protocol.a.SimplePacketReader.readHeader(SimplePacketReader.java:63)
at [email protected]/com.mysql.cj.protocol.a.SimplePacketReader.readHeader(SimplePacketReader.java:45)
at [email protected]/com.mysql.cj.protocol.a.NativeProtocol.readMessage(NativeProtocol.java:576)
... 13 more
我在互联网上尝试了多种解决方案,但似乎没有任何效果。有谁可以帮忙吗?
您加载 MySQL 驱动程序:
Class.forName("com.mysql.cj.jdbc.Driver");
然后您尝试使用看似 Oracle 端口和 SID 的内容连接到服务器
如果您想连接到 Oracle,则使用 Oracle 驱动程序和 连接字符串:
Class.forName("oracle.jdbc.OracleDriver");
String database = "jdbc:oracle:thin:@localhost:1521:orcl";
如果您想连接到 MySQL,请使用 MySQL 连接字符串:
String database = "jdbc:mysql://localhost/dbname";
但不要混淆两者。