今天是冲刺的第四天,主要在安卓方面写了一些sql语句以及图片的获取和bitmap转化为字符串。
遇到的困难:不知道如何获取imageView的图片,图片储存到数据库加载资源过慢的情况。
解决思路:获取安卓中已经加载好的图片:Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
明天计划完成的任务:页面实现下拉刷新
附上编写的代码:
DBOpenHelper.java
package com.itheima.cloudnotes.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBOpenHelper { private static String driver = "com.mysql.jdbc.Driver";//MySQL 驱动 private static String url = "jdbc:mysql://39.101.190.190:3306/test";//MYSQL数据库连接Url private static String user = "root";//用户名 private static String password = "123";//密码 /** * 连接数据库 * */ public static Connection getConn(){ Connection conn = null; try { Class.forName(driver);//获取MYSQL驱动 conn = (Connection) DriverManager.getConnection(url, user, password);//获取连接 } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } /** * 增删改关闭数据库 * */ public static void closeAll(Connection conn, PreparedStatement ps){ if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 查询关闭数据库 * */ public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs){ if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
DBService.java
package com.itheima.cloudnotes.db; import com.itheima.cloudnotes.enity.Note; import com.itheima.cloudnotes.enity.User; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class DBService { private StringAndBitmap sab; private Connection conn=null; //打开数据库对象 private PreparedStatement ps=null;//操作整合sql语句的对象 private ResultSet rs=null;//查询结果的集合 //DBService 对象 public static DBService dbService=null; //构造方法 私有化 private DBService(){ } //获取操作数据库单例类对象 public static DBService getDbService(){ if(dbService==null){ dbService=new DBService(); } return dbService; } //判断用户是否已经注册过 public boolean judgeUserExist(User user) { boolean flag=true; String sql="select * from user where user.account=?"; //获取数据库连接对象 conn=DBOpenHelper.getConn(); try{ if(conn!=null&&(!conn.isClosed())){ ps=conn.prepareStatement(sql); if(ps!=null){ ps.setString(1,user.getAccount()); rs=ps.executeQuery(); if(rs.next()==false){ flag=true; }else { flag=false; } } } }catch (SQLException e){ e.printStackTrace(); } DBOpenHelper.closeAll(conn,ps,rs); //关闭相关操作 return flag; } //判断用户是否已经注册过 public boolean judgeUserLegal(User user) { boolean flag=false; String sql="select * from user where user.account=? and user.password=?"; //获取数据库连接对象 conn=DBOpenHelper.getConn(); try{ if(conn!=null&&(!conn.isClosed())){ ps=conn.prepareStatement(sql); if(ps!=null){ ps.setString(1,user.getAccount()); ps.setString(2,user.getPassword()); rs=ps.executeQuery(); if(rs.next()){ flag=true; }else { flag=false; } } } }catch (SQLException e){ e.printStackTrace(); } DBOpenHelper.closeAll(conn,ps,rs); //关闭相关操作 return flag; } //用户注册:添加用户相关信息 public int insertUserData(User user,String bitToString){ int result=-1; if(user!=null){ conn=DBOpenHelper.getConn(); String sql="insert into user (pickname,account,password,grade,user_icon) values (?,?,?,?,?)"; try{ boolean closed=conn.isClosed(); if((conn!=null)&&(!closed)){ ps=conn.prepareStatement(sql); String pickname=user.getPickname(); String account=user.getAccount(); String password=user.getPassword(); String grade="大一"; if(ps!=null){ ps.setString(1,pickname); ps.setString(2,account); ps.setString(3,password); ps.setString(4,grade); ps.setString(5,bitToString); result=ps.executeUpdate();//返回1 执行成功 } } } catch (SQLException e) { e.printStackTrace( ); } } DBOpenHelper.closeAll(conn,ps); return result; } public List<Note> getNotes() { List<Note> list=new ArrayList<>(); String sql="select * from note"; conn=DBOpenHelper.getConn(); try{ if(conn!=null&&(!conn.isClosed())){ ps=conn.prepareStatement(sql); if(ps!=null) { if(sab==null) { sab=new StringAndBitmap(); } rs=ps.executeQuery(); while (rs.next()) { Note note=new Note(); note.setId(rs.getInt("id")); note.setIcon(sab.stringToBitmap(rs.getString("icon"))); note.setWriter_name(rs.getNString("writer_name")); note.setCourse(rs.getString("course")); note.setTitle(rs.getString("title")); note.setDate(rs.getString("date")); list.add(note); } } } }catch (SQLException e){ e.printStackTrace(); } DBOpenHelper.closeAll(conn,ps,rs); //关闭相关操作 return list; } public int getUserId(String account) { int userId=0; String sql="select id from user where account=?"; conn=DBOpenHelper.getConn(); try{ if(conn!=null&&(!conn.isClosed())){ ps=conn.prepareStatement(sql); if(ps!=null) { ps.setString(1,account); rs=ps.executeQuery(); if(rs.next()) { userId=rs.getInt("id"); } } } }catch (SQLException e){ e.printStackTrace(); } DBOpenHelper.closeAll(conn,ps,rs); //关闭相关操作 return userId; } public int insertCollect(int user_id,int note_id) { int result=-1; conn=DBOpenHelper.getConn(); String sql="insert into collect(user_id,note_id) values (?,?)"; try{ boolean closed=conn.isClosed(); if((conn!=null)&&(!closed)){ ps=conn.prepareStatement(sql); if(ps!=null){ ps.setInt(1,user_id); ps.setInt(2,note_id); result=ps.executeUpdate();//返回1 执行成功 } } } catch (SQLException e) { e.printStackTrace( ); } DBOpenHelper.closeAll(conn,ps); return result; } public List<Note> getCollectList(int user_id) { List<Note> list=new ArrayList<>(); String sql="SELECT collect.user_id,note.id note_id,note.title,note.date FROM collect JOIN note ON collect.note_id=note.id WHERE collect.user_id=?"; conn=DBOpenHelper.getConn(); try{ if(conn!=null&&(!conn.isClosed())){ ps=conn.prepareStatement(sql); if(ps!=null) { ps.setInt(1,user_id); rs=ps.executeQuery(); while (rs.next()) { Note note=new Note(); note.setId(rs.getInt("note_id")); note.setTitle(rs.getString("title")); note.setDate(rs.getString("date")); list.add(note); } } } }catch (SQLException e){ e.printStackTrace(); } DBOpenHelper.closeAll(conn,ps,rs); //关闭相关操作 return list; } public Note getDetailNote(int note_id) { Note note=new Note(); String sql="SELECT icon,writer_name,title,date,content from note where id=?"; conn=DBOpenHelper.getConn(); try{ if(conn!=null&&(!conn.isClosed())){ ps=conn.prepareStatement(sql); if(ps!=null) { ps.setInt(1,note_id); rs=ps.executeQuery(); if(rs.next()) { if(sab==null) { sab=new StringAndBitmap(); } note.setIcon(sab.stringToBitmap(rs.getString("icon"))); note.setWriter_name(rs.getString("writer_name")); note.setTitle(rs.getString("title")); note.setDate(rs.getString("date")); note.setContent(rs.getString("content")); } } } }catch (SQLException e){ e.printStackTrace(); } DBOpenHelper.closeAll(conn,ps,rs); //关闭相关操作 return note; } public int deleteCollect(int user_id,int note_id) { int result=-1; conn=DBOpenHelper.getConn(); String sql="delete from collect where user_id=? and note_id=?"; try{ boolean closed=conn.isClosed(); if((conn!=null)&&(!closed)){ ps=conn.prepareStatement(sql); if(ps!=null){ ps.setInt(1,user_id); ps.setInt(2,note_id); result=ps.executeUpdate();//返回1 执行成功 } } } catch (SQLException e) { e.printStackTrace( ); } DBOpenHelper.closeAll(conn,ps); return result; } }