Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2291249
  • 博文数量: 252
  • 博客积分: 5472
  • 博客等级: 大校
  • 技术积分: 3107
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-17 18:39
文章分类

全部博文(252)

文章存档

2012年(96)

2011年(156)

分类: Mysql/postgreSQL

2011-11-15 19:19:28

/** * 单独的java程序连接mysql数据库
* author:JavaAlpha
* date :2011-3-31 20:48:28
* IDE:EmEditor Version 9.16
*/
 
  1. import java.sql.*;

  2. public class Test {

  3. public static void main(String[] args){

  4. String driver = "com.mysql.jdbc.Driver";

  5. String url = "jdbc:mysql://localhost:3306/rftl";

  6. String user = "admin";

  7. String password = "admin";

  8. try {

  9. Class.forName(driver);

  10. Connection conn = DriverManager.getConnection(url, user, password);

  11.  if(!conn.isClosed())

  12. System.out.println("Succeeded connecting to the Database!");

  13.  Statement statement = conn.createStatement();

  14.  String sql = "select * from news order by id desc";

  15.  ResultSet rs = statement.executeQuery(sql);

  16.  String name = null;

  17. while(rs.next()) {

  18. name = rs.getString("title");

  19.  System.out.println(rs.getString("id")+"\t"+rs.getString("content") + "\t" + name + "\t" + rs.getString("title")+"\t"+rs.getString("time"));

  20.  }

  21.  rs.close();

  22. conn.close();

  23. } catch(ClassNotFoundException e) {

  24. System.out.println("Sorry,can`t find the Driver!");

  25. e.printStackTrace();

  26.  } catch(SQLException e) {

  27. e.printStackTrace();

  28. } catch(Exception e) {

  29. e.printStackTrace();

  30. }
  31. }

  32. }
阅读(1684) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

十七岁的回忆2011-11-19 20:02:36

学习了!