Posted on 2009-06-08 09:13
saobaolu 阅读(1229)
评论(1) 编辑 收藏 所属分类:
java基础与算法
http://www.blogjava.net/chenglu/archive/2009/06/08/Student_Manager.html
DB.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package opera.DB;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author soft
*/
public class DB {
private String sName;
private String Sno;
private String Sgrade;
private String Ssex;
private String QSql;
private Statement stm=null;
private ResultSet rs = null;
private int i =0;
private String ex;
private String usn;
private String pwd;
public Connection getCon(){
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stu","root","");
} catch (SQLException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return con;
}
/**
* @return the sName
*/
public String getSName() {
return sName;
}
/**
* @param sName the sName to set
*/
public void setSName(String sName) {
this.sName = sName;
}
/**
* @return the Sno
*/
public String getSno() {
return Sno;
}
/**
* @param Sno the Sno to set
*/
public void setSno(String Sno) {
this.Sno = Sno;
}
/**
* @return the Sgrade
*/
public String getSgrade() {
return Sgrade;
}
/**
* @param Sgrade the Sgrade to set
*/
public void setSgrade(String Sgrade) {
this.Sgrade = Sgrade;
}
/**
* @return the Ssex
*/
public String getSsex() {
return Ssex;
}
/**
* @param Ssex the Ssex to set
*/
public void setSsex(String Ssex) {
this.Ssex = Ssex;
}
public int DBinsert(int choice) throws SQLException{
//Connection con =null;
//Class.forName("com.mysql.jdbc.Driver");
//con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stu","root","");
stm = getCon().createStatement();
if(choice==1){
i = stm.executeUpdate("insert into stu values('"+getSno()+"','"+getSName()+"','"+getSsex()+"','"+getSgrade()+"')");
}
else if(choice==2){
i = stm.executeUpdate("insert into user values('"+getUsn()+"','"+getPwd()+"')");
}//关闭
getCon().close();
stm.close();
stm = null;
return i;
}
public void DBQuery(int check) throws SQLException{//throws Exception{
//ResultSet rs = null;
//Statement stm = null;
stm = getCon().createStatement();
rs = stm.executeQuery(getQSql());
//select * from stu where sno = getSno()getQSql()
if(check==1){
//获取
while(rs.next()){
setSno(rs.getString("sno"));
setSName(rs.getString("sName"));
setSsex(rs.getString("ssex"));
setSgrade(rs.getString("sGrade"));
}
/*关闭
getCon().close();
stm.close();
stm = null;
rs.close();
rs = null;
*/
}
else if(check==2){
while(rs.next()){
setPwd(rs.getString("pwd"));
setUsn(rs.getString("usn"));
}
}
}
public int DBUpdate(){
try {
//Statement stm = null;
stm = getCon().createStatement();
String sq = "update stu set sName = '"+getSName()+"',ssex = '"+getSsex()+"',sGrade = '"+getSgrade()+"'where sno ='"+getSno()+"'";
i = stm.executeUpdate(sq);
//UPDATE Store_Information SET Sales = 500 WHERE store_name = "Los Angeles" AND Date = "Jan-08-1999"
//关闭
getCon().close();
stm.close();
stm = null;
} catch (SQLException ex) {
Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
}
return i;
}
public int DBdelete(){
//Statement stm = null;
try {
getCon().createStatement();
String dsql="DELETE FROM stu WHERE sno = '"+getSno()+"'";
//DELETE FROM Store_Information WHERE store_name = "Los Angeles"
//娘啊,user里的表忘删除了……晕……2009.6.4 18:50 铭记!
stm.executeUpdate("delete from user where usn = '"+getSno()+"'");
i = stm.executeUpdate(dsql);
//关闭
getCon().close();
stm.close();
stm = null;
} catch (SQLException ex) {
ex.printStackTrace();
}
return i;
}
/**
* @return the QSql
*/
public String getQSql() {
return QSql;
}
/**
* @param QSql the QSql to set
*/
public void setQSql(String QSql) {
this.QSql = QSql;
}
/**
* @return the usn
*/
public String getUsn() {
return usn;
}
/**
* @param usn the usn to set
*/
public void setUsn(String usn) {
this.usn = usn;
}
/**
* @return the pwd
*/
public String getPwd() {
return pwd;
}
/**
* @param pwd the pwd to set
*/
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
完整NetBeans项目代码:
/Files/chenglu/Student_NetBeana_Src.zip
没有所谓的命运,只有不同的选择!