package connect_sql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connects {
private String dbDriver = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost/new_db";
private Connection con = null;
public Connects(){
try {
Class.forName(dbDriver).newInstance();
try {
con = DriverManager.getConnection(url, "root", "123");
System.out.println("提示:数据库加载成功!");
} catch (SQLException e) {
e.printStackTrace();
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConnection(){
return con;
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int i = 0;
flag0 = 1;
UserDao user = new UserDao();
if(user.queryUser(jTextField1.getText().trim(), jPasswordField1
.getText().trim())) {
MainFrom fr = new MainFrom();
fr.setVisible(true);
this.dispose();
}
else
{
i++;
if(i <= 3){
JOptionPane.showMessageDialog(this, "用户名或密码请重新登录!");
jTextField1.setText("");
jPasswordField1.setText("");
}
else
this.dispose();
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("");
jPasswordField1.setText("");
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
View ft = new View();
ft.setVisible(true);
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Search fr = new Search();
fr.setVisible(true);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Add ft = new Add();
ft.setVisible(true);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
Delete ft = new Delete();
ft.setVisible(true);
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
Sort ft = new Sort();
ft.setVisible(true);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Object title[] = { "学号", "姓名", "性别", "手机号", "学院", "专业", "地址", "班级" };
String str = (String) jComboBox1_view.getSelectedItem();
String sql = "SELECT * FROM STUINFO";
ResultSet rs1 = choose.dataSelect(sql);
int i = 0;
try
{
while(rs1.next()) {
i++;
}
}
catch(SQLException e) {
e.printStackTrace();
}
Object data[][] = new Object[i + 1][8];
if (str.equals("全部信息")) {
sql = "SELECT * FROM STUINFO";
}
else
sql = "SELECT * FROM STUINFO WHERE 班级='"+ str + "'";
ResultSet rs = choose.dataSelect(sql);
int j = 0;
try
{
while(rs.next()) {
data[j][0] = rs.getString(1);
data[j][1] = rs.getString(2);
data[j][2] = rs.getString(3);
data[j][3] = rs.getString(4);
data[j][4] = rs.getString(5);
data[j][5] = rs.getString(6);
data[j][6] = rs.getString(7);
data[j][7] = rs.getString(8);
j++;
}
}
catch (SQLException e) {
e.printStackTrace();
}
JTable table = new JTable(data, title);
jScrollPane1.setViewportView(table);
jScrollPane1.revalidate();
}
private void jButton_Search1ActionPerformed(java.awt.event.ActionEvent evt)
{
Object title[] = { "学号", "姓名", "性别", "手机号", "学院", "专业", "地址", "班级" };
String str1 = (String) jComboBox_Search1.getSelectedItem();
String str2 = jTextField_Search1.getText();
String sql = null;
if(str1.equals("学号")) {
sql = "SELECT * FROM STUINFO WHERE 学号='" + str2 + "'";
}
else if(str1.equals("姓名")) {
sql = "SELECT * FROM STUINFO WHERE 姓名='" + str2 + "'";
}
else if(str1.equals("班级")) {
sql = "SELECT * FROM STUINFO WHERE 班级='" + str2 + "'";
}
ResultSet rs = choose.dataSelect(sql);
Object[][] datas = new Object[50][8];
try{
int j = 0;
while(rs.next()) {
datas[j][0] = rs.getString(1);
datas[j][1] = rs.getString(2);
datas[j][2] = rs.getString(3);
datas[j][3] = rs.getString(4);
datas[j][4] = rs.getString(5);
datas[j][5] = rs.getString(8);
datas[j][6] = rs.getString(6);
datas[j][7] = rs.getString(7);
j++;
}
}
catch (SQLException e1) {
e1.printStackTrace();
}
JTable table = new JTable(datas, title);
jScrollPane1.setViewportView(table);
jScrollPane1.revalidate();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String[] option = { "添加", "修改" };
int n = JOptionPane.showOptionDialog(this, "请仔细检查信息,确定添加?", "添加信息",
JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null,
option, option[1]);
if (n == 0) {
String acctnum = jTextField1.getText();
String name = jTextField2.getText();
String sex = jTextField3.getText();
String mobile = jTextField4.getText();
String academy = jTextField5.getText();
String major = jTextField6.getText();
String adress = jTextField7.getText();
String classes = jTextField8.getText();
String sql = "INSERT INTO STUINFO VALUES('" + acctnum + "', '"+ name + "', '"+ sex + "', '" + mobile + "', '"+ academy + "', '" + major + "', '" + adress + "', '" + classes + "')";
choose.dataInsert(sql);
JOptionPane.showMessageDialog(this, "添加成功!", "提示",
JOptionPane.INFORMATION_MESSAGE);
jButton2ActionPerformed(evt);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
jTextField7.setText("");
jTextField8.setText("");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String str = (String) jComboBox1_view.getSelectedItem();
String str2 = jTextField1.getText();
String sql = null;
if(str.equals("学号")) {
sql = "SELECT * FROM stuinfo where 学号='" + str2 + "'";
}
else
sql = "SELECT * FROM stuinfo where 姓名='" + str2 + "'";
ResultSet rs = choose.dataSelect(sql);
try
{
while (rs.next()) {
jTextField2_acctnum.setText(rs.getString(1));
jTextField9_name.setText(rs.getString(2));
jTextField3_sex.setText(rs.getString(3));
jTextField4_mobile.setText(rs.getString(4));
jTextField5_academy.setText(rs.getString(5));
jTextField6_major.setText(rs.getString(6));
jTextField7_address.setText(rs.getString(7));
jTextField8_class.setText(rs.getString(8));
}
}
catch (SQLException e) {
e.printStackTrace();
}
String[] option = { "删除", "修改" };
int n = JOptionPane.showOptionDialog(this, "请仔细核对信息,确认删除?", "删除信息",
JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null,
option, option[1]);
if (n == 0) {
if (str.equals("学号")) {
sql = "DELETE FROM stuinfo WHERE 学号='" + str2 + "'";
}
else
sql = "DELETE FROM stuinfo WHERE 姓名='" + str2 + "'";
choose.dataDelete(sql);
JOptionPane.showMessageDialog(this, "删除成功!", "提示",
JOptionPane.INFORMATION_MESSAGE);
jTextField2_acctnum.setText("");
jTextField9_name.setText("");
jTextField3_sex.setText("");
jTextField4_mobile.setText("");
jTextField5_academy.setText("");
jTextField6_major.setText("");
jTextField7_address.setText("");
jTextField8_class.setText("");
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String str = (String) jComboBox1.getSelectedItem();
Object title[] = { "姓名", "学号", "班级", "语文", "数学", "英语", "平均成绩", "总成绩"};
String sql = null;
if (str.equals("学号")) {
sql = "SELECT * FROM stuinfo ORDER BY 学号 ASC";
}
else if (str.equals("姓名")) {
sql = "SELECT * FROM stuinfo ORDER BY 姓名 ASC";
}
else
sql = "SELECT * FROM stuinfo ORDER BY 班级 ASC";
ResultSet rs = choose.dataSelect(sql);
Object[][] datas = new Object[51][8];
try
{
int j = 0;
while (rs.next()) {
datas[j][0] = rs.getString(1);
datas[j][1] = rs.getString(2);
datas[j][2] = rs.getString(3);
datas[j][3] = rs.getString(4);
datas[j][4] = rs.getString(5);
datas[j][5] = rs.getString(6);
datas[j][6] = rs.getString(7);
datas[j][7] = rs.getString(8);
j++;
}
}
catch (SQLException e1) {
e1.printStackTrace();
}
JTable table = new JTable(datas, title);
jScrollPane1.setViewportView(table);
jScrollPane1.revalidate();
}
//8.图片插入:
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class LoginPanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
protected ImageIcon icon;
private int width;
private int height;
public LoginPanel(){
super();
icon = new ImageIcon("res/LoginImage1.jpg");
width = icon.getIconWidth();
height = icon.getIconHeight();
setSize(width, height);
}
protected void paintComponent(Graphics g){
super.paintComponents(g);
Image img = icon.getImage();
g.drawImage(img, 0, 0, this);
}
}
//9.音乐插入:
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
File musicFile = new File("res/1.wav");
URI uri = musicFile.toURI();
URL url;
try
{
url = uri.toURL();
AudioClip clip = Applet.newAudioClip(url);
clip.loop();
}
catch (MalformedURLException e) {
e.printStackTrace();
}
}
|