云相册服务器dao:
package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.List; public class dao { public Connection getConnection() { try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } String url = "jdbc:mysql://localhost:3306/groupdb?serverTimezone=GMT%2B8"; String username = "root"; String password="123456"; Connection a=null; try { a = DriverManager.getConnection(url,username,password); } catch (SQLException e) { e.printStackTrace(); } return a; } public boolean login(String id,String passwd) { Connection coon=getConnection(); String sql="select* from user where id = '"+id+"'";//table name Statement stmt; try { stmt = coon.createStatement(); ResultSet rs=stmt.executeQuery(sql); while(rs.next()) { if(passwd.equals(rs.getString("passwd"))) {rs.close();stmt.close();coon.close();return true;} } rs.close();stmt.close();coon.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return false; } public int signin(String id,String passwd) { if(checkuser(id)==1) { Connection coon=getConnection(); String sql; sql="insert into user(id,passwd)values(?,?)";//matters' name in table PreparedStatement b; try { b = coon.prepareStatement(sql); b.setString(1,id); b.setString(2,passwd); b.executeUpdate(); b.close(); coon.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return 1; }else return 2; } public int checkuser(String id) { Connection coon=getConnection(); String sql="select* from user"; try { PreparedStatement pa=coon.prepareStatement(sql); ResultSet rs=pa.executeQuery(sql); while(rs.next()) { if(rs.getString("id").equals(id)) return 0; } rs.close(); pa.cancel(); coon.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return 1; } public void addphoto(String id,String photo,String time) { Connection coon=getConnection(); String sql; sql="insert into photo(id,photoid,time)values(?,?,?)";//matters' name in table PreparedStatement b; try { b = coon.prepareStatement(sql); b.setString(1,id); b.setString(2,photo); b.setString(3,time); b.executeUpdate(); b.close(); coon.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } public StringBuffer getphotolist(String id) { String time=""; StringBuffer a=new StringBuffer(20000); Connection coon=getConnection(); String sql="select* from photo where id = '"+id+"'"; try { PreparedStatement pa=coon.prepareStatement(sql); ResultSet rs=pa.executeQuery(sql); while(rs.next()) { if(!(rs.getString("time").toString().equals(time))) { a.append("$"); time=rs.getString("time").toString(); } a.append(rs.getString("photoid").toString()+"+"); } rs.close(); pa.cancel(); coon.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return a; } public StringBuffer gettimelist(String id) { StringBuffer a=new StringBuffer(20000); String time=""; Connection coon=getConnection(); String sql="select* from photo where id = '"+id+"'"; try { PreparedStatement pa=coon.prepareStatement(sql); ResultSet rs=pa.executeQuery(sql); while(rs.next()) { if(!(rs.getString("time").toString().equals(time))) { time=rs.getString("time").toString(); a.append(time+"+"); } } rs.close(); pa.cancel(); coon.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return a; } public int photocount(String id,String time) { int a=0; Connection coon=getConnection(); String sql="select* from photo where id = '"+id+"' and time = '"+time+"'"; try { PreparedStatement pa=coon.prepareStatement(sql); ResultSet rs=pa.executeQuery(sql); while(rs.next()) { a++; } rs.close(); pa.cancel(); coon.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return a; } public String delete(String id) { Connection coon=getConnection(); String sql="delete from photo where photoid = '"+id+"'"; PreparedStatement a1; try { a1 = coon.prepareStatement(sql); a1.execute(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return "删除成功"; } public void addcomment(String id,String comment,String time) { Connection coon=getConnection(); String sql; sql="insert into comment(id,comment,time)values(?,?,?)";//matters' name in table PreparedStatement b; try { b = coon.prepareStatement(sql); b.setString(1,id); b.setString(2,comment); b.setString(3,time); b.executeUpdate(); b.close(); coon.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } }