日期:2014-05-16 浏览次数:20951 次
DROP DATABASE IF EXISTS databaseWeb; --如果存在,则删除数据库databaseWeb CREATE DATABASE databaseWeb CHARACTER SET utf8; --创建数据库,使用utf8编码 USE databaseWeb; --切换到数据库databaseWeb set Names 'gbk'; --控制台使用gbk编码 DROP TABLE IF EXISTS tb_person; CREATE TABLE tb_person( id INTEGER AUTO_INCREMENT COMMENT 'id', name VARCHAR(45) COMMENT '姓名', english_name VARCHAR(45) COMMENT '英文名', age INTEGER UNSIGNED COMMENT '年龄', sex VARCHAR(45) COMMENT '性别', birthday DATE COMMENT '出生日期', description TEXT COMMENT '备注', create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP() COMMENT '创建时间', PRIMARY KEY(id) ); INSERT INTO tb_person (name,english_name,age,sex,birthday,description) values('刘京华','Helloweenvsfei','25','男','1982-08-09','无备注'); INSERT INTO tb_person (name,english_name,age,sex,birthday,description) values('科特柯本','Kurt Cobain','27','男','1967-02-20','Nirvana'); INSERT INTO tb_person (name,english_name,age,sex,birthday,description) values('李四','Faye','31','女','1969-08-08','狮子座'); INSERT INTO tb_person (name,english_name,age,sex,birthday,description) values('张三','Foo Bar','18','女','2008-08-08','');
package com.jdbc.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import com.opensymphony.xwork2.ActionSupport; public class JDBCTest extends ActionSupport{ private static final long serialVersionUID = -6572397411780171140L; public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); private List<PersonBean> list; public static Connection getConnection() throws ClassNotFoundException, SQLException{ String url = "jdbc:mysql://localhost:3306/databaseWeb"; Class.forName("com.mysql.jdbc.Driver"); String username = "root"; String password = "admin"; Connection con = DriverManager.getConnection(url, username, password); return con; } public String getPersonList() { try { Connection con = getConnection(); Statement sql = con.createStatement(); String query = "select * from tb_person"; ResultSet result = sql.executeQuery(query); this.list = new ArrayList<PersonBean>(); while(result.next()){ PersonBean bean = new PersonBean(); bean.setId(String.valueOf(result.getInt("id"))); bean.setName(result.getString("name")); bean.setEnglish_name(result.getString("english_name")); bean.setAge(String.valueOf(result.getInt("age"))); bean.setSex(result.getString("sex")); bean.setBirthday(dateFormat.format(result.getDate("birthday"))); list.add(bean); } } catch (Exception e) { e.printStackTrace(); } finally{ } return SUCCESS; } public List<PersonBean> getList() { return list; } public void setList(List<PersonBean> list) { this.list = list; } }
package com.jdbc.test; public class PersonBean { private String id; private String name; private String english_name; private String age; private String sex; private String birthday; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEnglish_name() { return english_name; } public void setEnglish_name(String englishName) { english_name = englishName; } public String getAge() { return age; } publ