一、Java web开发
一下的划分并非按技术进行分类,只是个人的一种认识
1.动态网页技术:静态网页技术HTML的基本知识,servlet、JavaBean、jsp等Java语言层次上的知识以及美化网页的css和增加动态功能的javascript必备的知识
2.开发工具:数据库(包括JDBC技术)、web服务器(如tomcat)、集成的开发环境(myeclipse或eclipse)
二、本次课堂测试源代码:一个连接了SQL Server数据库的登录界面的实现
由于题目是提前公布的,于是我课下多做了些工作,这个题目我用两种方式实现的:
1.Java swing中自带的框架和组件来构建界面,由于这个方面我比较熟悉,所以我就多写了一个注册界面和登陆成功后的用户个人界面,不过时间有限,写的比较随意简陋:
数据结构:Student类
1 public class Student { 2 private String name; //姓名 3 private String pass; //密码 4 private String id; //学号 5 private String sex; //性别 6 private double score; //分数 7 private int age; 8 public Student() { 9 10 } 11 public String getName() { 12 return name; 13 } 14 public void setName(String name) { 15 this.name = name; 16 } 17 public String getId() { 18 return id; 19 } 20 public void setId(String id) { 21 this.id = id; 22 } 23 public String getSex() { 24 return sex; 25 } 26 public void setSex(String sex) { 27 this.sex = sex; 28 } 29 public double getScore() { 30 return score; 31 } 32 public void setScore(double score) { 33 this.score = score; 34 } 35 public int getAge() { 36 return age; 37 } 38 public void setAge(int age) { 39 this.age = age; 40 } 41 public String getPass() { 42 return pass; 43 } 44 public void setPass(String pass) { 45 this.pass = pass; 46 } 47 }
工具类:全部为静态成员,包括连接数据库、添加、查找数据库中的成员
1 package User; 2 3 import java.sql.*; 4 5 public class ConUtil { 6 public static void InitSQL() { 7 try { 8 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); 9 10 }catch(ClassNotFoundException | InstantiationException | IllegalAccessException e) { 11 System.out.println(e.getMessage()); 12 } 13 String user = "sa"; //默认就是这个 14 String password = "******"; //这里输入你密码即可 15 String url = "jdbc:sqlserver://localhost:1433;DasebaseName=master"; 16 //我使用的表是系统数据库master中我自己新建的表Student_Manage 17 Connection connection = null; 18 try { 19 connection = DriverManager.getConnection(url,user,password); 20 }catch(SQLException e) { 21 System.out.println(e.getMessage()); 22 } 23 ResultSet rs = null; 24 Statement stmt = null; 25 try { 26 stmt = connection.createStatement(); 27 rs = connection.getMetaData().getTables(null,null,"Student_Message",null); 28 if(!rs.next()) { //如果数据表不存在则新建 29 String query = "create table Student_Message(id varchar(50),pass varchar(50),name nvarchar(50),sex nvarchar(50),score float,age int)"; 30 stmt.executeQuery(query); 31 } 32 rs.close(); 33 stmt.close(); 34 connection.close(); 35 } catch (SQLException e) { 36 // TODO 自动生成的 catch 块 37 e.printStackTrace(); 38 System.out.println(e.getMessage()); 39 } 40 } 41 public static Connection getConnection() { 42 try { 43 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); 44 45 }catch(ClassNotFoundException | InstantiationException | IllegalAccessException e) { 46 System.out.println(e.getMessage()); 47 } 48 String user = "sa"; 49 String password = "leomessi10"; 50 String url = "jdbc:sqlserver://localhost:1433;DasebaseName=master"; 51 Connection connection = null; 52 try { 53 connection = DriverManager.getConnection(url,user,password); 54 }catch(SQLException e) { 55 System.out.println(e.getMessage()); 56 } 57 return connection; 58 } 59 60 61 public static int add(Student stu) { //注册时使用 62 Connection con = getConnection(); 63 String sql = "select * from Student_Message"; 64 PreparedStatement stmt = null; 65 ResultSet rs = null; 66 int flag = 1; 67 try { //先检查学号信息是否重复 68 stmt = con.prepareStatement(sql); 69 rs = stmt.executeQuery(); 70 71 while(rs.next()) { 72 if(rs.getString("id").equals(stu.getId())) { 73 flag = 0; 74 break; 75 } 76 } 77 if(flag==1) { 78 stmt = con.prepareStatement("insert into Student_Message values(?, ?, ?, ?, ?, ?)"); 79 stmt.setString(1,stu.getId()); 80 stmt.setString(2,stu.getPass()); 81 stmt.setString(3,stu.getName()); 82 stmt.setString(4,stu.getSex()); 83 stmt.setDouble(5,stu.getScore()); 84 stmt.setInt(6,stu.getAge()); 85 stmt.executeUpdate(); 86 } 87 rs.close(); 88 stmt.close(); 89 con.close(); 90 } catch (SQLException e) { 91 // TODO 自动生成的 catch 块 92 e.printStackTrace(); 93 System.out.println(e.getMessage()); 94 } 95 if(flag==1)return 1; 96 else return 0; 97 } 98 99 public static Student search(String id,String pass){ 100 //登陆时使用,查找成功则返回对应的Student类对象 101 Connection con = getConnection(); 102 String sql = "select * from Student_Message"; 103 PreparedStatement stmt = null; 104 ResultSet rs = null; 105 Student stu = null; 106 try { 107 stmt = con.prepareStatement(sql); 108 rs = stmt.executeQuery(); 109 110 while(rs.next()) { 111 if(rs.getString("id").equals(id)&&rs.getString("pass").equals(pass)) { 112 stu = new Student(); 113 stu.setId(id); 114 stu.setAge(rs.getInt("age")); 115 stu.setName(rs.getString("name")); 116 stu.setPass(pass); 117 stu.setScore(rs.getDouble("score")); 118 stu.setSex(rs.getString("sex")); 119 break; 120 } 121 } 122 rs.close(); 123 stmt.close(); 124 con.close(); 125 } catch (SQLException e) { 126 // TODO 自动生成的 catch 块 127 System.out.println(e.getMessage()); 128 } 129 return stu; 130 } 131 public static void update(String id,String pass,String age) { 132 //修改学生信息,仅限于修改年龄和登陆密码 133 Connection con = getConnection(); 134 String sql = "update Student_Message set pass = ? , age = ? where id = ?"; 135 PreparedStatement stmt = null; 136 try { 137 stmt = con.prepareStatement(sql); 138 stmt.setString(1,pass); 139 stmt.setInt(2,Integer.valueOf(age)); 140 stmt.setString(3,id); 141 stmt.executeUpdate(); 142 stmt.close(); 143 con.close(); 144 } catch (SQLException e) { 145 // TODO 自动生成的 catch 块 146 System.out.println(e.getMessage()); 147 } 148 149 } 150 151 }
界面构建的代码:
1 import javax.swing.*; 2 import java.awt.CardLayout; 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.Image; 6 import java.awt.event.ActionEvent; 7 import java.awt.event.ActionListener; 8 9 public class Login extends JFrame{ 10 Student stu_option = null; 11 CardLayout card = null; 12 JPanel Main = null; 13 JPanel panel1 = null; //登陆界面 14 JPanel panel2 = null; //注册界面 15 JPanel panel3 = null; //用户界面 16 public Login() { 17 super("乞丐版管理系统"); 18 card = new CardLayout(); 19 Main = new JPanel(); 20 panel1 = new JPanel(); 21 panel2 = new JPanel(); 22 panel3 = new JPanel(); 23 Main.setSize(400,400); 24 panel1.setSize(400,400); 25 panel2.setSize(400,400); 26 panel3.setSize(400,400); 27 this.setLayout(null); 28 this.add(Main); 29 Main.setLayout(card); 30 Main.add(panel1,"log"); 31 Main.add(panel2,"reg"); 32 Main.add(panel3,"qzone"); 33 34 35 //登录界面的初始化 36 panel1.setLayout(null); 37 JLabel log_id = new JLabel("学号"); 38 log_id.setFont(new Font("微软雅黑",Font.BOLD,20)); 39 JLabel log_pass = new JLabel("密码"); 40 log_pass.setFont(new Font("微软雅黑",Font.BOLD,20)); 41 JTextField log1_id = new JTextField(); 42 log1_id.setFont(new Font("微软雅黑",Font.BOLD,20)); 43 JPasswordField log1_pass = new JPasswordField(); 44 JButton log_button = new JButton("登陆"); 45 log_button.setFont(new Font("微软雅黑",Font.BOLD,20)); 46 JButton reg_button = new JButton("注册"); 47 reg_button.setFont(new Font("微软雅黑",Font.BOLD,20)); 48 JLabel log_tip = new JLabel(); 49 log_tip.setFont(new Font("微软雅黑",Font.BOLD,20)); 50 51 log_id.setBounds(20,50,50,50); //归置各组件的位置 52 log1_id.setBounds(100,50,150,50); 53 log_pass.setBounds(20,110,50,50); 54 log1_pass.setBounds(100,110,150,50); 55 log_tip.setBounds(50,170,300,50); 56 57 log_button.setBounds(50,250,100,50); 58 reg_button.setBounds(200,250,100,50); 59 60 log_button.addActionListener(new ActionListener() { 61 //为登陆按钮添加监听器 62 @Override 63 public void actionPerformed(ActionEvent e) { 64 // TODO 自动生成的方法存根 65 log_tip.setText(""); 66 String id = log1_id.getText(); 67 String pass = new String(log1_pass.getPassword()); 68 if(id==null||pass==null|| 69 id.equals("")||pass.equals("")) 70 log_tip.setText("学号或密码不能为空"); 71 else { 72 stu_option = ConUtil.search(id,pass); 73 if(stu_option!=null) { 74 log_tip.setText(""); 75 log1_id.setText(""); 76 log1_pass.setText(""); 77 78 card.show(Main,"qzone"); 79 } 80 if(stu_option==null) { 81 log_tip.setForeground(Color.RED); 82 log_tip.setText("学号不存在或密码有误!"); 83 } 84 85 } 86 } 87 88 }); 89 90 reg_button.addActionListener(new ActionListener() { 91 92 @Override 93 public void actionPerformed(ActionEvent e) { 94 // TODO 自动生成的方法存根 95 log_tip.setText(""); 96 log1_id.setText(""); 97 log1_pass.setText(""); 98 card.show(Main, "reg"); 99 } 100 101 }); 102 103 panel1.add(log_id); //添加各个组件 104 panel1.add(log_pass); 105 panel1.add(log1_pass); 106 panel1.add(log1_id); 107 panel1.add(log_tip); 108 panel1.add(log_button); 109 panel1.add(reg_button); 110 111 112 //注册界面的初始化 113 panel2.setLayout(null); 114 JLabel reg_id = new JLabel("学号"); 115 reg_id.setFont(new Font("微软雅黑",Font.BOLD,20)); 116 reg_id.setBounds(10,10,50,50); 117 JTextField reg1_id = new JTextField(); 118 reg1_id.setFont(new Font("微软雅黑",Font.BOLD,20)); 119 reg1_id.setBounds(60,10,150,50); 120 JLabel reg_pass = new JLabel("密码"); 121 reg_pass.setBounds(10,60,50,50); 122 reg_pass.setFont(new Font("微软雅黑",Font.BOLD,20)); 123 JTextField reg1_pass = new JTextField(); 124 reg1_pass.setBounds(60,60,150,50); 125 JLabel reg_sex = new JLabel("性别"); 126 reg_sex.setFont(new Font("微软雅黑",Font.BOLD,20)); 127 reg_sex.setBounds(10,110,50,50); 128 JComboBox <String>sex1 = new JComboBox <String>(); 129 sex1.addItem("男"); 130 sex1.addItem("女"); 131 sex1.setBounds(60,110,100,50); 132 JLabel reg_name = new JLabel("姓名"); 133 reg_name.setFont(new Font("微软雅黑",Font.BOLD,20)); 134 reg_name.setBounds(10,160,50,50); 135 JTextField reg1_name = new JTextField(); 136 reg1_name.setFont(new Font("微软雅黑",Font.BOLD,20)); 137 reg1_name.setBounds(60,160,150,50); 138 JLabel reg_mark = new JLabel("分数"); 139 reg_mark.setFont(new Font("微软雅黑",Font.BOLD,20)); 140 reg_mark.setBounds(10,210,50,50); 141 JTextField reg1_mark = new JTextField(); 142 reg1_mark.setBounds(60,210,150,50); 143 JLabel reg_age= new JLabel("年龄"); 144 reg_age.setFont(new Font("微软雅黑",Font.BOLD,20)); 145 reg_age.setBounds(10,260,50,50); 146 JTextField reg1_age = new JTextField(); 147 reg1_age.setBounds(60,260,150,50); 148 JButton reg = new JButton("确认注册"); 149 reg.setFont(new Font("微软雅黑",Font.BOLD,20)); 150 reg.setBounds(50,310,150,50); 151 reg.addActionListener(new ActionListener() { 152 153 @Override 154 public void actionPerformed(ActionEvent e) { 155 // TODO 自动生成的方法存根 156 if(e.getActionCommand().equals("确认注册")) { 157 String name = reg1_name.getText(); 158 String id = reg1_id.getText(); 159 String sex = (String)sex1.getSelectedItem(); 160 String pass = reg1_pass.getText(); 161 String mark = reg1_mark.getText(); 162 String age = reg1_age.getText(); 163 if(name==null||id==null|| 164 sex==null||pass==null|| 165 mark==null||age==null|| 166 name.equals("")||id.equals("")|| 167 sex.equals("")||pass.equals("")|| 168 mark.equals("")||age.equals("")) 169 reg1_name.setText("不能为空!"); 170 else { 171 double mark1 = Double.valueOf(mark); 172 int age1 = Integer.valueOf(age); 173 Student stu = new Student(); 174 stu.setAge(age1); 175 stu.setId(id); 176 stu.setName(name); 177 stu.setSex(sex); 178 stu.setPass(pass); 179 stu.setScore(mark1); 180 ConUtil.add(stu); 181 reg.setText("返回登陆"); 182 183 } 184 185 } 186 else if(e.getActionCommand().equals("返回登陆")) { 187 reg1_name.setText(""); 188 reg1_id.setText(""); 189 reg1_pass.setText(""); 190 reg1_mark.setText(""); 191 reg1_age.setText(""); 192 card.show(Main,"log"); 193 } 194 } 195 196 }); 197 198 panel2.add(reg_id); 199 panel2.add(reg1_id); 200 panel2.add(reg_pass); 201 panel2.add(reg1_pass); 202 panel2.add(reg_sex); 203 panel2.add(sex1); 204 panel2.add(reg_name); 205 panel2.add(reg1_name); 206 panel2.add(reg_mark); 207 panel2.add(reg1_mark); 208 panel2.add(reg_age); 209 panel2.add(reg1_age); 210 panel2.add(reg); 211 212 213 //用户界面的设计 214 215 panel3.setLayout(null); 216 JLabel title = new JLabel("学生空间"); 217 title.setFont(new Font("微软雅黑",Font.BOLD,17)); 218 title.setForeground(Color.RED); 219 title.setBounds(10,10,90,50); 220 JButton button_update = new JButton("修改信息"); 221 button_update.setFont(new Font("微软雅黑",Font.BOLD,15)); 222 button_update.setBounds(0,60,100,50); 223 JButton button_chart = new JButton("个人课表"); 224 button_chart.setBounds(0,110,100,50); 225 button_chart.setFont(new Font("微软雅黑",Font.BOLD,15)); 226 JButton button_mark = new JButton("个人成绩"); 227 button_mark.setBounds(0,160,100,50); 228 button_mark.setFont(new Font("微软雅黑",Font.BOLD,15)); 229 panel3.add(title); 230 panel3.add(button_update); 231 panel3.add(button_chart); 232 panel3.add(button_mark); 233 234 235 236 //用户界面的分界面 237 JPanel userFrame = new JPanel(); 238 CardLayout card1 = new CardLayout(); 239 userFrame.setSize(300,400); 240 userFrame.setLayout(card1); 241 userFrame.setBounds(100,0,300,400); 242 panel3.add(userFrame); 243 JPanel[]user = new JPanel[3]; 244 for(int i = 0;i<3;++i) { 245 user[i] = new JPanel(); 246 user[i].setLayout(null); 247 user[i].setSize(300,400); 248 } 249 250 userFrame.add(user[1],"chart"); 251 userFrame.add(user[2],"mark"); 252 userFrame.add(user[0],"update"); 253 254 255 256 257 258 //修改信息界面 259 JLabel update_title = new JLabel("修改信息"); 260 update_title.setFont(new Font("微软雅黑",Font.BOLD,15)); 261 update_title.setBounds(50,0,100,50); 262 JLabel update_age = new JLabel("年龄"); 263 update_age.setFont(new Font("微软雅黑",Font.BOLD,15)); 264 update_age.setBounds(10,50,50,50); 265 JTextField update_age1 = new JTextField(); 266 update_age1.setFont(new Font("微软雅黑",Font.BOLD,15)); 267 update_age1.setBounds(10,50,100,50); 268 JLabel update_pass = new JLabel("密码"); 269 update_pass.setFont(new Font("微软雅黑",Font.BOLD,15)); 270 update_pass.setBounds(10,100,50,50); 271 JTextField update_pass1 = new JTextField(); 272 update_pass1.setFont(new Font("微软雅黑",Font.BOLD,15)); 273 update_pass1.setBounds(10,150,100,50); 274 JButton update_button = new JButton("确认"); 275 update_button.setFont(new Font("微软雅黑",Font.BOLD,15)); 276 update_button.setBounds(100,200,100,50); 277 user[0].add(update_title); 278 user[0].add(update_pass); 279 user[0].add(update_pass1); 280 user[0].add(update_age1); 281 user[0].add(update_age); 282 user[0].add(update_pass); 283 user[0].add(update_pass1); 284 user[0].add(update_button); 285 update_button.addActionListener(new ActionListener() { 286 public void actionPerformed(ActionEvent e) { 287 String pass = update_pass1.getText(); 288 String age = update_age1.getText(); 289 if(pass.equals("")||age.equals("")) 290 update_pass1.setText("不能为空!"); 291 else { 292 ConUtil.update(stu_option.getId(), pass, age); 293 } 294 } 295 }); 296 297 //个人课表 298 JLabel tips = new JLabel("暂无服务"); 299 tips.setFont(new Font("微软雅黑",Font.BOLD,20)); 300 tips.setBounds(50,50,100,50); 301 JLabel image = new JLabel(new ImageIcon("logo.jpg")); 302 image.setBounds(80,150,100,100); 303 user[1].add(tips); 304 user[1].add(image); 305 306 307 //个人成绩 308 JLabel table = new JLabel("学号:姓名:成绩"); 309 table.setBounds(10,10,200,100); 310 JLabel image1 = new JLabel(new ImageIcon("image1.jpg")); 311 JScrollPane jsp = new JScrollPane(image1,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); 312 jsp.setBounds(0,120,250,200); 313 user[2].add(table); 314 user[2].add(jsp); 315 316 button_update.addActionListener(new ActionListener() { 317 public void actionPerformed(ActionEvent e) { 318 update_age1.setText(String.valueOf(stu_option.getAge())); 319 System.out.println(stu_option.getAge()); 320 update_pass1.setText(stu_option.getPass()); 321 card1.show(userFrame,"update"); 322 } 323 }); 324 325 button_chart.addActionListener(new ActionListener() { 326 public void actionPerformed(ActionEvent e) { 327 card1.show(userFrame,"chart"); 328 } 329 }); 330 331 button_mark.addActionListener(new ActionListener() { 332 public void actionPerformed(ActionEvent e) { 333 table.setText("姓名:"+stu_option.getName()+" 综合成绩:"+stu_option.getScore()); 334 card1.show(userFrame,"mark"); 335 } 336 }); 337 338 this.setVisible(true); 339 this.setResizable(false); 340 this.setDefaultCloseOperation(EXIT_ON_CLOSE); 341 this.setSize(400,400); 342 } 343 public static void main(String[]args) { 344 ConUtil.InitSQL(); 345 SwingUtilities.invokeLater(new Runnable() { 346 public void run() { 347 try { 348 UIManager.setLookAndFeel(UIManager //UIManager界面管理,可以改变当前界面的风格 349 .getSystemLookAndFeelClassName()); //选择当前系统的界面风格 350 } catch (Exception exception) { 351 exception.printStackTrace(); 352 } 353 new Login(); 354 } 355 }); 356 357 } 358 }
放几张验证截图:
注册界面
登陆界面:
登陆成功后显示个人空间:
2.正经的用jsp那一套东西来写一个简单的登陆界面:
首先利用.Java文件完成基本操作,连接数据库、添加、查找数据库中的成员,还有一些异常情况
不过这次我没有把创建表的操作写在代码中,需要手动在SQL Server中建立
UserException.Java
1 public class UserException extends RuntimeException{ 2 public UserException() { 3 super(); 4 } 5 6 public UserException(String arg0, Throwable arg1, boolean arg2, boolean arg3) { 7 super(arg0, arg1, arg2, arg3); 8 // TODO 自动生成的构造函数存根 9 } 10 11 public UserException(String arg0, Throwable arg1) { 12 super(arg0, arg1); 13 // TODO 自动生成的构造函数存根 14 } 15 16 public UserException(String arg0) { 17 super(arg0); 18 // TODO 自动生成的构造函数存根 19 } 20 21 public UserException(Throwable arg0) { 22 super(arg0); 23 // TODO 自动生成的构造函数存根 24 } 25 26 }
User.Java
1 public class User { 2 private int id; 3 private String username; 4 private String password; 5 private String nickname; 6 public int getId() { 7 return id; 8 } 9 public void setId(int id) { 10 this.id = id; 11 } 12 public String getUsername() { 13 return username; 14 } 15 public void setUsername(String username) { 16 this.username = username; 17 } 18 public String getPassword() { 19 return password; 20 } 21 public void setPassword(String password) { 22 this.password = password; 23 } 24 public String getNickname() { 25 return nickname; 26 } 27 public void setNickname(String nickname) { 28 this.nickname = nickname; 29 } 30 }
DBUtil.Java
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.PreparedStatement; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 7 public class DBUtil { 8 public static Connection getConnection() { 9 try { 10 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); 11 12 }catch(ClassNotFoundException | InstantiationException | IllegalAccessException e) { 13 System.out.println(e.getMessage()); 14 } 15 String user = "sa"; //和上面的例子一样,不过这次换了个表 16 String password = "******"; 17 String url = "jdbc:sqlserver://localhost:1433;DasebaseName=master"; 18 Connection connection = null; 19 try { 20 connection = DriverManager.getConnection(url,user,password); 21 }catch(SQLException e) { 22 System.out.println(e.getMessage()); 23 } 24 return connection; 25 } 26 27 public static void close(Connection connection) { 28 29 try { 30 if(connection!=null) 31 connection.close(); 32 } catch (SQLException e) { 33 // TODO 自动生成的 catch 块 34 System.out.println(e.getMessage()); 35 } 36 } 37 38 public static void close(PreparedStatement preparedStatement) { 39 40 try { 41 if(preparedStatement!=null) 42 preparedStatement.close(); 43 } catch (SQLException e) { 44 // TODO 自动生成的 catch 块 45 System.out.println(e.getMessage()); 46 } 47 } 48 49 public static void close(ResultSet resultSet) { 50 51 try { 52 if(resultSet!=null) 53 resultSet.close(); 54 } catch (SQLException e) { 55 // TODO 自动生成的 catch 块 56 System.out.println(e.getMessage()); 57 } 58 } 59 }
UserDao.Java
1 import java.sql.Connection; 2 import java.sql.PreparedStatement; 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import msg.Util.DBUtil; 9 import msg.Util.UserException; 10 import msg.Util.User; 11 12 public class UserDao{ 13 14 15 public void add(User user) { 16 Connection connection = DBUtil.getConnection(); 17 String sql = "select count(*) from t_user where username = ?"; 18 PreparedStatement preparedStatement = null; 19 ResultSet resultSet = null; 20 try { 21 preparedStatement = connection.prepareStatement(sql); 22 preparedStatement.setString(1,user.getUsername()); 23 resultSet = preparedStatement.executeQuery(); 24 while(resultSet.next()) { 25 if(resultSet.getInt(1)>0)throw new UserException("用户已存在"); 26 } 27 sql = "insert into t_user(username,password,nickname) values(?,?,?)"; 28 preparedStatement = connection.prepareStatement(sql); 29 preparedStatement.setString(1,user.getUsername()); 30 preparedStatement.setString(2,user.getPassword()); 31 preparedStatement.setString(3,user.getNickname()); 32 preparedStatement.executeUpdate(); 33 } catch (SQLException e) { 34 // TODO 自动生成的 catch 块 35 System.out.println(e.getMessage()); 36 }finally { 37 DBUtil.close(resultSet); 38 DBUtil.close(preparedStatement); 39 DBUtil.close(connection); 40 } 41 42 } 43 44 45 public void delete(int id) { 46 47 Connection connection = DBUtil.getConnection(); 48 String sql = "delete from t_user where id = ?"; 49 PreparedStatement preparedStatement = null; 50 try { 51 preparedStatement = connection.prepareStatement(sql); 52 preparedStatement.setInt(1, id); 53 preparedStatement.executeUpdate(); 54 } catch (SQLException e) { 55 // TODO 自动生成的 catch 块 56 System.out.println(e.getMessage()); 57 } 58 finally { 59 DBUtil.close(preparedStatement); 60 DBUtil.close(connection); 61 } 62 } 63 64 65 public void delete(String username) { 66 Connection connection = DBUtil.getConnection(); 67 String sql = "delete from t_user where username = ?"; 68 PreparedStatement preparedStatement = null; 69 try { 70 preparedStatement = connection.prepareStatement(sql); 71 preparedStatement.setString(1, username); 72 preparedStatement.executeUpdate(); 73 } catch (SQLException e) { 74 // TODO 自动生成的 catch 块 75 System.out.println(e.getMessage()); 76 } 77 finally { 78 DBUtil.close(preparedStatement); 79 DBUtil.close(connection); 80 } 81 } 82 83 84 public void update(User user) { 85 86 Connection connection = DBUtil.getConnection(); 87 String sql = "update t_user set password = ? , nickname = ? where id = ?"; 88 PreparedStatement preparedStatement= null; 89 90 try { 91 preparedStatement= connection.prepareStatement(sql); 92 preparedStatement.setString(1, user.getPassword()); 93 preparedStatement.setString(2, user.getNickname()); 94 preparedStatement.setInt(3, user.getId()); 95 preparedStatement.executeUpdate(); 96 } catch (SQLException e) { 97 // TODO 自动生成的 catch 块 98 System.out.println(e.getMessage()); 99 }finally { 100 DBUtil.close(preparedStatement); 101 DBUtil.close(connection); 102 } 103 104 } 105 106 107 public User load(int id) { 108 109 Connection connection = DBUtil.getConnection(); 110 String sql = "select * from t_user where id = ?"; 111 PreparedStatement preparedStatement = null; 112 User user = null; 113 ResultSet resultSet = null; 114 try { 115 preparedStatement = connection.prepareStatement(sql); 116 preparedStatement.setInt(1, id); 117 resultSet = preparedStatement.executeQuery(); 118 while(resultSet.next()) { 119 user = new User(); 120 user.setId(id); 121 user.setUsername(resultSet.getString("username")); 122 user.setPassword(resultSet.getString("password")); 123 user.setNickname(resultSet.getString("nickname")); 124 } 125 } catch (SQLException e) { 126 // TODO 自动生成的 catch 块 127 System.out.println(e.getMessage()); 128 }finally { 129 DBUtil.close(resultSet); 130 DBUtil.close(preparedStatement); 131 DBUtil.close(connection); 132 } 133 return user; 134 } 135 136 137 public User load(String username) { 138 Connection connection = DBUtil.getConnection(); 139 String sql = "select * from t_user where username = ?"; 140 PreparedStatement preparedStatement = null; 141 User user = null; 142 ResultSet resultSet = null; 143 try { 144 preparedStatement = connection.prepareStatement(sql); 145 preparedStatement.setString(1, username); 146 resultSet = preparedStatement.executeQuery(); 147 while(resultSet.next()) { 148 149 user = new User(); 150 user.setId(resultSet.getInt("id")); 151 user.setUsername(resultSet.getString("username")); 152 user.setPassword(resultSet.getString("password")); 153 user.setNickname(resultSet.getString("nickname")); 154 } 155 } catch (SQLException e) { 156 // TODO 自动生成的 catch 块 157 System.out.println(e.getMessage()); 158 }finally { 159 DBUtil.close(resultSet); 160 DBUtil.close(preparedStatement); 161 DBUtil.close(connection); 162 } 163 return user; 164 } 165 public List<User> load() { 166 Connection connection = DBUtil.getConnection(); 167 String sql = "select * from t_user"; 168 PreparedStatement preparedStatement = null; 169 ResultSet resultSet = null; 170 List<User> users = new ArrayList<User>(); 171 User user = null; 172 try { 173 preparedStatement = connection.prepareStatement(sql); 174 resultSet = preparedStatement.executeQuery(); 175 while(resultSet.next()) { 176 user = new User(); 177 user.setId(resultSet.getInt("id")); 178 user.setUsername(resultSet.getString("username")); 179 user.setPassword(resultSet.getString("password")); 180 user.setNickname(resultSet.getString("nickname")); 181 users.add(user); 182 } 183 } catch (SQLException e) { 184 // TODO 自动生成的 catch 块 185 System.out.println(e.getMessage()); 186 }finally { 187 DBUtil.close(resultSet); 188 DBUtil.close(preparedStatement); 189 DBUtil.close(connection); 190 } 191 return users; 192 } 193 194 }
接下来就是jsp中的代码:
menu.jsp
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2 <html> 3 <head> 4 <%@ page language="java" contentType="text/html; charset=UTF-8" 5 pageEncoding="UTF-8"%> 6 7 <title>菜单界面</title> 8 </head> 9 <body> 10 11 <a href="addInput.jsp">用户注册界面</a><br> 12 <a href="loginInput.jsp">用户登陆界面</a><br> 13 14 </body> 15 </html>
用户注册界面:addInput.jsp
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2 <html> 3 <head> 4 <%@ page language="java" contentType="text/html; charset=UTF-8" 5 pageEncoding="UTF-8"%> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>用户添加页面</title> 8 </head> 9 <body> 10 11 <% 12 String tip = (String)request.getAttribute("error"); 13 if(tip==null)tip = ""; 14 %> 15 16 <div align="center">用户添加界面</div> 17 <form action="add.jsp" method="get"> 18 <table align="center" border="1" width="500"> 19 <tr> 20 <td>用户名称:</td> 21 <td> 22 <input type="text" name="username" /> 23 </td> 24 </tr> 25 <tr> 26 <td>用户密码:</td> 27 <td> 28 <input type="password" name="password" /> 29 </td> 30 </tr> 31 <tr> 32 <td>用户昵称:</td> 33 <td> 34 <input type="text" name="nickname" /> 35 </td> 36 </tr> 37 <tr align="center"> 38 <td colspan="2"> 39 <input type="submit" value="提交" /> 40 <input type="reset" value="重置" /> 41 </td> 42 </tr> 43 </table> 44 45 </form> 46 <div align="center"><%=tip %></div> 47 </body> 48 </html>
后台处理注册请求:add.jsp
1 <%@page import="msg.Util.User"%> 2 <%@page import="msg.Util.UserException" %> 3 <%@page import="msg.Util.UserDao" %> 4 5 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 6 <html> 7 8 <%@ page language="java" contentType="text/html; charset=UTF-8" 9 pageEncoding="UTF-8"%> 10 <% 11 //接收客户端传递过来的参数 12 String username = request.getParameter("username"); 13 String password = request.getParameter("password"); 14 String nickname = request.getParameter("nickname"); 15 16 if(username==null||"".equals(username.trim())){ 17 18 request.setAttribute("error","用户名不能为空"); 19 20 %> 21 <jsp:forward page = "addInput.jsp"></jsp:forward> 22 <% 23 } 24 User user = new User(); 25 user.setUsername(username); 26 user.setPassword(password); 27 user.setNickname(nickname); 28 29 UserDao userDao = new UserDao(); 30 try{ 31 userDao.add(user); 32 33 %> 34 35 用户保存成功!!<br> 36 <a href="menu.jsp">返回主界面</a><br> 37 <% 38 }catch(UserException e){ 39 %> <h2 style="color:red;font-size:50px">发生错误<%=e.getMessage() %></h2> 40 <a href="addInput.jsp">返回用户添加界面</a><br> 41 <% 42 } 43 %> 44 45 46 47 </html>
登录界面:login.jsp
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2 <html> 3 <head> 4 <%@ page language="java" contentType="text/html; charset=UTF-8" 5 pageEncoding="UTF-8"%> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>用户登陆</title> 8 </head> 9 <body> 10 <% 11 String tip = (String)request.getAttribute("error"); 12 if(tip==null)tip = ""; 13 %> 14 15 <div align="center">用户登陆界面</div> 16 <form action="login.jsp" method="get"> 17 <table align="center" border="1" width="500"> 18 <tr> 19 <td>用户名称:</td> 20 <td> 21 <input type="text" name="username" /> 22 </td> 23 </tr> 24 <tr> 25 <td>用户密码:</td> 26 <td> 27 <input type="password" name="password" /> 28 </td> 29 </tr> 30 <tr align="center"> 31 <td colspan="2"> 32 <input type="submit" value="登陆" /> 33 <input type="reset" value="重置" /> 34 </td> 35 </tr> 36 </table> 37 38 </form> 39 <div align="center"><%=tip %></div> 40 </body> 41 </html>
处理登陆界面:loginInput.jsp
1 <%@page import="msg.Util.UserDao" %> 2 <%@page import="msg.Util.User"%> 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <html> 5 <head> 6 <%@ page language="java" contentType="text/html; charset=UTF-8" 7 pageEncoding="UTF-8"%> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>用户登陆</title> 10 </head> 11 <body> 12 13 <% 14 String username = request.getParameter("username"); 15 String password = request.getParameter("password"); 16 17 if(username==null||"".equals(username.trim())){ 18 request.setAttribute("error","用户名不能为空"); 19 %> 20 <jsp:forward page = "loginInput.jsp"></jsp:forward> 21 <% 22 } 23 if(password==null||"".equals(password)){ 24 request.setAttribute("passerror","密码不能为空"); 25 26 %> 27 <jsp:forward page = "loginInput.jsp"></jsp:forward> 28 <% 29 } 30 User user = null; 31 user = new UserDao().load(username); 32 if(user==null){ 33 request.setAttribute("error","该用户不存在"); 34 %> 35 <jsp:forward page = "loginInput.jsp"></jsp:forward> 36 <% 37 } 38 else if(!user.getPassword().equals(password)){ 39 request.setAttribute("error","密码错误"); 40 %> 41 <jsp:forward page = "loginInput.jsp"></jsp:forward> 42 <% 43 } 44 else{ 45 request.setAttribute("login",user); 46 %> 47 <jsp:forward page = "qzone.jsp"></jsp:forward> 48 <% 49 } 50 %> 51 </body> 52 </html>
登录后用户个人空间:qzone.jsp
<%@page import="msg.Util.User"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>个人主页</title> </head> <body> <% User user = (User)request.getAttribute("login"); %> <div align="center"><%=user.getUsername() %>,你好</div><br> <div align="center">当前没有更多服务内容</div> <a href="menu.jsp">退出账户</a> </body> </html>
验证截图:
主界面:
点击注册:
点击提交后:
返回主界面后,选择登陆:
登陆成功后界面:
点击退出账户后,会返回主界面
三、讲讲目标和计划
其实我个人没什么别的想法,也谈不上有什么计划,目前的想法就是课下靠自己学习Java web的技术,能够形成独立构建网站的能力。
现在的想法是每天至少得投入两个到三个小时学习Java web的内容,这需要耐心,我得慢慢来。
------------------------------------------------------------------------------------------------------------------------------------------------------------
END