import java.sql.*;
import java.io.*;
import java.util.*;public class dbconnect {
public Connection conn = null;//数据库连接对象
public Statement stmt = null;//SQL语句对象
public ResultSet rs = null;//结果集对象
//数据库连接字符串
public dbconnect(String strCon)
{
try {
Class.forName(strCon);////数据库连接字符串
}
catch (ClassNotFoundException e) {
System.err.println("连接错误:"+e.getMessage());
}
}
public ResultSet executeSelect(String sql,String strCon,String id,String pwd)//调用这个方法
{
try {
conn=DriverManager.getConnection(strCon,id,pwd);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sql);//execute sql
}
catch (java.sql.SQLException e) {
System.err.println("查询失败"+e.getMessage());
}
return rs;
}
public int executeUpdate(String sql,String strCon,String id,String pwd) {
int rowscount=0;
try {
conn=DriverManager.getConnection(strCon,id,pwd);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rowscount = stmt.executeUpdate(sql);//execute sql
}
catch (java.sql.SQLException e) {
System.err.println("插入失败"+e.getMessage());
}
return rowscount;
}
public void destroy() {
try {
stmt.close();
conn.close();
}
catch(Exception e) {
System.err.println("DBOperate.destroy():"+e.getMessage());
}
}
}--------------------------------------------------------------------------
import java.io.*;
import java.util.*;public class dbconnect {
public Connection conn = null;//数据库连接对象
public Statement stmt = null;//SQL语句对象
public ResultSet rs = null;//结果集对象
//数据库连接字符串
public dbconnect(String strCon)
{
try {
Class.forName(strCon);////数据库连接字符串
}
catch (ClassNotFoundException e) {
System.err.println("连接错误:"+e.getMessage());
}
}
public ResultSet executeSelect(String sql,String strCon,String id,String pwd)//调用这个方法
{
try {
conn=DriverManager.getConnection(strCon,id,pwd);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sql);//execute sql
}
catch (java.sql.SQLException e) {
System.err.println("查询失败"+e.getMessage());
}
return rs;
}
public int executeUpdate(String sql,String strCon,String id,String pwd) {
int rowscount=0;
try {
conn=DriverManager.getConnection(strCon,id,pwd);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rowscount = stmt.executeUpdate(sql);//execute sql
}
catch (java.sql.SQLException e) {
System.err.println("插入失败"+e.getMessage());
}
return rowscount;
}
public void destroy() {
try {
stmt.close();
conn.close();
}
catch(Exception e) {
System.err.println("DBOperate.destroy():"+e.getMessage());
}
}
}--------------------------------------------------------------------------
解决方案 »
- 求一sql的正则
- 类型转换问题
- java怎样像C/C++那样接收一行数字?
- 招聘论坛斑竹
- 请教如何在多线程程序中使用命令行参数
- 请问有在linux下使用jdk的吗?
- 可以有其他两个类的实例同时调用一个java实例的两个方法吗?
- 请问如何在java中通过Runtime类运行批处理文件?
- 请问如何得到ResultSet的行数,并且让它显示在JOptionPane.showMessageDialog中?
- 我是新手,问一下那里有java 的电子书籍啊!还有那里有java的安装程序下载啊?还有那里论坛比较好啊?给高分
- 我用JDBC连接的ACCESS数据库,有一个first1字段,有2000个值,我想每次去5个,改怎么做?
- 请问两个基本的java问题
import java.util.*;
import java.sql.*;public class dbsetsql {
public String rsAr;
public String rsFile; public String rsSql = "";
public String rsAddAr = "";
public String rsAddFile = "";
public ResultSet dbRs;
String ConnStr="jdbc:odbc:hh";
String MyUser="danny";
String MyPassword="zw"; public dbsetsql(String ls_sql){
int i = 0;
int IsOver = 0;
String sql = " ";
dbconnect dbConn = new dbconnect("sun.jdbc.odbc.JdbcOdbcDriver");
dbRs = dbconnect.executeSelect(ls_sql,ConnStr,MyUser,MyPassword);//这行出错}
}
你如果像这样访问的话,executeSelect()方法必须设为static;要么,你就应该得到一个dbconnect的实例,然后调用,可以将错误行改为:
dbConn.executeSelect(,,,);
应该是你的手误吧!