J2SE基础夯实系列之JDBC,链接数据库实例代码
2012-08-14 15:46
585 查看
标准示例
importjava.sql.*; publicclass TestMySQLConn { /** * @param args */ publicstatic void main(String[] args) { Connectionconn = null; Statementstmt = null; ResultSetrs = null; try{ Class.forName("com.mysql.jdbc.Driver"); conn= DriverManager.getConnection("jdbc:mysql://localhost/mydata?" +"user=root&password=root"); stmt= conn.createStatement(); rs= stmt.executeQuery("select * from dept"); while(rs.next()) { System.out.println(rs.getString("deptno")); } }catch (SQLException ex) { System.out.println("SQLException:" + ex.getMessage()); System.out.println("SQLState:" + ex.getSQLState()); System.out.println("VendorError:" + ex.getErrorCode()); }catch (Exception ex) { ex.printStackTrace(); }finally { try{ if(rs != null) { rs.close(); rs= null; } if(stmt != null) { stmt.close(); stmt= null; } if(conn != null) { conn.close(); conn= null; } }catch (SQLException e) { e.printStackTrace(); } } } }
相关文章推荐
- J2SE基础夯实系列之JDBC,链接数据库实例代码
- J2SE基础夯实系列之File的相关方法代码实例
- J2SE基础夯实系列之使用StackTraceElement封装,打印调试代码!
- J2SE基础夯实系列之内存堆栈
- J2SE基础夯实系列之静态导入,foreach循环,可变参数
- java JDBC链接数据库代码以及步骤
- J2SE基础夯实系列之hashcode和equals方法详细解析, hashmap对于hashcode方法的使用
- J2SE基础夯实系列之容器(待更新)
- 完整java开发中JDBC链接数据库代码和步骤
- Java代码之JDBC实现数据库之间定时的表格传输(由一个库读取到另一个库)实例
- J2SE基础夯实系列之内存堆栈
- J2SE基础夯实系列之抽象类和接口的区别(太透彻了!)
- J2SE基础夯实系列之正则表达式Java
- J2SE基础夯实系列之String字符串不可变的理解,不可变类,final关键字到底修饰了什么
- jdbc链接到数据库的示例代码
- 1 开发一个注重性能的JDBC应用程序不是一件容易的事. 当你的代码运行很慢的时候JDBC驱动程序并不会抛出异常告诉你。 本系列的性能提示将为改善JDBC应用程序的性能介绍一些基本的指导原则,这其中的原则已经被许多现有的JDBC应用程序编译运行并验证过。 这些指导原则包括: 正确的使用数据库MetaData方法 只获取需要的数据 选用最佳性能的功能 管理连
- J2SE基础夯实系列之使用Arrays.sort()方法,以及涉及到的Comparable和Comparator
- java 中JDBC连接数据库代码和步骤详解及实例代码
- J2SE基础夯实系列之抽象类和接口的区别(太透彻了!)
- J2SE基础夯实系列之正则表达式Java