JDBC代码的实现
JDBC的開發步驟
JDBC的代碼實現
/*** JDBC的入門程序* @author jt**/ public class JDBCDemo1 {@Test/*** JDBC的入門*/public void demo1() throws Exception{// 1.加載驅動Class.forName("com.mysql.jdbc.Driver");// 2.獲得連接Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/web_test3", "root", "abc");// 3.基本操作:執行SQL// 3.1獲得執行SQL語句的對象Statement statement = conn.createStatement();// 3.2編寫SQL語句:String sql = "select * from user";// 3.3執行SQL:ResultSet rs = statement.executeQuery(sql);// 3.4遍歷結果集:while(rs.next()){System.out.print(rs.getInt("id")+" ");System.out.print(rs.getString("username")+" ");System.out.print(rs.getString("password")+" ");System.out.print(rs.getString("nickname")+" ");System.out.print(rs.getInt("age"));System.out.println();}// 4.釋放資源rs.close();statement.close();conn.close();} }?
總結
- 上一篇: MySQL数据库事务的特性
- 下一篇: JDBC的CRUD操作之Prepared