package com.zl.manager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.zl.DB.DBConnection;
import com.zl.bean.UserArticle;
import com.zl.bean.UserInfor;
public class UserManager {
private Connection con = null;
public Boolean checkLogin(String username, String password) {
Statement st = null;
ResultSet re = null;
try {
con = DBConnection.getInstance().getConnection();
st = con.createStatement();
String query = "select * from users where username = '" + username
+ "' and password = '" + password + "'";
re = st.executeQuery(query);
if (re.next())
return true;
else
return false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
re.close();
st.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
public Boolean insertUser(UserInfor user, String password)
throws SQLException {
System.out.print("Address:" + user.getAddress());
con = DBConnection.getInstance().getConnection();
String username = user.getUsername();
int sex = user.getSex();
int age = user.getAge();
String email = user.getEmail();
String school = user.getSchool();
String mobile = user.getMobile();
String company = user.getCompany();
String address = user.getAddress();
String introduction = user.getIntroduction();
String picture = user.getPicture();
PreparedStatement pre = con
.prepareStatement("insert into user_infor(username,sex,age,email,school,mobile,company,address,introduction,picture) values(?,?,?,?,?,?,?,?,?,?)");
pre.setString(1, username);
pre.setInt(2, sex);
pre.setInt(3, age);
pre.setString(4, email);
pre.setString(5, school);
pre.setString(6, mobile);
pre.setString(7, company);
pre.setString(8, address);
pre.setString(9, introduction);
pre.setString(10, picture);
int i = pre.executeUpdate();
PreparedStatement pre2 = con
.prepareStatement("insert into users(username,password) values(?,?)");
pre2.setString(1, username);
pre2.setString(2, password);
int j = pre2.executeUpdate();
if (i == 1 && j == 1) {
con.close();
pre.close();
pre2.close();
return true;
} else {
con.close();
pre.close();
pre2.close();
return false;
}
}
public Boolean publishArticle(UserArticle article) {
PreparedStatement pre = null;
int i;
try {
con = DBConnection.getInstance().getConnection();
pre = con
.prepareStatement("insert into user_article(username,time,content,title,classs) values(?,?,?,?,?)");
pre.setString(1, article.getUsername());
pre.setDate(2, article.getTime());
pre.setString(3, article.getContent());
pre.setString(4, article.getTitle());
pre.setString(5, article.getClasss());
i = pre.executeUpdate();
if (i == 1)
return true;
else
return false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
pre.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
{
}
return null;
}
public ArrayList getUserArticle(String sql) {
Statement st = null;
ResultSet re = null;
try {
con = DBConnection.getInstance().getConnection();
st = con.createStatement();
re = st.executeQuery(sql);
ArrayList array = new ArrayList();
while (re.next()) {
UserArticle article = new UserArticle();
article.setUsername(re.getString("username"));
article.setClasss(re.getString("classs"));
article.setTitle(re.getString("title"));
article.setTime(re.getDate("time"));
article.setContent(re.getString("content"));
article.setId(re.getString("id"));
array.add(article);
}
return array;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
re.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
public Boolean saveArticle(UserArticle article) {
PreparedStatement pre = null;
try {
con = DBConnection.getInstance().getConnection();
pre = con.prepareStatement("update user_article set content='"
+ article.getContent() + "',title='" + article.getTitle()
+ "',classs='" + article.getClasss() + "' where id ='"
+ article.getId() + "'");
int i = pre.executeUpdate();
if (i != 0)
return true;
else
return false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
pre.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
public Boolean deleteArticle(int a[])
{
PreparedStatement pre = null;
try {
int j=0;
con = DBConnection.getInstance().getConnection();
for(int i=0;i<a.length;i++)
{
pre = con.prepareStatement("delete from user_article where id = '" + a[i] +"'");
j = pre.executeUpdate();
}
if(j!=0)
return true;
else
return false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
try {
pre.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}
评论2
最新资源