java(jsp)简单实现数据库学生信息的增删改查案例

时间:2021-01-12 19:24:59

1:建立学生类,建立主函数进行测试

package cn.edu.hpu.stu;
import java.util.Scanner;

import cn.edu.hpu.jdbclink.*;

/**
*
* @author Programer Guo
* 提供学生实体的javabean类
*/
public class Student {
private int id;
private String name;
private String myclass;
private double score;
private String email;
private int age;
public Student(){

}
public String getname() {
// TODO Auto-generated method stub
return this.name;
}
public String getmyclass() {
// TODO Auto-generated method stub
return this.myclass;
}
public double getScore() {
// TODO Auto-generated method stub
return this.score;
}
public int getage() {
// TODO Auto-generated method stub
return this.age;
}
public String getemail() {
// TODO Auto-generated method stub
return this.email;
}
public void setname(String string) {
// TODO Auto-generated method stub
this.name=string;
}
public void setmycalss(String string) {
// TODO Auto-generated method stub
this.myclass=string;
}
public void setscore(double d) {
// TODO Auto-generated method stub
this.score=d;
}
public void setemail(String string){
this.email=string;
}
public void setage(int d){
this.age=d;
}
public static void main(String[] args){
System.out.println("请选择:");
System.out.println("1:添加");
System.out.println("2:删除");
System.out.println("3:修改");
System.out.println("4:查询");
System.out.println("0:结束");
Scanner sc = new Scanner(System.in);
while(true){
int a = sc.nextInt();
if(a==1){
Student stu = new Student();
String name = null;
int age = 0;
String myclass = null;
double score = 0.0;
String email = null;
System.out.println("请输入信息:");
System.out.println("姓名:");
name = sc.next();
stu.setname(name);
System.out.println("年龄:");
age = sc.nextInt();
stu.setage(age);
System.out.println("班级:");
myclass = sc.next();
stu.setmycalss(myclass);
System.out.println("成绩:");
score = sc.nextDouble();
stu.setscore(score);
System.out.println("email:");
email = sc.next();
stu.setemail(email);
if(new Doinsert().addStudent(stu)){
System.out.println("成功");
}else{
System.out.println("失败");
}
}else if(a==2){
System.out.println("请输入删除姓名:");
String name = sc.next();
if(new Dodelete().delete(name)){
System.out.println("成功");
}else{
System.out.println("失败");
}
}else if(a==3){
System.out.println("请输入修改人姓名:(仅允许修改成绩)");
String name = sc.next();
System.out.println("请输入修改成绩:");
Double score= sc.nextDouble();
if(new Doupdate().update(name,score)){
System.out.println("成功");
}else{
System.out.println("失败");
}
}else if(a==4){
new Doselect().show();
}else if(a==0){
break;
}else {
System.out.println("输入有误!");
}
}
}
}

2.增加操作类

package cn.edu.hpu.jdbclink;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.sql.Connection;
import java.sql.Statement;

import com.mysql.jdbc.Driver;

import cn.edu.hpu.stu.*;

/**
*
* @author Programer Guo
*
*/
public class Doinsert {
public boolean addStudent(Student stu){
Connection conn = null;
//Statement对象
Statement st = null;
//结果集对象
ResultSet rs = null;
//驱动
String driver = "com.mysql.jdbc.Driver";
//连接数据库的URL
String url = "jdbc:mysql://localhost/db_jdbc?user=root&password=guo941102&useUnicode=true&characterEncoding=UTF-8";
String sql = "insert into tbl_students (name,age,myclass,score ,email) values('"+stu.getname()+"','"+stu.getage()+"','"+stu.getmyclass()+"','"+stu.getScore()+"','"+stu.getemail()+"')";
boolean flag = false;
try{
Class.forName(driver);
conn = DriverManager.getConnection(url);
st = conn.createStatement();
int i = st.executeUpdate(sql);
if(i==1){
flag = true;
}
}catch (Exception e){
e.printStackTrace();
}finally{
if(st!=null){
try{
st.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if(conn !=null){
try{
conn.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
return flag;
}
public static void main(String[] args){

Doinsert doinsert = new Doinsert();
Student stu = new Student();
stu.setname("孙连伟");
stu.setage(33);
stu.setmycalss("java001");
stu.setscore(98.00);
stu.setemail("4444444@qq.com");
boolean flag= doinsert.addStudent(stu);
if(flag){
System.out.println("学生信息添加成功");
}else{
System.out.println("学生信息添加失败");
}
}
}
3.修改操作类

package cn.edu.hpu.jdbclink;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class Doupdate {
public boolean update(String name,double score){
boolean flag = false;
Connection conn = null;
Statement st = null;
String driver =null;
String url = null;
String sql = null;
try{
driver="com.mysql.jdbc.Driver";
url = "jdbc:mysql://localhost/db_jdbc?user=root&password=guo941102&useUnicode=true&characterEncoding=UTF-8";
sql = "update tbl_students set score = "+score+" where name = '"+name+"'";
Class.forName(driver);
conn = DriverManager.getConnection(url);
st = conn.createStatement();
int i = st.executeUpdate(sql);
if(i>=1){
flag=true;
}
}catch (Exception e){
e.printStackTrace();
}
return flag;
}
public static void main(String[] args){
Doupdate update = new Doupdate();
boolean flag = update.update("孙连伟",120);
if(flag){
System.out.println("更新成功");
}else{
System.out.println("更新失败");
}
}
}

4.删除操作类
package cn.edu.hpu.jdbclink;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class Dodelete {
public boolean delete(String name){
boolean flag = false;
Connection conn = null;
Statement st = null;
String driver =null;
String url = null;
String sql = null;
try{
driver="com.mysql.jdbc.Driver";
url = "jdbc:mysql://localhost/db_jdbc?user=root&password=guo941102&useUnicode=true&characterEncoding=UTF-8";
sql = "delete from tbl_students where name = '"+name+"'";
Class.forName(driver);
conn = DriverManager.getConnection(url);
st = conn.createStatement();
int i = st.executeUpdate(sql);
if(i>=1){
flag=true;
}
}catch (Exception e){
e.printStackTrace();
}
return flag;
}
public static void main(String[] args){
Dodelete delete = new Dodelete();
boolean flag = delete.delete("孙连伟");
if(flag){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}
}

该案例仅仅实现功能,而且是doc版运行测试,并未嵌入标记语言。jsp更多实现,正在学习中。。。