求一个简单又经典的Java与数据库例子,要有源代码哦!

能用applet小程序或frame最好,建立数据库,创建表,进行简单的查询,删除,修改即可,这些事件是点击按钮时发生的!
如果很经典的话,将高分相送哥哥姐姐!
谢谢!

//下面的是连接mysql的例子
package com.song.struts.mySql;

import javax.swing.JComponent;
import java.sql.*;
import java.util.*;
// import com.borland.dx.sql.dataset.*;

public class mySqlDao extends JComponent {
private String UserName="root";
private String PWD="root";
private String url;
private Connection cn;
private Statement stmt;
private ResultSet rs = null;
public mySqlDao(){
try {
Class.forName("org.gjt.mm.mysql.Driver");
}
catch(java.lang.ClassNotFoundException e){
System.err.println("mydb() org.gjt.mm.mysql.Driver: " + e.getMessage());
}
catch(Exception e) {
e.printStackTrace();
}

}
//////////////////////////////
///返回mysql 连接,connection
/////////////////////////////
public Connection Connect(String dbname,String ip){
try{
String hostip=ip;
Properties myP = new Properties();
myP.setProperty("useUnicode","true");
myP.setProperty("characterEncoding","GB2312");
url="jdbc:mysql://"+hostip+":3306/"+dbname+"?user="+UserName+"&password="+PWD+"";
if(cn!=null){
cn.close();
}
cn=DriverManager.getConnection(url,myP);
stmt= cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
System.out.println("db connect success");
return cn;
}
catch(Exception e){
System.err.println("db connect err"+e.getMessage());
return null;
}
}
//////////////////////////////////
///关闭连接
/////////////////////////////////
public void close(){
try{
if(stmt!=null){
stmt.close();
}
if(cn!=null){
cn.close();
}
System.err.println("db colse success");
}
catch(Exception e){
System.err.println("db close err"+e.getMessage());
}
}
/////////////////////////////////////////////
// 用于进行记录的查询操�?,用于select 语句�?
//参数:sql语句�?
//返回:ResultSet对象
///////////////////////////////////////////
public ResultSet executeSelect(String sql) {
try {
stmt=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sql);
return rs;
}
catch(SQLException ex) {
System.err.println("db.executeQuery: " + ex.getMessage());
return null;
}
}
//////////////////////////////////////////////
//用于进行add或�?�update,insert,del等的记录的操�?,
//入口参数:sql语句
//返回 :true,false
//////////////////////////////////////////////
public boolean executeUpdate(String sql) {
boolean bupdate=false;
try{
stmt=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
int rowCount = stmt.executeUpdate(sql);
if (rowCount!=0)
bupdate=true;
}
catch(SQLException ex) {
System.err.println("db.executeUpdate: " + ex.getMessage());
}
return bupdate;
}
//////////////////////////////////////////////
//用于进行表结构的操作,creat drop,modify等�??
//入口参数:sql语句
//返回 :true,false
//////////////////////////////////////////////
public boolean executeTable(String sql) {
boolean bupdate=false;
try {
stmt= cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
System.out.print("对表的操作的sqlis :||"+sql+"||");
stmt.executeUpdate(sql);
bupdate=true;
}
catch(SQLException ex) {
System.err.println("db.executeTable: "+ex.getMessage());
}
return bupdate;
}
//////////////////////////
//返回数据库的信息
//////////////////////////
public Statement getLWPAIDStatement(){
try{
return cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
catch(java.sql.SQLException e){
System.err.println("getAISPStatement():"+e.getMessage());
return null;
}
}

public DatabaseMetaData getLWPAIDMetaData(){
try{
return cn.getMetaData();
}
catch(java.sql.SQLException e){
System.err.println("getAISPMetaData():"+e.getMessage());
return null;
}
}
public static void main(String args[]){
mySqlDao a=new mySqlDao();
a.Connect("mydb", "localhost");
int b=-100;
ResultSet rs=a.executeSelect("select max(bill_id) from t_bill limit 1");
try{
while(rs.next()){
System.out.println("is in");
b=rs.getInt(1);
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println(b);
// java.util.Date date=new java.util.Date();
// System.out.println(date.toString());
// a.executeTable("insert into t_user values(100,'123','1345')");
// a.executeTable("update t_user set insert_date='"+date.toString()+"' where user_id=100");
a.close();

System.out.print(new pub().asc2unicode("�?!"));
}
}
温馨提示:答案为网友推荐,仅供参考
相似回答