SqlServer数据库jdbc连接(bean)

java使用jtds连接sqlserver2000问题
一般以下几个问题:
1、WINDOWS防火墙屏蔽了1433端口
2、检查sqlserver2000是否使用的是1433端口
3、检查sqlserver2000是否升级到SP4版本(基本都是这个问题)


属性文件DataBase.properties
jdbc.driver=net.sourceforge.jtds.jdbc.Driver
jdbc.url=jdbc:jtds:sqlserver://localhost:1433;DatabaseName=pubs
jdbc.user=sa
jdbc.password=sa


package com.michael.database;
import java.io.*;
import java.sql.*;
import java.util.*;
public class DBManager {
   private Connection conn = null;
   private Statement stmt = null;
   private ResultSet rs = null;

   /**
    * the Default Constructor
    */

   public DBManager(){
    init();
   }

   /**
    * connect database
    */
  
   private void init(){
    Properties props = new Properties();
    /*从属性文件中读*/
    String fileName = "DataBase.properties";
    FileInputStream fis = null;
    try {
      fis = new FileInputStream(fileName);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    //InputStream is = getClass().getResourceAsStream(fileName);
    /*加载输入流中的文件*/
    try {
      props.load(fis);
    } catch (IOException e) {
     e.printStackTrace();
    }
    /*从输入流中读取属性列表(key键和value值对)*/
    String driver = props.getProperty("jdbc.driver");
    String url = props.getProperty("jdbc.url");
    String user = props.getProperty("jdbc.user");
    String password = props.getProperty("jdbc.password");
 
    try {
      Class.forName(driver);
    } catch (ClassNotFoundException e) {
     e.printStackTrace();
    }
    try {
      conn = DriverManager.getConnection(url, user, password);
      stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    } catch (SQLException e) {
     e.printStackTrace();
    }
  }
   /**
    * 查询数据库的结果集
    * @param sql
    * @return ResultSet
    */

  public ResultSet getResultSet(String sql){
    try {
     rs = stmt.executeQuery(sql);
    } catch (SQLException e) {
     e.printStackTrace();
    }
     return rs;
  }
  /**
   * insert/update/delete数据库记录
   * @param sql
   * @return boolean
   */
 
  public boolean execute(String sql){
    try {
     return stmt.execute(sql);
    } catch (SQLException e) {
     e.printStackTrace();
    }
    return false;
  }
  /**
   * close Connection/Statement/ResultSet
   * @param conn
   * @param stmt
   * @param rs
   */
 
  public void closeConnection(Connection conn,Statement stmt,ResultSet rs){
    if(conn!=null)
    try {
     conn.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   if(stmt!=null)
    try {
     stmt.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   if(rs!=null)
    try {
     rs.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
}

属性文件SqlScript.properties
sql.words1=c://dbtable1.sql
sql.words2=c://dbtable2.sql


package com.michael.database;
import java.io.*;
import java.util.*;
public class DataTransferFromSqlScript {
   private DBManager db = new DBManager();

   /*
    * @param positions
    */
 
   private static void swap(Object[] positions,int i,int j){
     Object temp = positions[i];
     positions[i] = positions[j];
     positions[j] = temp;
   }

   public void readAndSaveResumeData(){
     System.out.println("translate data starting ......");
     Properties props = new Properties();
     /*从属性文件中读*/    
     String fileName = "SqlScript.properties";
     FileInputStream fis = null;
     try {
       fis = new FileInputStream(fileName);
     } catch (FileNotFoundException e) {
       e.printStackTrace();
     }
     //InputStream is = getClass().getResourceAsStream(fileName);
     /*加载输入流中的文件*/

     try {
       props.load(fis);
     } catch (IOException e) {
       e.printStackTrace();
     }
     /*从输入流中读取属性列表(key键和value值对),value是sql脚本存放位置*/   
     List<String> files = new ArrayList<String>();
     files.add(props.getProperty("sql.words1"));
     files.add(props.getProperty("sql.words2"));

     for(String filename : files){
       File file = new File(filename);
       List<String> sqls = new ArrayList<String>();
       try {
         FileReader in = new FileReader(file);
         BufferedReader br = new BufferedReader(in);
         String s;
         String str = "";
         while((s = br.readLine())!= null){
          if(s.startsWith("insert into")){
            str = s;
          }else if(s.startsWith("values(")){
            s = getSql(s);
            sqls.add((str+s).replace(";", ""));
          }
       }
        br.close();
        in.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
  
      // Object[] sqlsSorted = sortSqls(sqls, -1);
  
      Object[] sqlsSorted = sqls.toArray();
  
     System.out.println(filename+" total: "+sqlsSorted.length);
  
     for(Object sql : sqlsSorted){
       System.out.println(sql);
      /*执行sql语句*/
      db.execute(sql.toString());
     }
   }
}

  private String getSql(String s){
    int i = s.indexOf("to_timestamp");
    if(i>0){
      return s.replace(s.substring(i,i+70),null);
    }else{
      return s;
    }
  }

  /**
   * @param sqls
   * @param n (n > 0 or n < 0)
   * @return
   */
 
  public static Object[] sortSqls(List<String> sqls,int n){
    Object[] array = sqls.toArray();
    for(int i = 0;i < array.length;i++){
      for(int j = array.length-1;j>i;j--){
       if(n>0){
         if(getId((String)array[j]) > getId((String)array[j-1]))
         swap(array,j,j-1);
      }else if(n<0){
        if(getId((String)array[j]) < getId((String)array[j-1]))
        swap(array,j,j-1);
      }
     }
    }
    return array;
  }

  /**
   * @param sql
   * @return
   */
 
  public static int getId(String sql){
    int start = sql.indexOf("values(");
    String s = sql.substring(start).split(",")[0].substring(8);
    return Integer.parseInt(s);
  }
}

你可能感兴趣的:(sql,bean,jdbc,防火墙,J#)