您好,登錄后才能下訂單哦!
本文實(shí)例講述了Java泛型與數(shù)據(jù)庫應(yīng)用。分享給大家供大家參考,具體如下:
一 點(diǎn)睛
BaseDao定義了基本的數(shù)據(jù)庫增刪查改, 之后可以繼承該泛型類,實(shí)現(xiàn)各自的增刪查改,或者使用超類的增刪查改,同時(shí)每個(gè)繼承類還能增加自己的操作。
二 實(shí)戰(zhàn)
1 BaseDao.java
import java.lang.reflect.Field; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class BaseDao<T> { Connection connection; PreparedStatement pStatement; String urlString = "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF8"; String drivString = "com.mysql.jdbc.Driver"; void OpenDB() { try { if (connection == null) { Class.forName(drivString); this.connection = DriverManager.getConnection(urlString, "root", "123456"); } } catch (Exception e) { e.printStackTrace(); } } void CloseDB() { try { if (pStatement != null) pStatement.close(); if (connection != null) connection.close(); } catch (Exception e) { e.printStackTrace(); } } public void Save( T t ) { OpenDB(); String table = ""; String sqlString = "insert into "; try { table = t.getClass().getSimpleName(); Field[] fields = t.getClass().getDeclaredFields(); sqlString += table; String sqlNameString = ""; String sqlValString = ""; for (Field field : fields) { sqlNameString += field.getName() + ","; sqlValString += "?,"; } sqlNameString = sqlNameString.substring(0, sqlNameString.length() - 1); sqlValString = sqlValString.substring(0, sqlValString.length() - 1); pStatement = connection.prepareStatement(sqlString + " (" + sqlNameString + ") values (" + sqlValString + ")"); int n = 1; for (Field field : fields) { field.setAccessible(true); System.out.println(field.get(t).toString()); pStatement.setString(n, field.get(t).toString()); n++; } pStatement.executeUpdate(); CloseDB(); } catch (Exception e) { e.printStackTrace(); CloseDB(); } } public void Del( T t ) { } public void Update( T t ) { } public void Search( T t ) { } }
2 StudentDao.java
public class StudentDao extends BaseDao<Student> { }
3 TeacherDao.java
public class TeacherDao extends BaseDao<Teacher> { }
4 Student.java
public class Student { public String name; public int age; public String dept; /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the age */ public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } /** * @return the dept */ public String getDept() { return dept; } /** * @param dept the dept to set */ public void setDept(String dept) { this.dept = dept; } }
5 Teacher.java
public class Teacher { }
6 TestDao.java
public class TestDao { public static void main(String[] args) { System.out.println("ok"); Student stud=new Student(); stud.age=20; stud.name="zhangsan"; stud.dept="computer"; StudentDao sd=new StudentDao(); sd.Save(stud); System.out.println(stud.dept); } }
三 運(yùn)行
ok
zhangsan
20
computer
computer
四 怎樣在IDEA中導(dǎo)入jar包
可參考附錄:IDEA連接數(shù)據(jù)庫(導(dǎo)入jar包)
五 另外一種寫法
StudentDao.java
public class StudentDao<M> extends BaseDao<M> { // 這里的M可以是任意合法標(biāo)識符 }
TestDao.java
public class TestDao { public static void main(String[] args) { System.out.println("ok"); Student stud=new Student(); stud.age=20; stud.name="zhangsan2"; stud.dept="computer"; StudentDao<Student> sd=new StudentDao<>(); // 這里要說明是Student sd.Save(stud); System.out.println(stud.dept); TeacherDao<Teacher> te= new TeacherDao<>(); } }
附:IDEA連接數(shù)據(jù)庫(導(dǎo)入jar包)
Java連接 MySQL 需要驅(qū)動包,最新版下載地址為:http://dev.mysql.com/downloads/connector/j/,解壓后得到j(luò)ar庫文件,然后在對應(yīng)的項(xiàng)目中導(dǎo)入該庫文件。
IDEA導(dǎo)入jar過程:新建文件夾(名稱任意,這里使用Lib),導(dǎo)入mysql-connector-java-*.*.**-bin.jar如下圖
右鍵點(diǎn)擊jar文件,然后點(diǎn)擊Add as Library,jar導(dǎo)入成功。后面百度java使用mysql即有詳細(xì)的訪問mysql的代碼。
ecplice導(dǎo)入jar過程:新建文件夾(名稱任意,這里使用Lib),導(dǎo)入mysql-connector-java-*.*.**-bin.jar如下圖
右鍵-》buildpath-》add to build path
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。