java 如何查询数据库_java怎么查询数据库

java怎么查询数据库,数据,数据库,条件,数据表,方法

java怎么查询数据库

易采站长站,站长之家为您整理了java怎么查询数据库的相关内容。

java查询数据库的方法:首先创建user和teacher数据库;然后将teacher表的【user_id】列与user表的id列建立一对多连接;接着向user数据表中添加数据,并按照条件查询user数据库数据;最后根据主表查询从表数据。

f5b36dc667a8b17449f2874c634e4cfb.png

【相关学习推荐:java基础教程】

java查询数据库的方法:

一、创建数据库

创建 user 数据库

1ae61b81f045d0bea5debd947cafe60a.png

创建 teacher 数据库

deb0b2447c1e0837763293b08b4d452c.png

teacher表的user_id列与user表的id列建立一对多连接,user_id作为外键。

43c8fa52ad5db4afeca5b2703307bf49.png

二、Java编程查询数据库

向user数据表中添加数据/** * 添加数据 */ @Test public void addData() { Connection connection = null; PreparedStatement pstmt =null; try { connection = JDBCUtils_V3.getConnection(); String sql = "insert into user values(null,?,?)"; pstmt = connection.prepareStatement(sql); pstmt.setString(1, "wangxuan"); pstmt.setString(2, "741852"); int row = pstmt.executeUpdate(); if (row>0) { System.out.println("数据添加成功!"); }else { System.out.println("数据添加失败!"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JDBCUtils_V3.release(connection, pstmt, null); } }

按照条件查询user数据库数据/** * 按照条件查询数据 */ @Test public void selectTest() { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs =null; try { conn = JDBCUtils_V3.getConnection(); String sql = "select * from user where password = ?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, "123456"); rs = pstmt.executeQuery(); while (rs.next()) { System.out.println(rs.getString(1)+"----"+rs.getString(2)+"---"+rs.getString(3)); }// System.out.println(rs); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JDBCUtils_V3.release(conn, pstmt, rs); } }

一对多查询/根据主表user查询从表teacher数据/** * 一对多查询 * 根据主表查询从表 */ @Test public void selectOnetoMore() { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = JDBCUtils_V3.getConnection();// String sql = "select * from teacher where user_id = (select id from user where username =?) "; String sql = "select * from user,teacher where user.id = teacher.user_id "; pstmt = conn.prepareStatement(sql);// pstmt.setString(1, "wangxuan"); rs = pstmt.executeQuery(); while (rs.next()) {// System.out.println(rs.getString(1)+"----"+rs.getString(2)+"---"+rs.getString(3)+"---"+rs.getString(4)); System.out.println(rs.getString(1)+"----"+rs.getString(2)+"---"+rs.getString(3)+"---"+rs.getString(4)+"----"+rs.getString(5)+"----"+rs.getString(6)+"----"+rs.getString(7)); } System.out.println("查询完成"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JDBCUtils_V3.release(conn, pstmt, rs); } }

一对多查询/根据从表查询主表/** * 一对多查询 * 根据从表查询主表数据 */ @Test public void selectMoretoOne() { Connection connection = null; PreparedStatement pstmtPreparedStatement = null; ResultSet rSet =null; try { connection = JDBCUtils_V3.getConnection(); String sql = "select * from user where id = (select user_id from teacher where teacher=?)"; pstmtPreparedStatement = connection.prepareStatement(sql); pstmtPreparedStatement.setString(1, "钱田"); rSet = pstmtPreparedStatement.executeQuery(); while (rSet.next()) { System.out.println(rSet.getString(1)+"----"+rSet.getString(2)+"---"+rSet.getString(3)); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JDBCUtils_V3.release(connection, pstmtPreparedStatement, rSet); } }}

相关推荐:编程视频课程以上就是关于对java怎么查询数据库的详细介绍。欢迎大家对java怎么查询数据库内容提出宝贵意见

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值