您好,登錄后才能下訂單哦!
小編給大家分享一下java查詢數(shù)據(jù)庫的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
java查詢數(shù)據(jù)庫的方法:首先創(chuàng)建user和teacher數(shù)據(jù)庫;然后將teacher表的【user_id】列與user表的id列建立一對(duì)多連接;接著向user數(shù)據(jù)表中添加數(shù)據(jù),并按照條件查詢user數(shù)據(jù)庫數(shù)據(jù);最后根據(jù)主表查詢從表數(shù)據(jù)。
java查詢數(shù)據(jù)庫的方法:
一、創(chuàng)建數(shù)據(jù)庫
創(chuàng)建 user 數(shù)據(jù)庫
創(chuàng)建 teacher 數(shù)據(jù)庫
teacher表的user_id
列與user表的id列建立一對(duì)多連接,user_id作為外鍵。
二、Java編程查詢數(shù)據(jù)庫
向user數(shù)據(jù)表中添加數(shù)據(jù)
/** * 添加數(shù)據(jù) */ @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("數(shù)據(jù)添加成功!"); }else { System.out.println("數(shù)據(jù)添加失?。?quot;); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ JDBCUtils_V3.release(connection, pstmt, null); } }
按照條件查詢user數(shù)據(jù)庫數(shù)據(jù)
/** * 按照條件查詢數(shù)據(jù) */ @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); } }
一對(duì)多查詢/根據(jù)主表user查詢從表teacher數(shù)據(jù)
/** * 一對(duì)多查詢 * 根據(jù)主表查詢從表 */ @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); } }
一對(duì)多查詢/根據(jù)從表查詢主表
/** * 一對(duì)多查詢 * 根據(jù)從表查詢主表數(shù)據(jù) */ @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查詢數(shù)據(jù)庫的方法的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。