Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2561396
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-07-12 15:15:23


 
JDBC连接mysql需要安装先安装mysql-connector-java.
本例实现,连接mysql,从test数据库emp(name,age)表中查询信息并输出到Console
 
  1. import java.sql.*;

  2. public class TestJDBC {

  3.     public static void main(String[] args){
  4.         ResultSet rs = null;
  5.         Statement stmt = null;
  6.         Connection conn = null;
  7.         //http://dev.mysql.com/downloads/connector/j/3.0.html

  8.         //将mysql-connector-java-5.0.8-bin.jar加入classpath,放入WEB-INF/lib

  9.         //

  10.         try{
  11.             Class.forName("com.mysql.jdbc.Driver");            
  12.             String URL = "jdbc:mysql://localhost:3306/test";
  13.             String user = "root";
  14.             String password = "root";
  15.             //连接数据库

  16.             conn = DriverManager.getConnection(URL, user, password);
  17.         
  18.             stmt = conn.createStatement();    
  19.             String Sql = "Select * from emp";            
  20.             rs = stmt.executeQuery(Sql);//执行sql查询

  21.             while(rs.next()){
  22.                 System.out.print(rs.getString("name")+" -");
  23.                 System.out.print(rs.getString("age")+"\n");
  24.             }
  25.         }catch(ClassNotFoundException e){
  26.             e.printStackTrace();
  27.         }catch(SQLException e){
  28.             e.printStackTrace();
  29.         }finally{
  30.             try{
  31.                 if(rs != null){
  32.                     rs.close();
  33.                     rs = null;
  34.                 }
  35.                 if(stmt != null){
  36.                     stmt.close();
  37.                     stmt = null;
  38.                 }
  39.                 if(conn != null){
  40.                     conn.close();
  41.                     conn = null;
  42.                     }
  43.             }catch(SQLException e){
  44.                 e.printStackTrace();
  45.             }
  46.         }//end finally

  47.     }//end main

  48.     
  49.     
  50. }

  51. /*

  52. -----------------------------------------------------
  53. C:\myeclipseApp\hiworld\src>javac TestJDBC.java

  54. C:\myeclipseApp\hiworld\src>java TestJDBC
  55. Henry -11
  56. Ella -12
  57. */
  1. mysql> use test;
  2. Database changed
  3. mysql> create table emp(
  4.     -> name char(20),
  5.     -> age int);
  6. Query OK, 0 rows affected (0.10 sec)

  7. mysql> insert into emp values('Henry',11);
  8. Query OK, 1 row affected (0.06 sec)

  9. mysql> insert into emp values('Ella',12);
  10. Query OK, 1 row affected (0.05 sec)

  11. mysql> select * from emp;
  12. +-------+------+
  13. | name | age |
  14. +-------+------+
  15. | Henry | 11 |
  16. | Ella | 12 |
  17. +-------+------+
  18. 2 rows in set (0.00 sec)
  19. -----------------------------------------------------
  20. C:\myeclipseApp\hiworld\src>javac TestJDBC.java

  21. C:\myeclipseApp\hiworld\src>java TestJDBC
  22. Henry -11
  23. Ella -12
阅读(857) | 评论(1) | 转发(0) |
0

上一篇:jsp之session与request对象

下一篇:理财常识

给主人留下些什么吧!~~

tianbianfei2011-07-13 13:20:12

在工作之余,也放松一下吧,给大家推荐一款android游戏,个人觉得挺好玩:http://doumiw.com/market/software!getSW.action?swId=9661