java查詢資料庫的方法:先建立user和teacher資料庫;然後將teacher表的【user_id】列與user表的id列建立一對多連接;接著在user資料表中加入數據,並依照條件查詢user資料庫資料;最後根據主表查詢從表格資料。
【相關學習推薦:java基礎教學】
java查詢資料庫的方法:
一、建立資料庫
建立user 資料庫
#建立teacher 資料庫
teacher表的user_id
列與user表的id列建立一對多連接,user_id作為外鍵。
二、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怎麼查詢資料庫的詳細內容。更多資訊請關注PHP中文網其他相關文章!