⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.java

📁 JAVA做的学生管理信息系统,共同交流
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
} 
if (con != null) { 
try { 
con.close(); 
} catch (SQLException ex) { 
} 
} 
} 
return changed; 
} 

// 添加用户 
public boolean adduser(String username, String password) { 
boolean add = false; 
String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student"; 
Connection con = null; 
Statement stmt = null; 
String adduserStr = new String("insert into Users values('" + username 
+ "','" + password + "'" + ",'1')"); 
try { 
con = DriverManager.getConnection(url); 
stmt = con.createStatement(); 
// 添加用户 
stmt.executeUpdate(adduserStr); 
add = true; 

} catch (SQLException e) { 
System.out.println("添加用户" + e); 
} finally { 
if (stmt != null) { 
try { 
stmt.close(); 
} catch (SQLException ex) { 
} 
} 
if (con != null) { 
try { 
con.close(); 
} catch (SQLException ex) { 
} 
} 
} 
return add; 
} 

// 删除用户 
public boolean deluser(String username) { 
boolean del = false; 
String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student"; 
Connection con = null; 
Statement stmt = null; 
String deluserStr = new String("delete from Users where Username='" 
+ username + "'"); 
try { 
con = DriverManager.getConnection(url); 
stmt = con.createStatement(); 
// 删除用户 
stmt.executeUpdate(deluserStr); 
del = true; 

} catch (SQLException e) { 
System.out.println("删除用户" + e); 
} finally { 
if (stmt != null) { 
try { 
stmt.close(); 
} catch (SQLException ex) { 
} 
} 
if (con != null) { 
try { 
con.close(); 
} catch (SQLException ex) { 
} 
} 
} 
return del; 
} 

public boolean isExist(String username) { 
boolean isexist = false; 

 String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student"; 
String user = "sa"; 
String pw = ""; 
Connection con = null; 
Statement stmt = null; 
ResultSet rs = null; 
String isExistStr = new String("select * from users where Username='" 
+ username + "'"); 
try { 
con = DriverManager.getConnection(url, user, pw); 
stmt = con.createStatement(); 
// 查询所输入的用户名 
rs = stmt.executeQuery(isExistStr); 
if (rs.next()) { 
isexist = true; 
} 
} catch (SQLException e) { 
System.out.println("查询数据库错误" + e); 
} finally { 
if (rs != null) { 
try { 
rs.close(); 
} catch (SQLException ex) { 
} 
} 
if (stmt != null) { 
try { 
stmt.close(); 
} catch (SQLException ex) { 
} 
} 
if (con != null) { 
try { 
con.close(); 
} catch (SQLException ex) { 
} 
} 
} 
return isexist; 
} 
} 


//import java.awt.*; 
//import java.awt.event.*; 
//import javax.swing.*; 
//import javax.swing.table.*; 
//import java.util.*; 
//import java.sql.*; 

 class ShowAllInfo extends JFrame implements ActionListener { 
JPanel contentPane; 

JButton b_first, b_pre, b_next, b_last; 

JLabel label_1, label_2; 

Vector vector; 

JTextField text; 

String[] title = { "学号", "姓名", "性别", "出生日期", "所属班级", "家庭地址", "手机", "入学时间", 
"毕业时间", "文凭" }; 

Connection con = null; 

Statement stmt = null; 

ResultSet rs = null; 

AbstractTableModel tm; 

String sql; 

int pagenum = 0; 

int lastpagenum; 

int t; 

int everypagenumber = 10; 

public ShowAllInfo() { 
super("显示所有学生信息"); 
contentPane = (JPanel) this.getContentPane(); 
contentPane.setLayout(null); 
setSize(555, 417); 

b_first = new JButton("首页"); 
b_pre = new JButton("上页"); 
b_next = new JButton("下页"); 
b_last = new JButton("尾页"); 

label_1 = new JLabel("第"); 
label_2 = new JLabel("页"); 

text = new JTextField(); 

contentPane.add(b_first); 
b_first.setBounds(30, 297, 77, 39); 
contentPane.add(b_pre); 
b_pre.setBounds(135, 297, 77, 39); 
contentPane.add(b_next); 
b_next.setBounds(240, 297, 77, 39); 
contentPane.add(b_last); 
b_last.setBounds(345, 297, 77, 39); 
contentPane.add(label_1); 
label_1.setBounds(373, 349, 22, 24); 
contentPane.add(label_2); 
label_2.setBounds(428, 349, 22, 34); 
contentPane.add(text); 
text.setBounds(392, 352, 30, 19); 

b_first.addActionListener(this); 
b_pre.addActionListener(this); 
b_next.addActionListener(this); 
b_last.addActionListener(this); 

// 将窗口置于屏幕中央 
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
Dimension frameSize = this.getSize(); 
if (frameSize.height > screenSize.height) { 
frameSize.height = screenSize.height; 
} 
if (frameSize.width > screenSize.width) { 
frameSize.width = screenSize.width; 
} 
setLocation((screenSize.width - frameSize.width) / 2, 
(screenSize.height - frameSize.height) / 2); 
 
 setResizable(false); 
setVisible(true); 

createtable(); 
} 

void createtable() { 
JTable table; 
JScrollPane scroll; 
vector = new Vector(); 
tm = new AbstractTableModel() { 
public int getColumnCount() { 
return title.length; 
} 

public int getRowCount() { 
return vector.size(); 
} 

public Object getValueAt(int row, int column) { 
if (!vector.isEmpty()) { 
return ((Vector) vector.elementAt(row)).elementAt(column); 
} else { 
return null; 
} 
} 

public void setValueAt(Object value, int row, int column) { 
} 

public String getColumnName(int column) { 
return title[column]; 
} 

public Class getColumnClass(int c) { 
return getValueAt(0, c).getClass(); 
} 

public boolean isCellEditable(int row, int cloumn) { 
return false; 
} 
}; 
table = new JTable(); 
table.setToolTipText("显示查询结果"); 
table.setAutoResizeMode(table.AUTO_RESIZE_OFF); 
table.setCellSelectionEnabled(false); 
table.setShowHorizontalLines(true); 
table.setShowVerticalLines(true); 

scroll = new JScrollPane(table); 
contentPane.add(scroll); 
scroll.setBounds(6, 20, 540, 250); 
firstrun(); 
} 

public void actionPerformed(ActionEvent e) { 
if (e.getSource() == b_first) { 
firstrun(); 
} else if (e.getSource() == b_pre) { 
try { 
pagenum--; 
if (pagenum <= 0) { 
pagenum++; 
JOptionPane.showMessageDialog(null, "已经到达首页", "警告", 
JOptionPane.WARNING_MESSAGE); 
} 
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student"; 
con = DriverManager.getConnection(url); 
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
ResultSet.CONCUR_UPDATABLE); 
sql = "select * from StudentInfo"; 
rs = stmt.executeQuery(sql); 
String displaypage = pagenum + "/" + lastpagenum; 
text.setText(displaypage); 
vector.removeAllElements(); 
tm.fireTableStructureChanged(); 
for (int i = 1; i <= (pagenum - 1) * everypagenumber; i++) { 
rs.next(); 
} 
for (int i = pagenum * everypagenumber + 1; i <= (pagenum + 1) 
* everypagenumber; i++) { 
if (rs.next()) { 
Vector rec_vector = new Vector(); 
for (int j = 1; j <= title.length; j++) { 
rec_vector.addElement(rs.getString(j)); 
} 
vector.addElement(rec_vector); 
} 
} 
tm.fireTableStructureChanged(); 
} catch (SQLException ex) { 
System.out.println("上页错误" + ex); 
} catch (ClassNotFoundException ex) { 
System.out.println("数据库连接错误" + ex); 
} finally { 
try { 
if (rs != null) { 
rs.close(); 
} 
if (stmt != null) { 
stmt.close(); 
} 
if (con != null) { 
con.close(); 
} 
} catch (Exception ex) { 
} 
} 

} else if (e.getSource() == b_next) { 
try { 
pagenum++; 
if (pagenum > lastpagenum) { 
pagenum--; 
JOptionPane.showMessageDialog(null, "已经到达尾页", "警告", 
  
 JOptionPane.WARNING_MESSAGE); 
} 
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student"; 
con = DriverManager.getConnection(url); 
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
ResultSet.CONCUR_UPDATABLE); 
sql = "select * from StudentInfo"; 
rs = stmt.executeQuery(sql); 
String displaypage = pagenum + "/" + lastpagenum; 
text.setText(displaypage); 
vector.removeAllElements(); 
tm.fireTableStructureChanged(); 
for (int i = 1; i <= (pagenum - 1) * everypagenumber; i++) { 
rs.next(); 
} 
for (int i = (pagenum - 1) * everypagenumber + 1; i <= pagenum 
* everypagenumber; i++) { 
if (rs.next()) { 
Vector rec_vector = new Vector(); 
for (int j = 1; j <= title.length; j++) { 
rec_vector.addElement(rs.getString(j)); 
} 
vector.addElement(rec_vector); 
} 
} 
tm.fireTableStructureChanged(); 
} catch (SQLException ex) { 
System.out.println("下页错误" + ex); 
} catch (ClassNotFoundException ex) { 
System.out.println("数据库连接错误" + ex); 
} finally { 
try { 
if (rs != null) { 
rs.close(); 
} 
if (stmt != null) { 
stmt.close(); 
} 
if (con != null) { 
con.close(); 
} 
} catch (Exception ex) { 
} 
} 

} else if (e.getSource() == b_last) { 
try { 
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student"; 
con = DriverManager.getConnection(url); 
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
ResultSet.CONCUR_UPDATABLE); 
sql = "select * from StudentInfo"; 
int recordnum = 0; 
rs = stmt.executeQuery(sql); 
while (rs.next()) { 
recordnum++; 
} 
int t = recordnum % everypagenumber; 
if (t != 0) { 
lastpagenum = recordnum / everypagenumber + 1; 
} else { 
lastpagenum = recordnum / everypagenumber; 
} 
String displaypage = pagenum + "/" + lastpagenum; 
text.setText(displaypage); 
int displaynum = (recordnum % everypagenumber); 
if (displaynum == 0) { 
displaynum = everypagenumber; 
} 
vector.removeAllElements(); 
tm.fireTableStructureChanged(); 
for (int i = 0; i <= displaynum; i++) { 
rs.previous(); 
} 
for (int i = 1; i <= displaynum; i++) { 
if (rs.next()) { 
Vector rec_vector = new Vector(); 
for (int j = 1; j <= title.length; j++) { 
rec_vector.addElement(rs.getString(j)); 
} 
vector.addElement(rec_vector); 
} 
} 
tm.fireTableStructureChanged(); 
} catch (SQLException ex) { 
System.out.println("首页错误" + ex); 
} catch (ClassNotFoundException ex) { 
System.out.println("数据库连接错误" + ex); 
} finally { 
try { 
if (rs != null) { 
rs.close(); 
} 
if (stmt != null) { 
stmt.close(); 
 
 } 
if (con != null) { 
con.close(); 
} 
} catch (Exception ex) { 
} 
} 

} 
} 

void firstrun() { 
try { 
pagenum = 1; 
int recordnum = 0; 
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student"; 
con = DriverManager.getConnection(url); 
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
ResultSet.CONCUR_UPDATABLE); 
sql = "select * from StudentInfo"; 
rs = stmt.executeQuery(sql); 
while (rs.next()) { 
recordnum++; 
} 
int t = recordnum % everypagenumber; 
if (t != 0) { 
lastpagenum = recordnum / everypagenumber + 1; 
} else { 
lastpagenum = recordnum / everypagenumber; 
} 
rs.beforeFirst(); 
String displaypage = pagenum + "/" + lastpagenum; 
text.setText(displaypage); 
vector.removeAllElements(); 
tm.fireTableStructureChanged(); 
for (int i = 1; i <= everypagenumber; i++) { 
if (rs.next()) { 
Vector rec_vector = new Vector(); 
for (int j = 1; j <= title.length; j++) { 
rec_vector.addElement(rs.getString(j)); 
} 
vector.addElement(rec_vector); 
} 
} 
tm.fireTableStructureChanged(); 
} catch (SQLException ex) { 
System.out.println("首页错误" + ex); 
} catch (ClassNotFoundException ex) { 
System.out.println("数据库连接错误" + ex); 
} finally { 
try { 
if (rs != null) { 
rs.close(); 
} 
if (stmt != null) { 
stmt.close(); 
} 
if (con != null) { 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -