package com.myWeb.user;
/**
* 使用MVC模式
* 是User一个类,封装User的属性
* @author user32
*
*/
public class User {
private String name;
private int sex;
private int age;
private String phone;
private String qq;
public User(){super();}
public User(String name,int sex,int age,
String phone,String qq){
this.name = name;
this.sex = sex;
this.age = age;
this.phone = phone;
this.qq = qq;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String toString(){
return "user[name="+name+";sex="+sex+
";age="+age+";phone="+phone+";qq="+
qq+"]";
}
}
刚学习JavaEE,有不对的地方请指出
----------------------------------------------------
代码上次有错误,这次修改,没有优化
----------------------------------------------------
想实现一个简单的web功能,安静数据库(mysql)里面的表的内容显示在html上。
1、准备工作
安装myeclipse(对JavaEE开发比较方便),tomcat插件,mysql数据库,Navicat(方便管理mysql数据库)
这些安装网上有很多教程,可自行百度
2、想实现这个功能,百度下很多方法如
Ajax:http://www.cnblogs.com/lansehai/p/4668917.html
mvc模式:http://www.cnblogs.com/lxnlxn/p/5813981.html
php :http://blog.csdn.net/xtqueen/article/details/8219002 这里参照mvc模式
3、在数据库中建立一个简单的表
语句:
create table userT
-> (id INT NOT NULL AUTO_INCREMENT,
-> name VARCHAR(30) NOT NULL,
-> sex int(1),
-> age int(2),
-> phone VARCHAR(11) NOT NULL,
-> qq VARCHAR(15),
-> PRIMARY KEY(id));
4、写一个简单的html文件
MyHtml.html
rel="stylesheet" type="text/css" href="./css/base.css"/>
姓名
性别
年龄
手机
QQ
操作
name
男
11
15060405110
1374465900
name
男
11
15060405110
1374465900
name
男
11
15060405110
1374465900
body{
border:0px ;
padding:0px;
margin:0px;
/*background-image: url(../web/body_bg.jpg);
background-repeat: repeat-x;*/
}
div{
border:1px solid black;
margin:0px auto;
}
#showData{
margin:0 auto;
border-collapse: collapse;
width: 800px;
text-align: center;
font-size: 16px;
}
tr.header{
height:40px;
font-weight:bold;
}
#showData td{
border:1px solid black;
height:30px;
margin:0
}
package com.myWeb.user;
/**
* 使用MVC模式
* 是User一个类,封装User的属性
* @author user32
*
*/
public class User {
private String name;
private int sex;
private int age;
private String phone;
private String qq;
public User(){super();}
public User(String name,int sex,int age,
String phone,String qq){
this.name = name;
this.sex = sex;
this.age = age;
this.phone = phone;
this.qq = qq;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String toString(){
return "user[name="+name+";sex="+sex+
";age="+age+";phone="+phone+";qq="+
qq+"]";
}
}
package com.myWeb.DAO;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.myWeb.user.User;
/**
* User的DAO类
* 实现数据库的连接;数据库的查询
* @author user32
*
*/
public class UserDAO {
private static Connection conn=null;
/**
* 连接数据库的相关信息
*/
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://127.0.0.1:3306/myweb";
private static String user = "root";
private static String password = "105092014032";
/**
* 连接数据库
*/
public static void getConnect(){
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
System.out.println("数据库连接失败");
e.printStackTrace();
}
}
/**
* 关闭数据库
*/
public static void closeConnect(){
if (conn==null){
return;
}
try{
conn.close();
}catch (SQLException e) {
System.out.println("关闭数据库失败");
e.printStackTrace();
}
}
public static List
getUsers(){
if (conn==null){return null;}
List
lists = new ArrayList
();
/*PreparedStatement会对sql语句进行检查,多语句
* 进行批处理。相对于Statement比较安全。一般在开发使用
* PreparedStatement
* PreparedStatement preparedStatement = null;
* 在改方法中只查询一次,使用Statement
*/
Statement state = null;
ResultSet rs = null;
String sql = "SELECT t_name,sex,age,phone,qq" +
"FROM userT";
sql = "select * from usert";
User user = new User();
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()){
System.out.println(rs.getString("t_name"));
user.setName(rs.getString("t_name"));
user.setSex(rs.getInt("sex"));
user.setAge(rs.getInt("age"));
user.setPhone(rs.getString("phone"));
user.setQq(rs.getString("qq"));
lists.add(user);
}
} catch (Exception e) {
lists = null;
e.printStackTrace();
}
return lists;
}
}
package com.myWeb.DAO;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.myWeb.user.User;
/**
* User的DAO类
* 实现数据库的连接;数据库的查询
* @author user32
*
*/
public class UserDAO {
private static Connection conn=null;
/**
* 连接数据库的相关信息
*/
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://127.0.0.1:3306/myweb?" +
"useUnicode=true&characterEncoding=UTF-8";
private static String user = "root";
private static String password = "105092014032";
/**
* 连接数据库
*/
public static void getConnect(){
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
System.out.println("数据库连接失败");
e.printStackTrace();
}
}
/**
* 关闭数据库
*/
public static void closeConnect(){
if (conn==null){
return;
}
try{
conn.close();
}catch (SQLException e) {
System.out.println("关闭数据库失败");
e.printStackTrace();
}
}
public static List
getUsers(){
if (conn==null){
UserDAO.getConnect();
}
List
lists = new ArrayList
();
/*PreparedStatement会对sql语句进行检查,多语句
* 进行批处理。相对于Statement比较安全。一般在开发使用
* PreparedStatement
* PreparedStatement preparedStatement = null;
* 在改方法中只查询一次,使用Statement
*/
Statement state = null;
ResultSet rs = null;
//这个t_name字段好像有点问题
// String sql = "SELECT t_name,sex,age,phone,qq" +
// "FROM userT";
String sql = "select * from usert";
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()){
/*User user = new User();
//System.out.println(rs.getString("t_name"));
user.setName(rs.getString("t_name"));
user.setSex(rs.getInt("sex"));
user.setAge(rs.getInt("age"));
user.setPhone(rs.getString("phone"));
user.setQq(rs.getString("qq"));*/
lists.add(new User(rs.getString("t_name"),
rs.getInt("sex"),
rs.getInt("age"),
rs.getString("phone"),
rs.getString("qq")));
}
try {
rs.close();
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (Exception e) {
lists = null;
e.printStackTrace();
}finally{
//UserDAO.closeConnect();
}
//System.out.println("测试lists:"+lists);
return lists;
}
}
最后截图。不过按钮不能使用,可以在servlet中接受继续添加功能,应该可以实现(也是刚学,不是很清楚)
前面是由于编码问题导致乱码,不过询问老师,终于改过来,所以最后一个正常显示
其中遇到一个mysql乱码问题:可以去http://blog.csdn.net/shawshank_bingo/article/details/52579526