package Basic1;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.swing.JTextField;
public class MySQLConnection {
Connection connection = null;
String url = "jdbc:oracle:thin:@localhost:1521:XE";
String DRIVER = "oracle.jdbc.driver.OracleDriver";
String userName = "system";
String password = "system";
ResultSet result = null;
Statement stmt = null;
String sql = "";
PreparedStatement preparedStatement = null;
public MySQLConnection() {
try {
Class.forName(DRIVER);
System.out.println("加载驱动成功");
connection = DriverManager.getConnection(url, userName, password);
System.out.println("创建连接成功");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("创建连接失敗");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("加载驱动失败");
}
}
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDRIVER() {
return DRIVER;
}
public void setDRIVER(String dRIVER) {
DRIVER = dRIVER;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public ResultSet getResult() {
return result;
}
public void setResult(ResultSet result) {
this.result = result;
}
public Statement getStmt() {
return stmt;
}
public void setStmt(Statement stmt) {
this.stmt = stmt;
}
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public boolean Valify(String sql, String user, String pwd) {
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user);
preparedStatement.setString(2, pwd);
result = preparedStatement.executeQuery();
if (result.next()) {
preparedStatement.close();
result.close();
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return false;
}
public boolean insert(String sql, String s1, String s2) {
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, s1);
preparedStatement.setString(2, s2);
preparedStatement.executeUpdate();
System.out.println("注册成功");
preparedStatement.close();
return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public void insert(String sql, String s1, String s2, String s3) {
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, s1);
preparedStatement.setString(2, s2);
preparedStatement.setString(3, s3);
preparedStatement.executeUpdate();
System.out.println("报修成功");
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insert(String sql, String s1, String s2, String s3, String s4) {
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, s1);
preparedStatement.setString(2, s2);
preparedStatement.setString(3, s3);
preparedStatement.setString(4, s4);
preparedStatement.executeUpdate();
System.out.println("提交成功");
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insert(String sql, String s1, String s2, String s3, String s4,
String s5) {
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, s1);
preparedStatement.setString(2, s2);
preparedStatement.setString(3, s3);
preparedStatement.setString(4, s4);
preparedStatement.setString(5, s5);
preparedStatement.executeUpdate();
System.out.println("提交成功");
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insert(String sql, String s1) {
try {
preparedStatement = connection.prepareStatement(sql);
System.out.println(s1);
preparedStatement.setString(1, s1);
preparedStatement.executeUpdate();
System.out.println("提交成功");
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public String[] searchMaintain(String sql, int n) {
String[] results = new String[1];
try {
int j = 0;
String s = "";
String temp = "";
preparedStatement = connection.prepareStatement(sql);
result = preparedStatement.executeQuery();
while (result.next()) {
if (j != 0) {
results = Arrays.copyOf(results, results.length + 1);
}
for (int i = 1; i <= n; i++) {
s = result.getString(i);
temp += s + " ";
}
results[j] = temp;
temp = "";
j++;
}
preparedStatement.close();
result.close();
return results;
} catch (SQLException e) {
e.printStackTrace();
}
return results;
}
public boolean modifyPassword(String sql, String user, String pwd) {
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user);
preparedStatement.setString(2, pwd);
result = preparedStatement.executeQuery();
while (result.next()) {
return true;
}
preparedStatement.close();
result.close();
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public boolean updatePassword(String sql, String pwd, String a_id) {
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, pwd);
preparedStatement.setString(2, a_id);
int c = preparedStatement.executeUpdate();
if (c != 0) {
return true;
}
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public boolean insert(String sql, JTextField[] fields) {
try {
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < fields.length; i++) {
preparedStatement.setString(i + 1, fields[i].getText().trim());
}
int c = preparedStatement.executeUpdate();
if (c != 0) {
return true;
}
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public void nightOut(Map<String, Date> out) {
List<Map.Entry<String, Date>> list = new ArrayList<Map.Entry<String, Date>>(
out.entrySet());
Iterator<Map.Entry<String, Date>> iterator = list.iterator();
try {
while (iterator.hasNext()) {
Map.Entry<String, Date> stu = iterator.next();
String s = stu.getKey();
preparedStatement = connection
.prepareStatement("select dorm_number from students where username=?");
preparedStatement.setString(1, s);
result = preparedStatement.executeQuery();
while (result.next()) {
String dorm = result.getString(1);
preparedStatement = connection
.prepareStatement("insert into nightout values(?,?,?)");
preparedStatement.setString(1, userName)