首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

eclipse连接mysql的有关问题

2012-10-08 
eclipse连接mysql的问题。import java.sql.*public class MysqlJdbc {public static void main(String arg

eclipse连接mysql的问题。
import java.sql.*;
public class MysqlJdbc {
  public static void main(String args[]) {
  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 {
  Connection connection = DriverManager.getConnection(
  "jdbc:mysql://localhost:3306/test","root","8566");
  //连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码

  System.out.println("Success connect Mysql server!");
  Statement stmt = connection.createStatement();
  ResultSet rs = stmt.executeQuery("select * from user");
  //user 为你表的名称
  while (rs.next()) {
  System.out.println(rs.getString("name"));
  }
  }
  catch (Exception e) {
  System.out.print("get data error!");
  e.printStackTrace();
  }
  }
}

运行后出现下面信息,不知道是怎么了?
<ConnectionProperties>
 <PropertyCategory name="Connection/Authentication">
  <Property name="user" required="No" default="" sortOrder="-2147483647" since="all">
  The user to connect as
  </Property>
  <Property name="password" required="No" default="" sortOrder="-2147483646" since="all">
  The password to use when connecting
  </Property>
  <Property name="socketFactory" required="No" default="com.mysql.jdbc.StandardSocketFactory" sortOrder="4" since="3.0.3">
  The name of the class that the driver should use for creating socket connections to the server. This class must implement the interface 'com.mysql.jdbc.SocketFactory' and have public no-args constructor.
  </Property>
  <Property name="connectTimeout" required="No" default="0" sortOrder="9" since="3.0.1">
  Timeout for socket connect (in milliseconds), with 0 being no timeout. Only works on JDK-1.4 or newer. Defaults to '0'.
  </Property>
  <Property name="socketTimeout" required="No" default="0" sortOrder="10" since="3.0.1">
  Timeout on network socket operations (0, the default means no timeout).
  </Property>
  <Property name="useConfigs" required="No" default="" sortOrder="2147483647" since="3.1.5">
  Load the comma-delimited list of configuration properties before parsing the URL or applying user-specified properties. These configurations are explained in the 'Configurations' of the documentation.
  </Property>
  <Property name="interactiveClient" required="No" default="false" sortOrder="alpha" since="3.1.0">
  Set the CLIENT_INTERACTIVE flag, which tells MySQL to timeout connections based on INTERACTIVE_TIMEOUT instead of WAIT_TIMEOUT
  </Property>
  <Property name="localSocketAddress" required="No" default="" sortOrder="alpha" since="5.0.5">
  Hostname or IP address given to explicitly configure the interface that the driver will bind the client side of the TCP/IP connection to when connecting.


  </Property>
  <Property name="propertiesTransform" required="No" default="" sortOrder="alpha" since="3.1.4">
  An implementation of com.mysql.jdbc.ConnectionPropertiesTransform that the driver will use to modify URL properties passed to the driver before attempting a connection
  </Property>
  <Property name="useCompression" required="No" default="false" sortOrder="alpha" since="3.0.17">
  Use zlib compression when communicating with the server (true/false)? Defaults to 'false'.
  </Property>
 </PropertyCategory>
 <PropertyCategory name="Networking">
  <Property name="tcpKeepAlive" required="No" default="true" sortOrder="alpha" since="5.0.7">
  ……
下面还有好多,贴不完了。。。

[解决办法]
运行方式不对,上面的信息肯定不是运行你这个main函数输出的... 也许你在运行一个web程序
[解决办法]
import java.sql.*;
  
public class TestMysqlConnection {
  
public static void main(String[] args) {
 Connection conn = null;
 Statement stmt = null;
 ResultSet rs = null;
 try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from dept");
while (rs.next()) {
System.out.println(rs.getString("deptno"));
}
 } catch (ClassNotFoundException e) {
e.printStackTrace();
 } catch (SQLException ex) {
System.out.println("SQLException:" + ex.getMessage());
System.out.println("SQLState:" + ex.getSQLState());
System.out.println("VendorError:" + ex.getErrorCode());
 }
}
}
[解决办法]
楼主的代码写得不够科学,楼上写挺好的

热点排行