2016-04-22jdbc-mysql-java
2016-04-22 22:58
471 查看
//需求:遍历学生表的姓名 @Test public void test() throws SQLException, ClassNotFoundException{ //注册驱动 //DriverManager.registerDriver(new Driver()); String url = "jdbc:mysql://localhost:3306/mytest"; String user = "root"; String password = "123456"; Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); //获得连接 conn = DriverManager.getConnection(url, user, password); //发送sql语句的对象 String sql = "select * from student;"; stmt = conn.prepareStatement(sql); //结果集 rs = stmt.executeQuery(); while(rs.next()){ String name = rs.getString("name"); System.out.println(name); } } catch (Exception e) { }finally{ try { if(rs != null){ rs.close(); } rs = null; } catch (Exception e2) { } try { if( stmt != null){ stmt.close(); } stmt = null; } catch (Exception e2) { } try { if( conn != null){ conn.close(); } conn = null; } catch (Exception e2) { } } }
相关文章推荐
- java日志之log4j简单使用
- java线程安全
- Jmap 和MAT结合分析JAVA 内存问题
- Java基础5——设计模式
- java日志之slf4j与logback简单使用
- java中equals和==区别小结
- 趣味素数-金蝉素数-java
- JavaMelody应用监控使用指南
- 个人笔记 java第二季
- JavaMelody应用监控使用指南
- hdu-5670 Machine(水题附上java代码)
- java运算优先级
- spring框架学习笔记(五)
- Markers 提示 Maven Java EE Configuration Problem错误
- java语言基础知识回顾
- 为什么函数式编程在Java中很危险?
- 为什么函数式编程在Java中很危险?
- JAVA--浅谈集合
- 利用 spring bean 的属性 init-method 解决因为数据库连接没有初始化而导致首次点击页面超慢的问题
- java使double保留两位小数的方法