一、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例子
-
(如QQ博客页面,有查询记录、增加评论等操作)
-
建基本类,定义各个基本值:id,title,comment,时间,增加人
-
建数据库连接基本类
-
建处理类程序
-
-
建基本类
-
(定义各个基本元素值,并自动调用GET SET语句)
-
package com.cic.Jdbc_Test.vo;
-
-
import java.sql.Date;
-
-
public class Article {
-
-
private int id;
-
private String title;
-
private String content;
-
private Date deployDate;
-
private String deployer;
-
-
public int getId() {
-
return id;
-
}
-
public void setId(int id) {
-
this.id = id;
-
}
-
public String getTitle() {
-
return title;
-
}
-
public void setTitle(String title) {
-
this.title = title;
-
}
-
public String getContent() {
-
return content;
-
}
-
public void setContent(String content) {
-
this.content = content;
-
}
-
public Date getDeployDate() {
-
return deployDate;
-
}
-
public void setDeployDate(Date deployDate) {
-
this.deployDate = deployDate;
-
}
-
public String getDeployer() {
-
return deployer;
-
}
-
public void setDeployer(String deployer) {
-
this.deployer = deployer;
-
}
-
-
建数据库连接基本类
-
在mysql中建好数据库后,写数据库连接类(和示例1-4中的共用类是一样的)
-
package com.cic.Jdbc_Test.common;
-
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
import java.sql.Statement;
-
-
-
public class JdbcUtil {
-
static {
-
try {
-
Class.forName("com.mysql.jdbc.Driver");
-
} catch (ClassNotFoundException e) {
-
e.printStackTrace();
-
}
-
}
-
public static Connection getConnection() {
-
Connection conn = null;
-
try {
-
conn = DriverManager.getConnection(
-
"jdbc:mysql://localhost:3306/mydb", "root", "111");
-
} catch (SQLException e) {
-
e.printStackTrace();
-
}
-
return conn;
-
}
-
public static void closed(ResultSet rs, Statement stmt, Connection conn) {
-
try {
-
if (rs != null) {
-
rs.close();
-
}
-
} catch (SQLException e) {
-
e.printStackTrace();
-
} finally {
-
try {
-
if (stmt != null)
-
stmt.close();
-
} catch (SQLException e) {
-
e.printStackTrace();
-
} finally {
-
try {
-
if (conn != null)
-
conn.close();
-
} catch (SQLException e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
}
-
-
public static void main(String[] args) {
-
JdbcUtil.getConnection();
-
-
}
-
-
}
-
建处理类程序
-
package com.cic.Jdbc_Test.jdbcService;
-
-
import java.sql.Connection;
-
import java.sql.Date;
-
import java.sql.PreparedStatement;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
import java.util.ArrayList;
-
import java.util.List;
-
-
import com.cic.Jdbc_Test.common.JdbcUtil;
-
import com.cic.Jdbc_Test.vo.Article;
-
-
public class ArticlePer {
-
-
public void delete(int id) {
-
}
-
-
public void modify() {
-
}
-
-
public void insert(Article article) {
-
//注:参数为article类型,参数可以为一个对象
-
Connection conn = null;
-
PreparedStatement pstmt = null;
-
String sql = "INSERT INTO ARTICLE VALUES(NULL,?,?,?,?)";
-
try {
-
conn = JdbcUtil.getConnection();
-
pstmt = conn.prepareStatement(sql);
-
pstmt.setString(1, article.getTitle());
-
pstmt.setString(2, article.getContent());
-
pstmt.setDate(3, new Date(new java.util.Date().getTime()));
-
pstmt.setString(4, article.getDeployer());
-
//此处用到了article的get方法
-
pstmt.executeUpdate();
-
} catch (SQLException e) {
-
e.printStackTrace();
-
} finally {
-
JdbcUtil.closed(null, pstmt, conn);
-
}
-
}
-
public List<Article> queryAll() {
-
//注:返回为List类型
-
Connection conn = null;
-
PreparedStatement pstmt = null;
-
ResultSet rs = null;
-
List<Article> articles = new ArrayList<Article>();
-
//需要一个东西来存储查询结果,JAVA有变量、数值、容器;因为查询结果经常变化,所以用容器
-
String sql = "select * from article";
-
try {
-
conn = JdbcUtil.getConnection();
-
pstmt = conn.prepareStatement(sql);
-
rs = pstmt.executeQuery();
-
while (rs.next()) {
-
Article article = new Article();
-
article.setId(rs.getInt("id"));
-
//将get方法得到的查询结果用set赋给article对象
-
article.setTitle(rs.getString("title"));
-
article.setContent(rs.getString("content"));
-
article.setDeployDate(rs.getDate("deploydate"));
-
article.setDeployer(rs.getString("deployer"));
-
articles.add(article);
-
//增加article对象,因为容器里只能保存对象。同时传对象、返回对象是可以的。
-
}
-
} catch (SQLException e) {
-
e.printStackTrace();
-
} finally {
-
JdbcUtil.closed(rs, pstmt, conn);
-
}
-
return articles;
-
}
-
-
public void queryById() {
-
}
-
-
public static void main(String[] args) {
-
-
ArticlePer ap = new ArticlePer();
-
-
/*
-
* String title = "泥石流"; String content = "中国人民是打不垮的!!!!"; String
-
* deployer = "卢冲"; Article article = new Article();
-
* article.setTitle(title); article.setContent(content);
-
* article.setDeployer(deployer); ap.insert(article);
-
//此处用到了article的get方法,注ap.insert(article)传递对象的方法
-
*/
-
-
List<Article> articles = ap.queryAll();
-
//将查询结果显示出来,用for()或遍历来显示查询结果
-
for (Article art : articles) {
-
System.out.println(art.getTitle() + ":" + art.getContent());
-
}
-
}
-
-
}
阅读(504) | 评论(0) | 转发(0) |