DBConn.java
资源名称:0102010.rar [点击查看]
上传用户:nbluoke
上传日期:2013-08-09
资源大小:4851k
文件大小:6k
源码类别:
教育系统应用
开发平台:
WORD
- //DBConn.java
- //include required classes
- import java.sql.*;
- //==========================================
- // Define Class DBConn
- //==========================================
- public class DBConn
- {
- public String sql_driver = "org.gjt.mm.mysql.Driver";
- public String sql_url = "jdbc:mysql://localhost:3306";
- public String sql_DBName = "jinghua";
- public String user = "sa";
- public String Passwd = "";
- Connection conn = null;
- Statement stmt = null;
- ResultSet rs = null;
- public boolean setDriver(String drv)
- {
- this.sql_driver = drv;
- return true;
- }
- public String getDriver()
- {
- return this.sql_driver;
- }
- public boolean setUrl(String url)
- {
- this.sql_url = url;
- return true;
- }
- public boolean setDBName(String dbname)
- {
- this.sql_DBName = dbname;
- return true;
- }
- public String getDBName()
- {
- return this.sql_DBName;
- }
- public boolean setUser(String user)
- {
- this.user = user;
- return true;
- }
- public String getUser()
- {
- return this.user;
- }
- public boolean setPwd(String Passwd)
- {
- this.Passwd = Passwd;
- return true;
- }
- public String getPwd()
- {
- return this.Passwd;
- }
- public DBConn()
- {
- try{
- Class.forName(sql_driver);//加载数据库驱动程序
- this.conn = DriverManager.getConnection(sql_url + "/" + sql_DBName + "?user=" + user + "&password=" + Passwd + "&useUnicode=true&characterEncoding=gb2312");
- this.stmt = this.conn.createStatement();
- }catch(Exception e){
- System.out.println(e.toString());
- }
- }
- //执行查询操作
- public ResultSet executeQuery(String strSql)
- {
- try{
- this.rs = stmt.executeQuery(strSql);
- return this.rs;
- }catch(SQLException e){
- System.out.println(e.toString());
- return null;
- }catch(NullPointerException e){
- System.out.println(e.toString());
- return null;
- }
- }
- //执行数据的插入、删除、修改操作
- public boolean execute(String strSql)
- {
- try{
- if(this.stmt.executeUpdate(strSql) == 0)
- return false;
- else
- return true;
- }catch(SQLException e){
- System.out.println(e.toString());
- return false;
- }catch(NullPointerException e){
- System.out.println(e.toString());
- return false;
- }
- }
- //结果集指针跳转到某一行
- public boolean rs_absolute(int row)
- {
- try{
- this.rs.absolute(row);
- return true;
- }catch(SQLException e){
- System.out.println(e.toString());
- return false;
- }
- }
- public void rs_afterLast()
- {
- try{
- this.rs.afterLast();
- }catch(SQLException e){
- System.out.println(e.toString());
- }
- }
- public void rs_beforeFirst()
- {
- try{
- this.rs.beforeFirst();
- }catch(SQLException e){
- System.out.print(e.toString());
- }
- }
- public void rs_close()
- {
- try{
- this.rs.close();
- }catch(SQLException e){
- System.out.print(e.toString());
- }
- }
- public void rs_deleteRow()
- {
- try{
- this.rs.deleteRow();
- }catch(SQLException e){
- System.out.print(e.toString());
- }
- }
- public boolean rs_first()
- {
- try{
- this.rs.first();
- return true;
- }catch(SQLException e){
- System.out.print(e.toString());
- return false;
- }
- }
- public String rs_getString(String column)
- {
- try{
- return this.rs.getString(column);
- }catch(SQLException e){
- System.out.println(e.toString());
- return null;
- }
- }
- //此方法用于获取大段文本,
- //将其中的回车换行替换为<br>
- //输出到html页面
- public String rs_getHtmlString(String column)
- {
- try{
- String str1 = this.rs.getString(column);
- String str2 = "rn";
- String str3 = "<br>";
- return this.replaceAll(str1,str2,str3);
- }catch(SQLException e){
- System.out.println(e.toString());
- return null;
- }
- }
- //把str1字符串中的str2字符串替换为str3字符串
- private static String replaceAll(String str1,String str2,String str3)
- {
- StringBuffer strBuf = new StringBuffer(str1);
- int index=0;
- while(str1.indexOf(str2,index)!=-1)
- {
- index=str1.indexOf(str2,index);
- strBuf.replace(str1.indexOf(str2,index),str1.indexOf(str2,index)+str2.length(),str3);
- index=index+str3.length();
- str1=strBuf.toString();
- }
- return strBuf.toString();
- }
- public int rs_getInt(String column)
- {
- try{
- return this.rs.getInt(column);
- }catch(SQLException e){
- System.out.println(e.toString());
- return -1;
- }
- }
- public int rs_getInt(int column)
- {
- try{
- return this.rs.getInt(column);
- }catch(SQLException e){
- System.out.println(e.toString());
- return -1;
- }
- }
- public boolean rs_next()
- {
- try{
- return this.rs.next();
- }catch(SQLException e){
- System.out.println(e.toString());
- return false;
- }
- }
- //判断结果集中是否有数据
- public boolean hasData()
- {
- try{
- boolean has_Data = this.rs.first();
- this.rs.beforeFirst();
- return has_Data;
- }catch(SQLException e){
- System.out.println(e.toString());
- return false;
- }
- }
- public boolean rs_last()
- {
- try{
- return this.rs.last();
- }catch(SQLException e){
- System.out.println(e.toString());
- return false;
- }
- }
- public boolean rs_previous()
- {
- try{
- return this.rs.previous();
- }catch(Exception e){
- System.out.println(e.toString());
- return false;
- }
- }
- //main方法,调试用
- public static void main(String args[])
- {
- try{
- DBConn myconn = new DBConn();
- //myconn.setDBName("shopping");
- //myconn.DBConn();
- //myconn.execute("Insert Into tbl_test(ID,UserID) values('10','shandaer')");
- //myconn.execute("Update tbl_test set UserID='yyyyyyyyyyyy' where ID=10");
- //myconn.execute("Delete from tbl_test where ID=1");
- ResultSet rs = myconn.executeQuery("select * from sysadmin order by ID desc limit 1");
- //boolean hasData = myconn.hasData();
- //System.out.println("has data:" + hasData);
- //rs.first();
- while (myconn.rs.next())
- {
- int ID = myconn.rs_getInt("ID") + 1;
- System.out.print(ID);
- System.out.println(myconn.rs_getInt("ID") + myconn.rs_getString("UserID"));
- //System.out.println('n' + myconn.rs_getHtmlString("UserID"));
- //System.out.println(myconn.rs.getString("UserID") + myconn.rs_getInt(1));
- }
- }catch(Exception e){
- System.err.println(e.toString());
- }
- }
- }