发表于: 2018-02-12 20:41:09

1 640


今日完成

继续完善JDBCtemplate代码;

2个properties文件,分别保存了数据库连接数据和sql语句

#DatabaseConnection
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/wodeshujuku
name=root
password=w7217459
#SqlSentense
selectByID=select * from student_copy where ID=?
selectByName=select * from student where name like 
DeleteByName=delete from student_copy where name='
Update=UPDATE student_copy SET name='Pony',update_at="+current_time +" WHERE ID=20
insert=insert into student_copy (name,QQ,onlineID,time_of_enrollment,\
 graduate_institutions,report_link,swear,hearfrom,create_at) VALUES \
 ('Pony', 222222222, 'java-9999','2017-09-10','四川大学','www.lscs.com','老大最帅','知;乎',

测试类

public class Test {
public static void main(String[] args) {
Crud q=new Crud();
       q.selectByID(1,11);
       q.selectByName("'胡%'");
       q.Insert();
       q.Update("Jack",28);
       q.DeleteByName("Gates'");
   }
}

方法类,包含了増删改查,共5个方法;

public class Crud {
static Connection con;
   String a;
   PreparedStatement sql;
   String stb;

   public Crud() {
TemplateDemo c = new TemplateDemo();
       con = c.open();
   }

public void selectByID(int a, int b) {
try {
//            stb = "select * from student_copy where ID=?";
           GetProperties q=new GetProperties();
           Properties prop=q.GetProp();
           stb=prop.getProperty("selectByID");
//           new InitStatement().select(sql,stb);
           sql con.prepareStatement(stb);
           sql.setInt(ab);
           ResultTest resultTest = new ResultTest();
           resultTest.getResult(sql);
       catch (SQLException e) {
           e.printStackTrace();
       }
}
public void selectByName(String a) {
//        stb = "select * from student where name like ";
       GetProperties q=new GetProperties();
       Properties prop=q.GetProp();
       stb=prop.getProperty("selectByName");
       stb stb + a;
       try {
          sql con.prepareStatement(stb);
           ResultTest resultTest = new ResultTest();
           resultTest.getResult(sql);
       catch (SQLException e) {
           e.printStackTrace();
       }
}
public void  DeleteByName(String c) {
        try {
//            stb = "delete from student_copy where name='"+c+"'";
           GetProperties q=new GetProperties();
           Properties prop=q.GetProp();
           stb=prop.getProperty("DeleteByName");
           stb=stb+c;
           sql con.prepareStatement(stb);
           sql.executeUpdate();
       catch (SQLException e) {
            e.printStackTrace();
       }
}
public void Insert() {
        try {
           InsertDate insertDate=new InsertDate();
           String current_time=insertDate.getCurrent_time();
           GetProperties q=new GetProperties();
           Properties prop=q.GetProp();
           stb=prop.getProperty("insert");
           stb=stb+current_time+")";
           sql con.prepareStatement(stb);
           sql.executeUpdate();
       catch (SQLException e) {
            e.printStackTrace();
       }
}
public void Update(String a,int b) {
        try {
           InsertDate insertDate=new InsertDate();
           String current_time=insertDate.getCurrent_time();
           stb "UPDATE student_copy SET name='"+a+"',update_at="+current_time +" WHERE ID="+b;
      /*     GetProperties q=new GetProperties();
           Properties prop=q.GetProp();
           stb=prop.getProperty("Update");*/
           sql con.prepareStatement(stb);
           sql.executeUpdate();
       catch (SQLException e) {
            e.printStackTrace();
       }
  }
}

用于获取properties文件的类

public class GetProperties {
Reader r;
       public Properties GetProp(){
         Properties prop=new Properties();
           try 
             r=new FileReader("E:\\Mypractice\\src\\main\\resources\\db.properties");
           catch (FileNotFoundException e) {
                e.printStackTrace();
           }
          try 
            prop.load(r);
           catch (IOException e) {
               e.printStackTrace();
         }
            return prop;
       }

结果集类

public class ResultTest {
ResultSet result;
   PreparedStatement sql;
   public void getResult(PreparedStatement sql) throws SQLException {
          result = sql.executeQuery();
       while (result.next()) {
           Long ID = result.getLong("ID");
           String name = result.getString("name");
           Long QQ = result.getLong("QQ");
           String onlineID = result.getString("onlineID");
           String time_of_enrollment = result.getString("time_of_enrollment");
           String graduate_institutions = result.getString("graduate_institutions");
           String report_link = result.getString("report_link");
           String swear = result.getString("swear");
           String hearfrom = result.getString("hearfrom");
           System.out.print(ID);
           System.out.print(name);
           System.out.print(QQ);
           System.out.print(onlineID);
           System.out.print(time_of_enrollment);
           System.out.print(graduate_institutions);
           System.out.print(report_link);
           System.out.print(ID);
           System.out.println(hearfrom);
       }

}
}

数据库连接类

public class TemplateDemo {

static Connection conn;
       private static String url;
       private static String name;
       private static String password;
       private static String driver;
       static{
           GetProperties q=new GetProperties();
           Properties prop=q.GetProp();
           driver=prop.getProperty("driver");
           url=prop.getProperty("url");
           name=prop.getProperty("name");
           password=prop.getProperty("password");
              }
public Connection open(){
           try {
               Class.forName(driver);
           catch (ClassNotFoundException e) {
               e.printStackTrace();
           }
          try {
          conn= DriverManager.getConnection(url,name,password);
           catch (SQLException e) {
               e.printStackTrace();
           }
           System.out.println("连接成功");
           return conn;

       }

}

用于获取当前时间的类

public class InsertDate {
private static long current_time;
   private static String update_at;
   private static String c;
   private String u;

 public String getCurrent_time(){
     current_time=System.currentTimeMillis();
     c=Long.toString(current_time);
     return c;
   }

}

明天计划

回家过年期间抽时间学习;

遇到问题

——————

收获

回过头重新写一遍以前写的代码,并完善,提升了自己对JDBCTemplate及JAVA基础的认识;


返回列表 返回列表
评论

    分享到