smisdb.java
来自「用java2核心类库写的一个学生课程管理系统」· Java 代码 · 共 256 行
JAVA
256 行
import java.sql.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class SMISDB
{
private Connection conn;
private Statement stat;
private ResultSet res;
private ResultSetMetaData rsmeta;
private JTable table;
private String querystr;
private Container container;
private Vector columnheads = new Vector();
private Vector rows = new Vector();
public SMISDB()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//加载驱动程序
}
catch (ClassNotFoundException ex)
{
//in fact, not find exception here.
}
try
{
//conn = DriverManager.getConnection (url, userName, password);
conn = DriverManager.getConnection ("jdbc:odbc:SMIS_SOURCE","","");//建立连接,SMIS_SOURCE为数据源
stat = conn.createStatement();//创建Statement对象
}
catch (SQLException ex)
{
//sql exception
System.out.println("sql exception: connect or driver!!!!");
}
}
public int getTable(String str,Container cont, boolean isShowTable)
{
container = cont;
try
{
this.querystr = str;
if (querystr.startsWith("select") || querystr.startsWith("SELECT"))
{
System.out.println("-----------select------------------- ");
this.res = stat.executeQuery(querystr);
System.out.println("----------executeQuery ok------------------------------ ");
if ( this.getResultSet(res) >= 0)
{
if (isShowTable)
{
// add table to container, and show table in container
table = new JTable(rows,columnheads);
JScrollPane scroller = new JScrollPane(table);
container.add(scroller,BorderLayout.CENTER);
container.validate();
cont.setSize(500,600);
cont.setVisible(true);
table.setEnabled(false);
}
else
{
//table.setEnabled(true);
System.out.println("-----------setEnabled(true)--------------- ");
}
return 1;
// table.setEditingRow(false);
// table.setEditingColumn(true);
/*if (isShowTable)
{
table.setEnabled(true);
System.out.println("-----------setEnabled(true)--------------- ");
//updateTable();
}
else
{
table.setEnabled(false);
}
*/
}
else
{
JOptionPane.showMessageDialog(cont,"没有找到记录!");
cont.setVisible(false);
return -1;
}
}
else
{
System.out.println("-----------not select------------------- ");
int updateCount = stat.executeUpdate(querystr);
if (updateCount >0)
{
JOptionPane.showMessageDialog(cont,"sql success: modify one or more record ");
}
else
{
JOptionPane.showMessageDialog(cont,"没有输入信息,请重输!");
}
conn.close();
return updateCount;
}
//System.out.println("-------6666666666666--------------------------------- ");
}
catch (SQLException ex)
{
System.out.println("sql exception: sql statment");
cont.setVisible(false);
//JOptionPane.showMessageDialog(cont,"输入有误,请重输!");
JOptionPane.showMessageDialog(cont,ex.getMessage());
}
return 0;
}
public int getResultSet(ResultSet rs) throws SQLException
{
boolean moreRecord = rs.next();
if (!moreRecord)
{
return -1;
}
//Vector columnheads = new Vector();
//Vector rows = new Vector();
try
{
ResultSetMetaData rsmd = rs.getMetaData();
for (int i =1;i<=rsmd.getColumnCount();i++)
columnheads.addElement(rsmd.getColumnName(i));
do
{
rows.addElement(getNextRow(rs,rsmd));
} while (rs.next());
/*
table = new JTable(rows,columnheads);
JScrollPane scroller = new JScrollPane(table);
container.add(scroller,BorderLayout.CENTER);
container.validate();
*/
return 0;
}
catch(SQLException ex)
{
System.out.println("sql exception: getResultSet function");
return -2;
}
}
public Vector getNextRow(ResultSet rs, ResultSetMetaData rsmd) throws SQLException
{
Vector currentRow = new Vector();
for (int i =1; i<=rsmd.getColumnCount();i++)
currentRow.addElement(rs.getString(i));
return currentRow;
}
public ResultSet getRow(String strx) throws SQLException
{
this.res = this.stat.executeQuery(strx);
return this.res;
}
public Vector getVectorData()
{
return this.rows;
}
public int login(String sql, String user, String password)
{
System.out.println("SMISDB.login-----begin");
String usertemp = null;
String passwordtemp = null;
querystr = sql;
try
{
res = stat.executeQuery(querystr);
boolean moreRecord = res.next();
if (!moreRecord) // no record
{
return 0;
}
else
{
do
{
usertemp = res.getString("用户名");
passwordtemp = res.getString("密码");
if ((usertemp.equals(user)) && (passwordtemp.equals(password) ))
{
//user and password is correct
return 1;
}
}while(res.next());
return -1;
}
}
catch(SQLException ex)
{
System.out.println("sql exception: login function");
ex.getMessage();
return -2;
}
}
}
/*
class UpdateFrame extends JFrame
{
public UpdateFrame(String tableName)
{
if (tableName == "学生信息")
{
}
else if (tableName == "课程信息")
{
}
else if (tableName == "学生成绩")
{
}
else if (tableName == "用户信息")
{
}
else // tableName error
{
System.out.println("tableName error!");
}
}
}
*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?