Home > php教程 > PHP开发 > body text

Eclipse连接Mysql数据库操作总结

高洛峰
Release: 2016-12-27 13:17:51
Original
1520 people have browsed it

(自己亲测,开始学习Eclipse(我的Eclipse版本是4.5.2,Jdbc驱动器的jar包版本是5.1.7,亲测可以使用)连接到数据库的时候,发现网上有好多经验,但是发现网上的出现很多错误,故此发表此博客,希望对大家有所帮助)

1:首选你需要下载Jdbc驱动器(mysql-connector-java-5.1.7-bin.jar)这个文件

下载上面那个文件也是很是坎坷啊,所以为了你们的方便,特此分享。

2:下载好上面的Jdbc驱动器后,就可以开始动手操作了,首先打开Eclipse,创建一个Project,我的工程的名字叫做demo,然后右击src,继续找到new,找到Floder,然后如图

Eclipse连接Mysql数据库操作总结

然后在工程下面的src右击接下来就是将下载好Jdbc驱动器粘贴到这个demo这个工程下面的lib下面,然后点击刚才粘贴的那个jar包,找到build path继续找到add to build path,

Eclipse连接Mysql数据库操作总结

出现的结果如上图所示,这时添加完成之后,才可以使用Eclipse连接My sql数据库

连接数据库代码如下(需要重点注意的是Connection connect=DriverManage.getConnection("jdbc:mysql://localhost:3306/test","root","密码"))

上面那一句里面的"密码"是你的数据库自己的密码;需要自己修改一下,上面"jdbc:mysql://localhost:3306/test"里面的test是自己使用mysql创建的一个表,是自己创建的,需要额外注意;(创建my sql语句将在本博客的其他地方找到,敬请关注)

package com.ningmengxueyuan;
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/test1","root","123456"); 
//连接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(); 
} 
} 
}
Copy after login

以上所述是小编给大家介绍的Eclipse连接Mysql数据库操作总结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对PHP中文网的支持!

更多Eclipse连接Mysql数据库操作总结相关文章请关注PHP中文网!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!