Java泛型与数据库应用实例详解
作者:cakincqm
本文实例讲述了Java泛型与数据库应用。分享给大家供大家参考,具体如下:
一 点睛
BaseDao定义了基本的数据库增删查改, 之后可以继承该泛型类,实现各自的增删查改,或者使用超类的增删查改,同时每个继承类还能增加自己的操作。
二 实战
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); } }
三 运行
ok
zhangsan
20
computer
computer
四 怎样在IDEA中导入jar包
可参考附录:IDEA连接数据库(导入jar包)
五 另外一种写法
StudentDao.java
public class StudentDao<M> extends BaseDao<M> { // 这里的M可以是任意合法标识符 }
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<>(); } }
Java连接 MySQL 需要驱动包,最新版下载地址为:http://dev.mysql.com/downloads/connector/j/,解压后得到jar库文件,然后在对应的项目中导入该库文件。
IDEA导入jar过程:新建文件夹(名称任意,这里使用Lib),导入mysql-connector-java-*.*.**-bin.jar如下图
右键点击jar文件,然后点击Add as Library,jar导入成功。后面百度java使用mysql即有详细的访问mysql的代码。
ecplice导入jar过程:新建文件夹(名称任意,这里使用Lib),导入mysql-connector-java-*.*.**-bin.jar如下图
右键-》buildpath-》add to build path
更多java相关内容感兴趣的读者可查看本站专题:《Java面向对象程序设计入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。