package com.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import com.bean.Message;
public class DBManager {
public static Connection conn;
public static Statement stmt;
// 注册
public static int addUser(String userName, String password) {
String sql = "insert into Users values('" + userName + "','" + password
+ "')";
int result = executeNonQuery(sql);
return result;
}
// 登录
public static boolean canLogin(String userName, String password) {
String sql = "select * from Users where userName='" + userName
+ "' and password='" + password + "'";
ResultSet rs = executeQuery(sql);
try {
while (rs.next()) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
// 查询所有留言
public static ArrayList<Message> queryAllMessages(String username)
throws Exception {
ArrayList<Message> messages = new ArrayList<Message>();
String sql = "select * from message where userName='" + username + "'";
ResultSet rs = executeQuery(sql);
while (rs.next()) {
Message message = new Message();
message.setId(rs.getString("id"));
message.setTitle(rs.getString("title"));
message.setContent(rs.getString("content"));
message.setDate(rs.getString("date"));
message.setUserName(rs.getString("userName"));
messages.add(message);
}
Close();
return messages;
}
// 查询编号所对应的留言
public static Message queryMessageInfoById(String id) throws Exception {
String sql = "select * from message where id ='" + id + "'";
ResultSet rs = executeQuery(sql);
Message message = new Message();
while (rs.next()) {
message.setId(rs.getString("id"));
message.setTitle(rs.getString("title"));
message.setContent(rs.getString("content"));
message.setDate(rs.getString("date"));
message.setUserName(rs.getString("userName"));
}
Close();
return message;
}
// 修改一条留言
public static void alterMessage(String content, String date, String id) throws SQLException {
String sql = "update message set content='" + content + "',date='"
+ date + "' where id='" + id + "'";
int result = executeNonQuery(sql);
}
// 添加一条留言
public static void addMessage(String tilte, String content, String date, String username) throws SQLException {
String sql = "insert into message values('" + tilte
+ "','" + content + "','" + date
+ "','" + username + "')";
int result = executeNonQuery(sql);
}
// 删除一条留言
public static int deleteMessage(String id) throws SQLException {
String sql = "delete from message where id='" + id + "'";
int result = executeNonQuery(sql);
return result;
}
public static void getcon() {
String url = "jdbc:odbc:MessageBoard";
String user = "sa";
String pwd = "123";
try { // 加载驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// 创建连接
conn = DriverManager.getConnection(url, user, pwd);
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "加载驱动程序出错!", "警告",
JOptionPane.WARNING_MESSAGE);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "警告",
JOptionPane.WARNING_MESSAGE);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "不能建立连接!", "警告",
JOptionPane.WARNING_MESSAGE);
}
}
public static int executeNonQuery(String SQL) {
try {
getcon();
stmt = conn.createStatement();
return stmt.executeUpdate(SQL);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "数据库操作失败!", "警告",
JOptionPane.WARNING_MESSAGE);
}
return 0;
}
public static ResultSet executeQuery(String SQL) {
try {
getcon();
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(SQL);
return rs;
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "查询失败!", "警告",
JOptionPane.WARNING_MESSAGE);
}
return null;
}
public static void Close() {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "不能正常关闭!", "警告",
JOptionPane.WARNING_MESSAGE);
}
}
}
- 1
- 2
前往页