首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 冲浪宝典 > 网络资源 > 连接MySQL并提供查询,删除,修改和添加的java程序
【标  题】:连接MySQL并提供查询,删除,修改和添加的java程序
【关键字】:MySQL,java
【来  源】:http://blog.csdn.net/lilijun4143/archive/2006/11/25/1413677.aspx

连接MySQL并提供查询,删除,修改和添加的java程序

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

 

/*
 * 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!!!!
    }

}


一个完成阳历到阴历的转换函数:【上一篇】
自己測試sql功能一:【下一篇】
【相关文章】
  • JAVAScript 使用函数整理 (1)
  • How hibernate benifit the Java Programming?
  • The 10 Things You Need to Know About Java SE 6 'Mustang'
  • JAVA中对象创建和初始化过程
  • JAVA中处理字符串
  • java运行速度比C++慢的几点原因
  • JAVA中浅复制与深复制
  • 一个Java Enum的C++实现
  • JavaScript之内存释放(推荐)【摘自Oscar's 牛牛】
  • JAVA与C++的一点比较
  • 【随机文章】
  • oracle中 三个分析函数的区分
  • 图片 随意拖动(java)
  • 采用XML数据来填充ASP表单
  • CSDN 质量圈已经诞生了
  • 网页之定时器详解
  • 参考_PL/SQL基础
  • 20051010调试setup
  • 3DS Max 制作茶杯(旋转+多边形建模)(3)
  • 英语阅读推荐:海明威写作技巧 & UpdatePanel为何失灵
  • 下载baidu等流氓软件/插件的SmartNet.exe
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.