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

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;...
求一个简单又经典的Java与数据库例子,要有源代码哦!
//下面的是连接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("�?!"));
}
}2008-02-20
mengvlog 阅读 5 次 更新于 2025-07-18 09:17:07 我来答关注问题0
  • //下面的是连接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"...

  • 首先,你需要导入必要的包,包括随机数生成器和输入流。代码如下:import java.util.*;import java.io.*;然后定义一个名为CaiShu的类,并在其中编写主函数main方法。在main方法中,使用Random类生成一个1到100之间的随机数作为目标数字。Random a=new Random();int num=a.nextInt(100);System.out....

  •  吃鱼的鸽子 java语言实现求1+2+3……+1000的和,把和输出,计算每步结果中有多少个最后以8结尾的。

    for(int i=1; i

  •  湖北倍领科技 一个简单的java 题目(保留小数点两位)

    这是一个简单的Java题目,代码如下:while(x

  • 在Java中生成1或2的随机数,可以利用Random类。首先,我们需要导入Random类,这可以通过在代码的开头添加import语句实现,例如:import java.util.Random;。接着,创建一个Random对象,代码为:java.util.Random rd = new java.util.Random();。然后,我们使用Random对象的nextInt(2)方法来获取一个0到1...

檬味博客在线解答立即免费咨询

Java相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部