Ⅰ 如何使用java寫一個增刪改查的通用類,不需要連接資料庫
importjava.util.LinkedList;
importjavax.swing.text.html.HTMLDocument.Iterator;
publicclassTestKnow{
publicstaticvoidmain(String[]args){
Students1=newStudent(1,"s1",15);
Students2=newStudent(2,"s2",15);
Students3=newStudent(3,"s3",15);
Gradeg=newGrade(1);
g.add(s1);g.add(s2);g.add(s3);//增
g.delete(s1);//刪
Students4=newStudent(4,"s4",12);
g.change(s2,s4);//改
Studentss=g.check(3);//查
System.out.println(ss.name);
}
}
classStudent{
intnum;//學號
Stringname;
intage;
publicStudent(intnum,Stringname,intage){
this.num=num;
this.name=name;
this.age=age;
}
}
classGrade{
intgnum;
LinkedList<Student>grade;
publicGrade(intgnum){
this.gnum=gnum;
grade=newLinkedList<Student>();
}
publicvoidadd(Students){
grade.add(s);
}
publicStudentcheck(intn){
java.util.Iterator<Student>it=grade.iterator();
while(it.hasNext()){
Studenttemp=it.next();
if(temp.num==n)
returntemp;
}
returnnull;
}
publicvoiddelete(Students){
if(grade!=null)
if(check(s.num)!=null)
grade.remove(s);
else
System.out.println("沒有此人");
else
System.out.println("班裡沒人");
}
publicvoidchange(Students1,Students2){
if(grade!=null)
if(check(s1.num)!=null)
{
grade.remove(s1);
grade.add(s2);
}
else
System.out.println("沒有要替換的人");
else
System.out.println("班裡沒人");
}
}
Ⅱ 用java做界面連接oracle資料庫實現增刪改查功能,具體步驟怎麼做
如果不用ORM框架,那麼直接引入ojdbc6.jar 驅動包,然後在代碼中JDBC配置;
然後可以使用jdbc實現增刪改查,比如下面代碼
ResultSetrs=null;
Statementstmt=null;
Connectionconn=null;
try{
Class.forName("oracle.jdbc.driver.oracleDriver");
//neworacle.jdbc.driver.oracleDriver();
conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:yuewei","scott","tiger");
stmt=conn.createStatement();
rs=stmt.executeQuery("select*fromdept");
while(rs.next()){
System.out.println(rs.getString("deptno"));
//System.out.println(rs.getInt("deptno"));
}
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}catch(SQLExceptione){
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
rs=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(SQLExceptione){
e.printStackTrace();
}
}
2.
Ⅲ java增刪改查功能怎麼實現
你所說的增刪改查是資料庫的命令操作。在Java編程中,在Java和資料庫完成連接以後,可以調用資料庫的select、delete、updata等命令。你也可以把相關的命令變成一個字元串對象,這樣調用起來會更簡單。