ConDB.java
上传用户:mkymky1985
上传日期:2014-06-23
资源大小:754k
文件大小:3k
源码类别:

其他数据库

开发平台:

Java

  1. package wsxk;
  2. import java.sql.*;
  3. public class ConDB{
  4. public static Connection init(){
  5. Connection conn = null;
  6. String url = "jdbc:odbc:xuanke";
  7. try {
  8. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  9. try {
  10. conn= DriverManager.getConnection(url);
  11. } catch (SQLException e) {
  12. // TODO Auto-generated catch block
  13. e.printStackTrace();
  14. }
  15. } catch (ClassNotFoundException e) {
  16.              e.printStackTrace();
  17. }
  18. return conn;
  19. }
  20.   
  21.   public ResultSet executeQuery(String sql)
  22.   {
  23.   ResultSet rs = null;
  24.     try {
  25.       rs = null;
  26.       //System.out.println(url);
  27.       Connection conn= this.init();
  28.       //System.out.println(url);
  29.       Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  30.       rs = stmt.executeQuery(sql);
  31.     }
  32.     catch (SQLException ex) {
  33.  System.out.println("执行错误");  
  34.  ex.printStackTrace();}
  35.     return rs;
  36.   }
  37.         public void executeUpdate(String strSQL)
  38.         {
  39.                 try
  40.                 {
  41.                         Connection conn= this.init();
  42.                         Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  43.                         stmt.executeQuery(strSQL);
  44.                 }
  45.                 catch(SQLException ex)
  46.                 {
  47.                         System.err.println("连接数据库失败了,异常为:" +ex.getMessage());
  48.                 }
  49.         }
  50.          public String ex_chinese(String str)
  51.          {
  52.                  if(str==null)
  53.                  {
  54.                                 str  ="" ;
  55.                  }
  56.                  else
  57.                  {
  58.                            try
  59.                            {
  60.                                          str = new String(str.getBytes("iso-8859-1"),"gb2312") ;
  61.                            }
  62.                            catch (Exception ex)
  63.                            {
  64.                                         System.err.println("异常为:" +ex.getMessage());
  65.                            }
  66.                  }
  67.                  return str ;
  68.          }
  69.          public String gettime()
  70.          {
  71.                 String datestr =  "" ;
  72.                 try
  73.                 {
  74.                         java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy年MM月dd日 HH点ss分") ;
  75.                         java.util.Date date = new java.util.Date() ;
  76.                         datestr = df.format(new java.util.Date()) ;
  77.                 }
  78.                 catch (Exception ex)
  79.                 {
  80.                         System.err.println("异常为:" +ex.getMessage());
  81.                 }
  82.                 return datestr ;
  83.          }
  84.          public static void main(String args[]){
  85.           ConDB c = new ConDB();
  86.           
  87.          }
  88.         
  89. }