java連線access資料庫

fog911811發表於2011-12-22
 
package org;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class TestAcess {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try{
			String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=d:\\Report.mdb";
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			Connection conn= DriverManager.getConnection(url);
			PreparedStatement pstmt = null;
			//System.out.println("----"+ conn);

			String delSql = "delete from phone ";
			pstmt = conn.prepareStatement(delSql);
			pstmt.execute();
//			
			
            String sql ="select * from phone "; //where phone = 282
			
			pstmt = conn.prepareStatement(sql);
			ResultSet rs = null;
			rs = pstmt.executeQuery();
			String str = "";
			while(rs.next()){
				//System.out.println("***1:"+rs.getString(1)+"***2:"+rs.getString(2)+"***3:"+rs.getString(3)+"***4:"+rs.getString(4)+"***5:"+rs.getString(5)+"***6:"+rs.getString(6)+"***7:"+rs.getString(7)+"***8:"+rs.getString(8)+"***9:"+ rs.getString("phone"));
				str = str + rs.getString("phone")+",";
			}
			System.out.println("----"+str);
			
		}catch(Exception e){
			System.out.println("----"+e.getMessage());
		}
	}
}

相關文章