每次在面試時(shí)被問(wèn)到j(luò)dbc的數(shù)據(jù)路鏈接過(guò)程都卡著,這次不怕了,背會(huì)了。。。
第一個(gè) ,比較粗糙的 try{ ? Class.forName("com.mysql.jdbc.Driver"); } catch(ClassNotFoundException e) {} //定義所要用到的三個(gè)數(shù)據(jù)庫(kù)應(yīng)用對(duì)象 Connection con=null; //連接對(duì)象 Statement sql=null; //Statement對(duì)象(SQL語(yǔ)句) ResultSet rs=null; //結(jié)果集對(duì)象 //進(jìn)行數(shù)據(jù)源的連接 try{ ? con=DriverManager.getConnection ("jdbc:mysql://localhost/scutcs","","");//連接數(shù)據(jù)庫(kù)的url 用戶名和密碼 sql=con.createStatement(); String to="Select * From user1 Where username='"+username+"'"; rs=sql.executeQuery(to); //根據(jù)所定義的Statement執(zhí)行生成相應(yīng)的結(jié)果集并存在RS中 if(rs.next()) //判斷結(jié)果集是否為空,如果不為空則表示有記錄 { out.print("<script>alert('用戶名 "+xm+"已存在,請(qǐng)另選一個(gè)!');history.back();</script>");//如果存在返回注冊(cè)頁(yè)面 } else {如果不存在就向數(shù)據(jù)庫(kù)添加一條記錄} } catch (SQLException e) ? { out.print(e); ? } 第二個(gè) ,作為公共類(lèi)比較完整些的? 把部分的定義為單個(gè)函數(shù)方便調(diào)用 import java.sql.*; public class DB { public static Connection getConn() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost/shopping?user=root&password=123456"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } public static PreparedStatement prepare(Connection conn, String sql) { PreparedStatement pstmt = null; ? try { if(conn != null) { pstmt = conn.prepareStatement(sql); } } catch (SQLException e) { e.printStackTrace(); } return pstmt; } public static PreparedStatement prepare(Connection conn, String sql, int autoGenereatedKeys) { PreparedStatement pstmt = null; ? try { if(conn != null) { pstmt = conn.prepareStatement(sql, autoGenereatedKeys); } } catch (SQLException e) { e.printStackTrace(); } return pstmt; } public static Statement getStatement(Connection conn) { Statement stmt = null; ? try { if(conn != null) { stmt = conn.createStatement(); } } catch (SQLException e) { e.printStackTrace(); } return stmt; } /* public static ResultSet getResultSet(Connection conn, String sql) { Statement stmt = getStatement(conn); ResultSet rs = getResultSet(stmt, sql); close(stmt); return rs; } */ public static ResultSet getResultSet(Statement stmt, String sql) { ResultSet rs = null; try { if(stmt != null) { rs = stmt.executeQuery(sql); } } catch (SQLException e) { e.printStackTrace(); } return rs; } public static void executeUpdate(Statement stmt, String sql) { try { if(stmt != null) { stmt.executeUpdate(sql); } } catch (SQLException e) { e.printStackTrace(); } } public static void close(Connection conn) { try { if(conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } public static void close(Statement stmt) { try { if(stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e) { e.printStackTrace(); } } ? |
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
