一共有六个函数,四种功能,一个初始化以及一个关闭连接 .详细情况见 代码:

/**//*
* SQLOperation.java
*
* Created on 2006年11月25日, 上午10:00
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package homework;
import java.sql.*;
/** *//**
* @author Administrator
*/
public class SQLOperation ...{
private Connection con = null;
private Statement stmt = null;
private String url = "jdbc:mysql://localhost/test";
private String user = "root";
private String pwd = "0429";
/** *//** Creates a new instance of SQLOperation */
public SQLOperation() ...{
init();
}
/** *//** init */
private void init()...{
try ...{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
con = DriverManager.getConnection(url,user,pwd);
stmt = con.createStatement();
} catch (Exception e) ...{
// your installation of JDBC Driver Failed
e.printStackTrace();
}
}
/** *//**
* TODO 增加一条记录
* @param stuID 学生号
* @param stuName 学生名字
* @param stuTel 该学生电话号码
* @return void
*/
public void add(String stuID,String stuName,String stuTel)...{
String sql2 = "insert into student value('"
+stuID+"','"+stuName+"','"+stuTel+"');";
try...{
stmt.execute(sql2);
}catch(SQLException e)...{
e.printStackTrace();
}
}
/** *//**
* TODO 查询记录
* @param stuID 记录的学生号
* @return String 查询的结果
*/
public String search(String stuID)...{
String str = "学生信息:学生号:"+stuID+" 姓名:";
String sql1 = "select * from student where stuID='"+stuID+"';";
try...{
ResultSet rs = stmt.executeQuery(sql1); 
if(rs.next())...{
str = str+rs.getString("stuName")+" 手机号:"
+rs.getString("stuTel");
}else str = "该记录不存在!!!";
}catch(Exception e)...{
e.printStackTrace();
}
return str;
}
/** *//**
* TODO 修改记录
* @param stuID 记录的学生号
* @param stuTel 该学生电话号码
* @parame result 记录的成绩
* @return void
*/
public void modify(String stuID,String stuTel)...{
String sql = "update sturesult set stuTel="+stuTel
+" where stuID='"+stuID+"'";
try...{
stmt.executeUpdate(sql);
}catch(SQLException e)...{
e.printStackTrace();
}
}
/** *//**
* TODO 删除记录
* @param stuID 记录的学生号
* @return void
*/
public void delete(String stuID)...{
String sql1 = "delete from student "+" where stuID='"+stuID+"';";
try...{
stmt.executeUpdate(sql1);
}catch(SQLException e)...{
e.printStackTrace();
}
}
public void close()...{
try...{
if(con != null) con.close();
if(stmt != null) stmt.close();
}catch(SQLException e)...{
e.printStackTrace();
}
}
public static void main(String args[])throws SQLException...{
//add test here!!!!
}
}
