程序源代码: mysql权限已经设置好了,求帮忙啊
import java.sql.DriverManager; import java.sql.ResultSet; import com.mysql.jdbc.Connection; import com.mysql.jdbc.Statement; public class ConnectMySQLTest { private Connection connect=null; private Statement stmt =null; private ResultSet rs =null; public String connect() { String idString=""; try { Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序 //Class.forName("org.gjt.mm.mysql.Driver"); System.out.println("Success loading Mysql Driver!"); } catch( Exception e) { System.out.print("Error loading Mysql Driver!"); e.printStackTrace(); } try { //connect =(Connection) DriverManager.getConnection("jdbc:mysql://192.168.1.70:3306/devices"); connect = (Connection) DriverManager.getConnection( "jdbc:mysql://192.168.1.70:3306/devices","tom","8950615"); //连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码 System.out.println("Success connect Mysql server!"); stmt = (Statement) connect.createStatement(); rs = stmt.executeQuery("select * from tb_equip"); //user 为你表的名称 if (rs.next()) { System.out.println(rs.getString("id")); idString = rs.getString("id"); } } catch (Exception e) { // System.out.print("get data error!"); e.printStackTrace(); } closeDataBase(); return idString; } public void closeDataBase() { try { if(rs != null) rs.close(); if(stmt != null) stmt.close(); if(connect!= null) connect.close(); } catch (Exception e) { // TODO: handle exception } } }
然后在main函数调用该类,再调用connect()方法,这个是在另外一台电脑运行,
估计网络不通吧,使用mysql命令行看看,或者eclipse中直接配置数据源连接测试一下
或者使用的mysql jar包版本不对
已经找到问题所在,谢谢