eclipse 连接mysql问题 求助

人生万事须自为,跬步江山即寥廓。
全栈领域优质创作者
2014-04-03 07:04:35
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 connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","");
//连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码

System.out.println("Success connect Mysql server!");
Statement stmt = connect.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();
}
}
}

上面是代码 下面是eclipse控制台输出


<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">
If connecting using TCP/IP, should the driver set SO_KEEPALIVE?
</Property>
<Property name="tcpNoDelay" required="No" default="true" sortOrder="alpha" since="5.0.7">
If connecting using TCP/IP, should the driver set SO_TCP_NODELAY (disabling the Nagle Algorithm)?
</Property>
<Property name="tcpRcvBuf" required="No" default="0" sortOrder="alpha" since="5.0.7">
If connecting using TCP/IP, should the driver set SO_RCV_BUF to the given value? The default value of '0', means use the platform default value for this property)
</Property>
<Property name="tcpSndBuf" required="No" default="0" sortOrder="alpha" since="5.0.7">
If conn

...全文
184 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
问题解决了 谢谢各位 下面是解决的流程 我重新建工程,重新导入JDBC驱动 重新作为简单程序而不是工程 运行了一下 问题解决 问题在 JDBC驱动上面
  • 打赏
  • 举报
回复
eclipse里面,problems里面显示三个错误, 好像是不能读取我的JDBC驱动程 我用的是mysql-connector-jar 这里有问题吗
  • 打赏
  • 举报
回复
清除,重新编译
  • 打赏
  • 举报
回复
java.lang.NoClassDefFoundError: SimpleJdbc
Caused by: java.lang.ClassNotFoundException: SimpleJdbc
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
  • 打赏
  • 举报
回复
上次是直接点工具条 的运行按钮 的输出
  • 打赏
  • 举报
回复
我重新运行了一下 在eclipse里面点击,run as java application 输出 could not find the main class: SimpleJdbc ,Program will exit.
tony4geek 2014-04-03
  • 打赏
  • 举报
回复
好乱。异常贴出来。。
  • 打赏
  • 举报
回复
代码换成如下 import java.sql.*; public class SimpleJdbc { /** * @param args */ public static void main(String[] args) throws SQLException, ClassNotFoundException { // TODO Auto-generated method stub Class.forName("com.mysql.jdbc.Driver"); System.out.println("Driver loaded"); Connection con = DriverManager.getConnection ("jdbc:mysql://localhost/test","root",""); System.out.println("Database connected"); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("select * from user"); while(rs.next())System.out.println(rs.getString(1)+rs.getString(2)); con.close(); } } 输出相同, 到底是什么问题
聪头 2014-04-03
  • 打赏
  • 举报
回复
没看到问题
java_caoxing 2014-04-03
  • 打赏
  • 举报
回复
eclipse控制台输出那部分是不是代码复制错了? 怎么感觉你下面贴出来的部分是某个配置文件里面的东西。

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧