//处理数据库的JavaBean
package mvc_Exam ; import java .sql.Connection ;
import java .sql.Statement ;
import java .sql.ResultSet ;
import java .sql.DriverManager;
import java .sql.SQLException ;
import java .util.Vector ;
import java .io .* ;/**
* 包名
* 该类是对数据库操作处理
* @author
* Data
*/public class MysqlBean
{
/**
* @operateOption 1 : insert
* @ 2 : update
* @ 3 : select
* @ 4 : delete
* @fieldSum 字段总数
* @StrSQL SQL语句
* @return Vector
*/
public static Vector mysqlOperate(String operateOption,String StrSQL,int fieldSum)
{
Vector vVector =new Vector ();
Connection Conn = null;
Statement Stmt = null;
ResultSet Rs = null;
String UserName = "admin";
String Password = "system";
String DriverName = "com.mysql.jdbc.Driver";
String SourceUrl = "jdbc:mysql://localhost:3306/Examsys?";
//String[] ArrValue=new String [fieldSum]; int i;
try
{
Class.forName(DriverName).newInstance();
Conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/Examsys?user=admin&password=system&useUnicode=true&characterEncoding=gb2312");
Stmt=Conn.createStatement();
//insert
if(operateOption.equals ("1"))
{
Stmt.executeUpdate (StrSQL);
}
//update
if(operateOption.equals ("2"))
{
Stmt.executeUpdate (StrSQL);
}
//select
if(operateOption.equals ("3"))
{
Rs = Stmt.executeQuery(StrSQL);
while (Rs.next ())
{
Vector ArrValue = new Vector ();
for(i=0;i<fieldSum;i++)
{
//ArrValue[i] = Rs.getString (i+1);
ArrValue.addElement( Rs.getString(i+1));
}
//vVector.addElement ((String[])ArrValue.clone());
vVector.addElement(ArrValue);
}
}
//delete
if(operateOption.equals("4"))
{
Stmt.executeUpdate(StrSQL);
}
} catch (Exception ex)
{
ex.printStackTrace ();
}
finally
{
try
{
if (Rs !=null)
{
Rs=null;
}
if(Stmt !=null)
{
Stmt.close ();
Stmt=null;
}
if(Conn!=null)
{
Conn.close ();
Conn=null;
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
return vVector ;
}
}
package mvc_Exam ; import java .sql.Connection ;
import java .sql.Statement ;
import java .sql.ResultSet ;
import java .sql.DriverManager;
import java .sql.SQLException ;
import java .util.Vector ;
import java .io .* ;/**
* 包名
* 该类是对数据库操作处理
* @author
* Data
*/public class MysqlBean
{
/**
* @operateOption 1 : insert
* @ 2 : update
* @ 3 : select
* @ 4 : delete
* @fieldSum 字段总数
* @StrSQL SQL语句
* @return Vector
*/
public static Vector mysqlOperate(String operateOption,String StrSQL,int fieldSum)
{
Vector vVector =new Vector ();
Connection Conn = null;
Statement Stmt = null;
ResultSet Rs = null;
String UserName = "admin";
String Password = "system";
String DriverName = "com.mysql.jdbc.Driver";
String SourceUrl = "jdbc:mysql://localhost:3306/Examsys?";
//String[] ArrValue=new String [fieldSum]; int i;
try
{
Class.forName(DriverName).newInstance();
Conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/Examsys?user=admin&password=system&useUnicode=true&characterEncoding=gb2312");
Stmt=Conn.createStatement();
//insert
if(operateOption.equals ("1"))
{
Stmt.executeUpdate (StrSQL);
}
//update
if(operateOption.equals ("2"))
{
Stmt.executeUpdate (StrSQL);
}
//select
if(operateOption.equals ("3"))
{
Rs = Stmt.executeQuery(StrSQL);
while (Rs.next ())
{
Vector ArrValue = new Vector ();
for(i=0;i<fieldSum;i++)
{
//ArrValue[i] = Rs.getString (i+1);
ArrValue.addElement( Rs.getString(i+1));
}
//vVector.addElement ((String[])ArrValue.clone());
vVector.addElement(ArrValue);
}
}
//delete
if(operateOption.equals("4"))
{
Stmt.executeUpdate(StrSQL);
}
} catch (Exception ex)
{
ex.printStackTrace ();
}
finally
{
try
{
if (Rs !=null)
{
Rs=null;
}
if(Stmt !=null)
{
Stmt.close ();
Stmt=null;
}
if(Conn!=null)
{
Conn.close ();
Conn=null;
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
return vVector ;
}
}
解决方案 »
- RationalRose与powerdesign有什么不同?
- 请教高手在jQuery中如何出去字符串头尾字符(比如空格或者逗号)
- tomcat 6.0的admin怎么安装?
- java应用服务器的优劣
- 为什么我在spring配置文件中配置这个就报404 找不到 action
- 请高手解决将浏览器窗口强制执行在1024*768像素的问题
- 请问 用 java 如何 设置 日志中的 referer和useragen 值
- 复杂数据结构的操作!
- 请问,有什么原因会让tomcat服务器停止
- servlet怎么执行?
- jsp 格式化数字
- 急急急!!oracle中char类型的数据用什么api取出来?我用rs.getString()出错!
import java.sql.*;
import java.io.*;
import java.lang.*;
import java.util.Vector;
import mvc_Exam.*;public class mysql
{
public static void main(String[] as)
{
MysqlBean mysqlw =new MysqlBean();
Vector vE = new Vector();
String commnad = "select teach_id,name from teach_information where (teach_id='h'and name='h')";
vE=mysqlw.mysqlOperate("3",commnad,2);
System.out.println("vE.size()="+vE.size());
System.out.println("command="+commnad);
}
}
import java.lang.*;
import mvc_Exam.*;
import java.util.Vector;
import java .util.Vector ;
import java .io .* ;
import java .util.*;
import java .util.Locale;
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.DateFormat;
import java.net.*;
import java.sql.*;public class MVC_Test extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException
{
String outputFileStr="";
resp.setContentType("text/html;charset=gb2312");
HttpSession session = req.getSession(true);
java.io.PrintWriter out = resp.getWriter();
MysqlBean mysqlw =new MysqlBean();
Vector vE = new Vector();
String commnad = "select teach_id,name from teach_information where (teach_id='h'and name='h')";
vE=mysqlw.mysqlOperate("3",commnad,2);
out.println("vE.size()="+vE.size());
out.println("command="+commnad); }
}