这周写了我的第一个Dao。第一次和我的数据库链接。下面是我的成果!
package 新课程添加; import java.sql.*; import java.util.*; import 新课程添加.stunandpaw; public class Dao { public ArrayList getAllstunumbers() throws Exception{ Connection conn = null; ArrayList<stunandpaw> stunumbers = new ArrayList<stunandpaw>(); try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/class?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT"; String user = "root"; String password = "qwer1234"; conn = DriverManager.getConnection(url,user,password); String sql = "select stunumber,password from new_table"; Statement stat = conn.createStatement(); ResultSet re = stat.executeQuery(sql); while(re.next()) { stunandpaw stp = new stunandpaw(); stp.setPassword(re.getString("stunumber")); stp.setStunum(re.getString("password")); stunumbers.add(stp); } re.close(); stat.close(); }catch(SQLException e) { e.printStackTrace(); }finally { try { if(conn!=null) { conn.close(); conn = null; } }catch(Exception ex) { } } return stunumbers; } }
显然这个数据库的名字是class,我写的这个小web就是“新课程添加”实现的添加新的课程信息,还有查看课程信息。