Chinaunix首页 | 论坛 | 博客
  • 博客访问: 307747
  • 博文数量: 206
  • 博客积分: 1040
  • 博客等级: 少尉
  • 技术积分: 1756
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-09 17:22
文章分类

全部博文(206)

文章存档

2015年(3)

2014年(147)

2013年(2)

2012年(54)

我的朋友

分类: Java

2014-12-09 13:30:45

一、java基础知识
 public static void main(String args[]){}
  System.out.println("hello")

基本类型:


只有构造函数可以不使用void
class Person{
  private int age;
  private int name;
  Person(int n,int i){
       age=n;
      name=i;
}
}

异常:

public static void main(String[] args) {

FileInputStream fis = null;

try {

fis = new FileInputStream("c:/Exception01.java");

int i = 0;

while((i=fis.read())!=-1){

System.out.print((char)i);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e1){

e1.printStackTrace();

}

}



数组: 
数组不能指定其长度, 如int a[5]是非法的。 正确的是int[] a; a=new int[5];
元素为引用类型的数组中每一个元素都需要实例化
静态初始化:int a[]={3,9,8}
Date days[]={
 new Date(1,4,2004),
 new Date(1,5,2005)
}
用for循环遍历数组:
for(int s:arr){
  System.out.println(s);
}

String类常用方法:







容器:

Set中的数据对象没有顺序且不可以重复;List中的有顺序且可以重复
Map定义了存储"键(key)--值(value)映射对"的方法








jdbc例子

点击(此处)折叠或打开

  1. (如QQ博客页面,有查询记录、增加评论等操作)
  2. 建基本类,定义各个基本值:id,title,comment,时间,增加人
  3. 建数据库连接基本类
  4. 建处理类程序

  5. 建基本类
  6. (定义各个基本元素值,并自动调用GET SET语句)
  7. package com.cic.Jdbc_Test.vo;

  8. import java.sql.Date;

  9. public class Article {

  10. private int id;
  11. private String title;
  12. private String content;
  13. private Date deployDate;
  14. private String deployer;

  15. public int getId() {
  16.         return id;
  17.     }
  18.     public void setId(int id) {
  19.         this.id = id;
  20.     }
  21.     public String getTitle() {
  22.         return title;
  23.     }
  24.     public void setTitle(String title) {
  25.         this.title = title;
  26.     }
  27.     public String getContent() {
  28.         return content;
  29.     }
  30.     public void setContent(String content) {
  31.         this.content = content;
  32.     }
  33.     public Date getDeployDate() {
  34.         return deployDate;
  35.     }
  36.     public void setDeployDate(Date deployDate) {
  37.         this.deployDate = deployDate;
  38.     }
  39.     public String getDeployer() {
  40.         return deployer;
  41.     }
  42.     public void setDeployer(String deployer) {
  43.         this.deployer = deployer;
  44.     }
  45.     
  46. 建数据库连接基本类
  47. 在mysql中建好数据库后,写数据库连接类(和示例1-4中的共用类是一样的)
  48.   package com.cic.Jdbc_Test.common;
  49.   
  50.   import java.sql.Connection;
  51.   import java.sql.DriverManager;
  52.   import java.sql.ResultSet;
  53.   import java.sql.SQLException;
  54.   import java.sql.Statement;
  55.   
  56.   
  57.   public class JdbcUtil {    
  58.       static {
  59.           try {
  60.               Class.forName("com.mysql.jdbc.Driver");
  61.           } catch (ClassNotFoundException e) {
  62.               e.printStackTrace();
  63.           }
  64.       }
  65.       public static Connection getConnection() {
  66.           Connection conn = null;
  67.           try {
  68.               conn = DriverManager.getConnection(
  69.                       "jdbc:mysql://localhost:3306/mydb", "root", "111");
  70.           } catch (SQLException e) {
  71.               e.printStackTrace();
  72.           }
  73.           return conn;
  74.       }
  75.       public static void closed(ResultSet rs, Statement stmt, Connection conn) {
  76.           try {
  77.               if (rs != null) {
  78.                   rs.close();
  79.               }
  80.           } catch (SQLException e) {
  81.               e.printStackTrace();
  82.           } finally {
  83.               try {
  84.                   if (stmt != null)
  85.                       stmt.close();
  86.               } catch (SQLException e) {
  87.                   e.printStackTrace();
  88.               } finally {
  89.                   try {
  90.                       if (conn != null)
  91.                           conn.close();
  92.                   } catch (SQLException e) {
  93.                       e.printStackTrace();
  94.                   }
  95.               }
  96.           }
  97.       }
  98.       
  99.       public static void main(String[] args) {
  100.           JdbcUtil.getConnection();
  101.   
  102.       }
  103.   
  104. }
  105. 建处理类程序
  106. package com.cic.Jdbc_Test.jdbcService;

  107. import java.sql.Connection;
  108. import java.sql.Date;
  109. import java.sql.PreparedStatement;
  110. import java.sql.ResultSet;
  111. import java.sql.SQLException;
  112. import java.util.ArrayList;
  113. import java.util.List;

  114. import com.cic.Jdbc_Test.common.JdbcUtil;
  115. import com.cic.Jdbc_Test.vo.Article;

  116. public class ArticlePer {

  117.     public void delete(int id) {
  118.     }

  119.     public void modify() {
  120.     }

  121.     public void insert(Article article) {
  122. //注:参数为article类型,参数可以为一个对象
  123.         Connection conn = null;
  124.         PreparedStatement pstmt = null;
  125.         String sql = "INSERT INTO ARTICLE VALUES(NULL,?,?,?,?)";
  126.         try {
  127.             conn = JdbcUtil.getConnection();
  128.             pstmt = conn.prepareStatement(sql);
  129.             pstmt.setString(1, article.getTitle());
  130.             pstmt.setString(2, article.getContent());
  131.             pstmt.setDate(3, new Date(new java.util.Date().getTime()));
  132.             pstmt.setString(4, article.getDeployer());
  133. //此处用到了article的get方法
  134.             pstmt.executeUpdate();
  135.         } catch (SQLException e) {
  136.             e.printStackTrace();
  137.         } finally {
  138.             JdbcUtil.closed(null, pstmt, conn);
  139.         }
  140.     }
  141.     public List<Article> queryAll() {
  142. //注:返回为List类型
  143.         Connection conn = null;
  144.         PreparedStatement pstmt = null;
  145.         ResultSet rs = null;
  146.         List<Article> articles = new ArrayList<Article>()
  147. //需要一个东西来存储查询结果,JAVA有变量、数值、容器;因为查询结果经常变化,所以用容器
  148.         String sql = "select * from article";
  149.         try {
  150.             conn = JdbcUtil.getConnection();
  151.             pstmt = conn.prepareStatement(sql);
  152.             rs = pstmt.executeQuery();
  153.             while (rs.next()) {
  154.                 Article article = new Article();
  155.                 article.setId(rs.getInt("id"));
  156. //将get方法得到的查询结果用set赋给article对象
  157.                 article.setTitle(rs.getString("title"));
  158.                 article.setContent(rs.getString("content"));
  159.                 article.setDeployDate(rs.getDate("deploydate"));
  160.                 article.setDeployer(rs.getString("deployer"));
  161.                 articles.add(article);
  162. //增加article对象,因为容器里只能保存对象。同时传对象、返回对象是可以的。
  163.             }
  164.         } catch (SQLException e) {
  165.             e.printStackTrace();
  166.         } finally {
  167.             JdbcUtil.closed(rs, pstmt, conn);
  168.         }
  169.         return articles;
  170.     }

  171.     public void queryById() {
  172.     }

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

  174.         ArticlePer ap = new ArticlePer();

  175.         /*
  176.          * String title = "泥石流"; String content = "中国人民是打不垮的!!!!"; String
  177.          * deployer = "卢冲"; Article article = new Article();
  178.          * article.setTitle(title); article.setContent(content);
  179.          * article.setDeployer(deployer); ap.insert(article);
  180. //此处用到了article的get方法,注ap.insert(article)传递对象的方法
  181.          */

  182.         List<Article> articles = ap.queryAll();
  183. //将查询结果显示出来,用for()或遍历来显示查询结果
  184.         for (Article art : articles) {
  185.             System.out.println(art.getTitle() + ":" + art.getContent());
  186.         }
  187.     }

  188. }


阅读(324) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~