java語句實現數據庫的增刪查改(面向對象方式,以student爲例)

共創建了三個包 分別爲com.hkd.myschool.biz(包含main類和studentBiz類),com.hkd.myschool.dao(包含BaseDao類和StudentDao類),

com.hkd.myschool.enty(包含student類) ,具體實現代碼如下:

package com.hkd.myschool.biz;


import java.util.Scanner;


public class main {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
studentBiz sb =new studentBiz();
int option = 0;
show();
while(option<5){
option =scan.nextInt();
switch (option) {
case 1:
sb.search();//查詢學生
show();
break;
case 2:
sb.delete();//刪除學生
show();
break;
case 3:
sb.add();//增加學生
show();
break;
case 4:
sb.update();//修改學生信息
show();
break;
}

}
}


private static void show() {
System.out.println("****************************************");
System.out.println("*                1、查詢學生                                    *");
System.out.println("*                2、刪除學生                                    *");
System.out.println("*                3、增加學生                                    *");
System.out.println("*                4、修改學生信息                            *");
System.out.println("****************************************");
System.out.print("請選擇你要執行的功能:");
}




}

package com.hkd.myschool.biz;


import java.util.List;
import java.util.Scanner;


import com.hkd.myschool.dao.StudentDao;
import com.hkd.myschool.entity.student;


public class studentBiz {
student stu =new student();
Scanner scan = new Scanner(System.in);


public void add(){
StudentDao sd =new StudentDao();
System.out.print("請輸入要添加的學生姓名:");
String name=scan.next();
stu.setName(name);
System.out.print("請輸入要添加的學生年齡:");
int age =scan.nextInt();
stu.setAge(age);
System.out.print("請輸入要添加的學生電話:");
String tel =scan.next();
stu.setTel(tel);
int a =sd.add(stu);
if(a>0){
System.out.println("添加成功");
}else{
System.out.println("添加失敗");
}
}

public void delete(){
StudentDao sd =new StudentDao();
search();
System.out.print("輸入你要刪除的學號:");
int id =scan.nextInt();
int a =sd.delete(id);
if(a>0){
System.out.println("刪除成功");
}else{
System.out.println("刪除失敗");
}

}

public void update(){
StudentDao sd =new StudentDao();
search();
System.out.println("請輸入要更改的編號:");
stu.setStudentNo(scan.nextInt());
System.out.println("請輸入要更改的姓名:");
stu.setName(scan.next());
System.out.println("請輸入要更改的年齡:");
stu.setAge(scan.nextInt());
System.out.println("請輸入要更改的電話號碼:");
stu.setTel(scan.next());
int a =sd.update(stu);
if(a>0){
System.out.println("更改成功");
}else{
System.out.println("更改失敗");
}




}

public void search(){
StudentDao sd =new StudentDao();
System.out.println("編號\t姓名\t年齡\t電話號碼");
List <student> list =sd.selectAll();
for(student stu:list){
System.out.println(stu.getStudentNo()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getTel());
}
}
}

package com.hkd.myschool.dao;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class BaseDao {

String DBUser ="root";//用戶名
String DBPwd= "root";//密碼
String DBHost ="localhost";//服務器地址
String DBName ="myschool";//數據庫名稱
int Port1 =3306;//數據庫端口
int Port2 =1433;
int DBType =1;//1代表mysql 2 代表sqlserver
Connection conn;//連接對象
PreparedStatement ps;//查詢對象
ResultSet rs;//結果集

public void open(){
try{
if(DBType==1){
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://"+DBHost+":"+Port1+"/"+DBName,DBUser,DBPwd);
}else if(DBType==2){
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://"+DBHost+":"+Port2+";databasename="+DBName,DBUser,DBPwd);
}
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("驅動加載失敗");
e.printStackTrace();
}
catch(Exception e){
System.out.println("數據庫鏈接失敗");
e.printStackTrace();
}
}
public void close(){
try{
if(rs!=null) rs.close();//如果結果集存在,就關閉結果集
ps.close();//關閉查詢對象
conn.close();//關閉鏈接對象
}
catch(Exception e){
e.printStackTrace();
}
}
/**
* 執行增刪改
* @param sql語句
* @param parm參數
* @return 受影響的行數
*/
public int executeUpdate(String sql,Object[] parm){
open();
int a =0;
try{
ps=conn.prepareStatement(sql);
if(parm!=null){
for(int i=0;i<parm.length;i++){
ps.setObject(i+1, parm[i]);
}
}
a=ps.executeUpdate();//執行

}catch(SQLException e){
e.printStackTrace();
}
close();
return a;
}
/**
* 執行查詢結果集
* @param sql 語句
* @param parm 參數
* @return 結果集
*/
public ResultSet executeQuery(String sql,Object[] parm){
open();
try{
ps=conn.prepareStatement(sql);
if(parm!=null){
for(int i=0;i<parm.length;i++){
ps.setObject(i+1, parm[i]);
}
}
rs=ps.executeQuery();//執行

}catch(SQLException e){
e.printStackTrace();
}
return rs;
}
}

package com.hkd.myschool.dao;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import com.hkd.myschool.entity.student;


public class StudentDao extends BaseDao {

public int add(student stu){
String sql="insert into student values(null,?,?,?)";
Object parm[]={stu.getName(),stu.getAge(),stu.getTel()};
int a =executeUpdate(sql, parm);
return a;
}
public int delete(int id){
String sql="delete from student where studentNo=? ";
Object parm[]={id};
int a =executeUpdate(sql, parm);
return a;

}
public int update(student stu){
String sql="update student set  name=? , age=? , tel=? where studentNo =?";
Object parm[]={stu.getName(),stu.getAge(),stu.getTel(),stu.getStudentNo()};
int a =executeUpdate(sql, parm);
return a;
}
public List<student> selectAll(){
String sql="select * from student";
List<student> list=new ArrayList<student>();
rs=executeQuery(sql, null);
try {
while(rs.next()){
student stu = new student();
stu.setStudentNo(rs.getInt("studentNo"));
stu.setName(rs.getString("name"));
stu.setAge(rs.getInt("age"));
stu.setTel(rs.getString("tel"));
list.add(stu);
}
close();//在這裏進行關閉

catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;

}
}

package com.hkd.myschool.entity;


public class student {
String name;
int age;
int studentNo;
String tel;


public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getStudentNo() {
return studentNo;
}
public void setStudentNo(int studentNo) {
this.studentNo = studentNo;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}



}

發佈了54 篇原創文章 · 獲贊 68 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章