连接到远程数据库时出现JDBC错误

时间:2017-05-03 16:00:56

标签: java mysql jdbc

我创建了一个远程数据库in this website

enter image description here

然后我创建了一个表

enter image description here

之后,我尝试使用此Java代码连接到我的远程数据库:

try {
    Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} 
try{  
   Connection con = DriverManager.getConnection("jdbc:mysql://www.000webhost.com/id1538634_mydrive","root","id1538634_root");
   PreparedStatement ps = con.prepareStatement("insert into membre(name,pseudo,pw,profilephoto) values(?,?,?,?)");
   FileInputStream is = new FileInputStream(new File(s));
   ps.setString(1, t1.getText());
   ps.setString(2, t2.getText());
   ps.setString(3,  pw);
   ps.setBinaryStream(4,   (InputStream)is, (int)( new File(s).length() ));
   ps.executeUpdate();
   JOptionPane.showMessageDialog(null, "Registration with success"); 
}catch(Exception ex){
   ex.printStackTrace();
}

编译时,我什么也得不到。没有异常也没有错误,也没有插入数据。

1 个答案:

答案 0 :(得分:0)

您必须对my.cnf文件进行以下更改

`my.cnf`

bind-address = www.000webhost.com (OR)
bind-address = xx.xx.xx.xx (IP Address)

如果您没有任何用户登录问题,您应该能够从Java代码连接到MySQL数据库。