您当前的位置: 首页 >  sql

大前端之旅

暂无认证

  • 6浏览

    0关注

    403博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

JDBC如何连接mysql数据库附详细步骤

大前端之旅 发布时间:2019-11-09 18:51:25 ,浏览量:6

JDBC连接数据库在学习中是很重要的一个环节,今天给大家详细说明JDBC连接数据库需要的步骤 1.加载驱动 驱动包的下载地址 https://dev.mysql.com/downloads/connector/j/ 选择platform iindependent 在这里插入图片描述在这里插入图片描述到时候要将这个加载到项目中然后输入 Class.forName("com.mysql.jdbc.Driver");. 2.获取连接

private static final String URL="jdbc:mysql://localhost:3306/student
 private static final String NAME="root";
 private static final String PASSWORD="123456";
 Connection  conn=DriverManager.getConnection(URL, NAME, PASSWORD);
 

URL为路径;其中student为数据库名 NAME 为用户名 PASSWORD为密码 3.通过数据库的连接完成

 String sql = "select sno,sname from st_profiles";
 rs = stmt.executeQuery(sql);
 while (rs.next()) {
  System.out.println(rs.getString("sno"));
  System.out.println(rs.getString("sname"));
 }

4.关闭数据库

 rs.close();
 stmt.close();
 conn.close();

通过以上步骤就可以完成JAVA与数据库的搭桥技术!第一次写可能写的不太好。

关注
打赏
1660524863
查看更多评论
立即登录/注册

微信扫码登录

0.0681s