eclipse和mysql怎么连接

eclipse和mysql怎么连接,第1张

1.在新建的Project中右键新建Floder

2.创建名为lib的包

3.接下来解压你下载的mysql的jar包,拷贝其中的.jar文件

4.在工程lib包下邮件 选择paste即粘贴,把mysql的jar包拷贝进来

5.在mysql的jar包上右键选择 build path - add to build path

6.添加完毕之后,工程才与Mysql的jar包关联起来,现在可以使用相关类和方法了

7.在工程中新建JdbcTest1.java类

8.连接MySQL数据库驱动或使用连接池

9.在java类中准备连接数据库的基本信息

mysql和eclipse连接参考下面代码

import java.sql.*

publicclass MysqlJdbc {

publicstaticvoid 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","198876")

//连接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()

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/6155994.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-16
下一篇2023-03-16

发表评论

登录后才能评论

评论列表(0条)

    保存