我有在連接到MySQL數據庫的時候我用這code.I問題已經檢查該端口號是3128.So沒有關於that.I問題檢查它,我認爲問題出在使用Java獲取連接到mysql數據庫?
connection= DriverManager.getConnection("jdbc:mysql://localhost:3128/gcc","root", "root");
編譯時沒有錯誤。有人可以幫我解決這個問題嗎?
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.sql.*;
//import java.net.*;
public class Main extends Applet implements ActionListener
{
TextArea tarea;
Button bsubmit;
public void init()
{
setBackground(new Color(0,0,0));
setForeground(Color.white);
Label l1=new Label("Write your code : ");
l1.setFont(new Font("lucida console",Font.PLAIN,25));
l1.setSize(200,30);
tarea=new TextArea();
tarea.setFont(new Font("lucida console",Font.PLAIN,18));
tarea.setForeground(new Color(0,0,0));
tarea.setSize(600,250);
bsubmit=new Button("Submit");
bsubmit.setFont(new Font("lucida console",Font.PLAIN,15));
bsubmit.setBackground(new Color(255,255,255));
bsubmit.setForeground(new Color(0,0,0));
bsubmit.setSize(100,30);
add(l1);
add(tarea);
add(bsubmit);
setLayout(null);
l1.setLocation(40,40);
tarea.setLocation(40,100);
bsubmit.setLocation(40,400);
bsubmit.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==bsubmit)
{
Connection connection=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
connection= DriverManager.getConnection("jdbc:mysql://localhost:3128/gcc","root", "root");
Statement stmt = connection.createStatement();
stmt.executeUpdate("CREATE TABLE test2 (code VARCHAR(254))");
}
catch (Exception e) {}
}
}
}
有什麼樣的問題?它超時了?沒有權限?拒絕訪問? –
如果將'jdbc:mysql:// localhost:3128/gcc'更改爲'jdbc:mysql://127.0.0.1:3128/gcc',它會工作嗎?您還應該在catch塊中打印異常消息,以便查看發生了什麼錯誤。 – drew010
一個空的catch塊是你可以對自己做的最糟糕的事情之一。爲了好心,打印堆棧跟蹤。檢查是否可以使用MySQL管理員訪問該數據庫。如果它不能,那麼Java就不太可能。爲什麼要使用標準3306以外的任何端口?你通過改變完成了什麼? – duffymo