如题,我需要用Java来连接MySQL数据库,但是这个数据库实在别的服务器上,地址是: 208.81.166.169 ,现在我需要连接该数据库,假定用户名是:hythzxSQL,密码:727799796,请问该如何写呢?下面是我写的片段,我是初学者,请多包涵。
- Java code
try { //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Class.forName("com.mysql.jdbc.Driver"); } catch(ClassNotFoundException e2){} try { //Connection con = DriverManager.getConnection("jdbc:odbc:mail"); Connection con = DriverManager.getConnection("jdbc:mysql://208.81.166.169:3306/hythzxSQL","hythzxSQL","727799796"); Statement stmt = con.createStatement(); String sql = "SELECT user,password FROM log"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { name=rs.getString("user"); password=rs.getString("password"); boolean name_password1=tf1.getText().equals(name); boolean name_password2=tf2.getText().equals(password); if(name_password1==true &&name_password2==true) { System.out.println("登录成功!"); } else { System.out.println("用户名或密码错误!"); } } rs.close(); stmt.close(); con.close(); } catch(Exception e3){}
------解决方案--------------------