java面向?qū)ο?實(shí)驗(yàn)十二數(shù)據(jù)庫(kù)編程_第1頁(yè)
java面向?qū)ο?實(shí)驗(yàn)十二數(shù)據(jù)庫(kù)編程_第2頁(yè)
java面向?qū)ο?實(shí)驗(yàn)十二數(shù)據(jù)庫(kù)編程_第3頁(yè)
java面向?qū)ο?實(shí)驗(yàn)十二數(shù)據(jù)庫(kù)編程_第4頁(yè)
java面向?qū)ο?實(shí)驗(yàn)十二數(shù)據(jù)庫(kù)編程_第5頁(yè)
已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、學(xué)號(hào)姓名實(shí)驗(yàn)序號(hào)實(shí)驗(yàn)十二實(shí)驗(yàn)名稱數(shù)據(jù)庫(kù)編程實(shí)驗(yàn)地點(diǎn)實(shí)驗(yàn)日期實(shí)驗(yàn)內(nèi)容1.在數(shù)據(jù)庫(kù)(xscj)里建立如下數(shù)據(jù)表tb_student:使用的數(shù)據(jù)庫(kù)系統(tǒng)不限。2.使用Java JDBC對(duì)該數(shù)據(jù)表進(jìn)行增刪改查,輸出到屏幕。JDBC連接方式不限。(1)從鍵盤輸入數(shù)據(jù)并能寫入數(shù)據(jù)表中(2)修改學(xué)生成績(jī)(3)刪除指定學(xué)號(hào)的數(shù)據(jù)(4)按學(xué)生姓名模糊查詢實(shí)驗(yàn)過(guò)程及步驟1. 在SQL數(shù)據(jù)庫(kù)中創(chuàng)建xscj數(shù)據(jù)庫(kù),建立數(shù)據(jù)表tb_student。2. 使用Java JDBC對(duì)該數(shù)據(jù)表進(jìn)行增刪改查,輸出到屏幕。一:BaseDao:package com.util;import java.sql.Connection;im

2、port java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;public class BaseDao protected Connection conn=null; protected Statement stmt=null;protected PreparedStatement pstmt=null;protected ResultSet rs=null;public Connection getConn()Connectio

3、n aconn=null;tryClass.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");aconn=DriverManager.getConnection實(shí)驗(yàn)過(guò)程及步驟("jdbc:sqlserver:/localhost:1433;DatabaseName=xscj","sa","123456");catch(Exception ex)ex.printStackTrace();return aconn;public void closeAll()

4、tryif(rs!=null)rs.close();if(pstmt!=null)pstmt.close();if(stmt!=null)stmt.close();if(conn!=null)conn.close();catch(Exception ex)ex.printStackTrace();二:StudentDaopackage com.dao;import java.util.List;import com.bean.Student;public interface StudentDao public int insert(Student stu); public int delete

5、student(int stuno); public List<Student> getStudentByName(String stuname); public int modify(Student stu);三:StudentDaoImplpackage com.dao.impl;import java.util.ArrayList;import java.util.List;import com.bean.Student;import com.dao.StudentDao;import com.util.BaseDao;public class StudentDaoImpl

6、extends BaseDao implements StudentDao public int insert(Student stu) int row = 0;try conn = this.getConn();String sql = "insert into tb_student(stuno,stuname,stuscore) values(?,?,?)"實(shí)驗(yàn)過(guò)程及步驟pstmt = conn.prepareStatement(sql);pstmt.setInt(1, stu.getStuNo();pstmt.setString(2, stu.getStuname()

7、;pstmt.setInt(3, stu.getStuscore();row = pstmt.executeUpdate(); catch (Exception ex) ex.printStackTrace(); finally this.closeAll();return row;public int deletestudent(int stuno) int row = 0;try conn = this.getConn();String sql = "delete tb_student where stuno=? "pstmt = conn.prepareCall(sq

8、l);pstmt.setInt(1, stuno);row = pstmt.executeUpdate(); catch (Exception ex) ex.printStackTrace(); finally this.closeAll();return row; public List<Student> getStudentByName(String stuname) List<Student> stulist = new ArrayList<Student>();try conn = this.getConn();String sql = "

9、select stuno,stuname,stuscore from tb_student where stuname like ?"pstmt = conn.prepareStatement(sql);pstmt.setString(1, "%" + stuname + "%");rs = pstmt.executeQuery();while (rs.next() Student stu1 = new Student();stu1.setStuNo(rs.getInt(1);stu1.setStuname(rs.getString(2);st

10、u1.setStuscore(rs.getInt(3);stulist.add(stu1); catch (Exception ex) ex.printStackTrace(); finally 實(shí)驗(yàn)過(guò)程及步驟this.closeAll();return stulist;public int modify(Student stu) int row = 0;try conn = this.getConn();String sql = "update tb_student set stuno=?,stuname=?,stuscore=? where stuno=?"pstmt

11、= conn.prepareStatement(sql);pstmt.setInt(1, stu.getStuNo();pstmt.setString(2, stu.getStuname();pstmt.setInt(3, stu.getStuscore();pstmt.setInt(4, stu.getStuNo();row = pstmt.executeUpdate(); catch (Exception ex) ex.printStackTrace(); finally this.closeAll();return row;四:Studentpackage com.bean;public

12、 class Student private int stuNo;private String stuname;private int stuscore;public int getStuNo() return stuNo; public void setStuNo(int stuNo) this.stuNo = stuNo;public String getStuname() return stuname;public void setStuname(String stuname) this.stuname = stuname;實(shí)驗(yàn)過(guò)程及步驟public int getStuscore()

13、return stuscore;public void setStuscore(int stuscore) this.stuscore = stuscore;(1)從鍵盤輸入數(shù)據(jù)并能寫入數(shù)據(jù)表中package com.test;import java.util.Scanner;import com.bean.Student;import com.dao.impl.StudentDaoImpl;public class test1 public static Student stuInPut() / 輸入學(xué)生信息Student stu = new Student();Scanner in = n

14、ew Scanner(System.in);System.out.println("請(qǐng)輸入學(xué)號(hào)");stu.setStuNo(in.nextInt();Scanner in1 = new Scanner(System.in);System.out.println("請(qǐng)輸入姓名");stu.setStuname(in1.nextLine();Scanner in2 = new Scanner(System.in);System.out.println("請(qǐng)輸入成績(jī)");stu.setStuscore(in2.nextInt();retu

15、rn stu;public static void main(String args) Student stu = new Student();StudentDaoImpl studentdaoimpl = new StudentDaoImpl();stu = stuInPut(); / 插入學(xué)生信息if (studentdaoimpl.insert(stu) != 0)System.out.println("成功插入學(xué)生信息!");實(shí)驗(yàn)過(guò)程及步驟(2)修改學(xué)生成績(jī)package com.test;import com.bean.Student;import com.dao

16、.impl.StudentDaoImpl;public class test2 public static void main(String args) Student stu = new Student();StudentDaoImpl studentdaoimpl = new StudentDaoImpl(); stu.setStuNo(1); /修改學(xué)生信息,將編號(hào)為一的學(xué)生成績(jī)改為88 stu.setStuscore(88); stu.setStuname("zs"); if( studentdaoimpl.modify(stu)!=0 ) System.out.p

17、rintln("成功更改學(xué)生信息!");實(shí)驗(yàn)過(guò)程及步驟(3)刪除指定學(xué)號(hào)的數(shù)據(jù)package com.test;import com.dao.impl.StudentDaoImpl;public class test3 public static void main(String args) StudentDaoImpl studentdaoimpl = new StudentDaoImpl();if (studentdaoimpl.deletestudent(3) != 0) / 刪除學(xué)號(hào)為3的學(xué)生System.out.println("成功刪除學(xué)生信息!&qu

18、ot;);(4)按學(xué)生姓名模糊查詢 (查詢姓名包含“l(fā)”的學(xué)生信息)package com.test;import java.util.ArrayList;import java.util.List;import com.bean.Student;import com.dao.impl.StudentDaoImpl;public class test4 public static void main(String args) StudentDaoImpl studentdaoimpl = new StudentDaoImpl();List<Student> stulist = new ArrayList<Student>();stulist = studentdaoimpl.getStudentByName(&

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論