java sqlserver2008

工具:eclipse,jdk,sqljdbc4.jar

0.      将sqljdbc4.jar放在Java\jre7\lib路径下。

1.      新建工程,右键properties====java buildpath=====libraries=======add external jars=========找到sqljdbc.jar,选中。

工程目录下会多出一个referenced libraries,如图


2.建立一个SQL_Util类,作为辅助,该类中有一个静态方法vertify(),返回一个Connection对象。

代码

public class SQL_Util {
   public static final String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
   //SQL数据库引擎
   public static final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName= databaseName";
   //set a url
   private static final String USER = "sa";
   private static final String PASSWORD = "password";
   //set username andpassword
  
   //以下是一个静态方法,用于连接数据库,该方法返回一个Connection(数据库连接对象)
   public static final Connection vertify() {
      Connection con= null;
      try {
         Class.forName(JDriver);//加载数据库引擎
      } catch (ClassNotFoundException e){
         System.out.println("加载数据库引擎失败");
         System.exit(0);
      }
      if(con==null){
         try{
            con =DriverManager.getConnection(URL,USER,PASSWORD);
            //获取数据库连接对象
         } catch (SQLException e){
              e.printStackTrace();
         }
        }
      return con;
   } 
}

3.自己设置的一些静态方法,放在SQL_Util辅助里,(java里应该是有这些方法的,做课设的时候没有去查==,自己写了一点。)

//获取列数据
	public static String[] getColData(String colname, String tableName){
		String colData[] = null;
		Connection con = null;
		ResultSet rs = null;
		PreparedStatement pst = null;
		String sql = "select * from "+ tableName;
		//获取PreparedStatement变量
		try{
			con = SQL_Util.vertify();
			pst = con.prepareStatement(sql,
				     ResultSet.TYPE_SCROLL_INSENSITIVE,
				     ResultSet.CONCUR_READ_ONLY);
			//获取结果集
			rs = pst.executeQuery();
			//结果集滚动到最后一行
			rs.last();
			int x = rs.getRow();//获取行数
			colData = new String[x];
			int y = rs.getMetaData().getColumnCount();//获取列数
			//结果集滚回第一行
			rs.first();
			String data[][] = new String[x][y];//设置一个二维数组存放表格数据
			String head[] = new String[y];//设置一个一位数组作为表格列名
//			获取结果的列名,存储到一位数组中
			for(int i = 0; i<y; i++) {
				head[i] = rs.getMetaData().getColumnName(i+1).trim();
			}
//			将结果集中的数据取出,存放进二维数组
			for(int i = 0; i<x; i++) {
				for(int j = 0; j<y; j++) {
					data[i][j] = rs.getString(j+1);
				}
				rs.next();
			}
			int colnum = 0;
			for(int i = 0;i<head.length; i++){
				if(colname.equals(head[i])) { colnum = i; }
			}
			for(int i = 0; i<x; i++){
				colData[i] = data[i][colnum];
			}
		} catch (SQLException e){e.printStackTrace();}
		finally{
			try {con.close();} catch (SQLException e) {e.printStackTrace();}	
		}
		return colData;
	}

	//获取行数据
	public static String[] getRowData(int row, String tableName) throws SQLException{
		String[] rowData = null;
		Connection con = null;
		ResultSet rs = null;
		PreparedStatement pst = null;
		String sql = "select * from "+ tableName;
		try{
			//获取PreparedStatement变量
			con = SQL_Util.vertify();
			pst = con.prepareStatement(sql,
				     ResultSet.TYPE_SCROLL_INSENSITIVE,
				     ResultSet.CONCUR_READ_ONLY);
			//获取结果集
			rs = pst.executeQuery();
			//结果集滚动到最后一行
			rs.last();
			int x = rs.getRow();//获取行数
			int y = rs.getMetaData().getColumnCount();//获取列数
			rowData = new String[y];
			//结果集滚回第一行
			rs.first();
			String data[][] = new String[x][y];//设置一个二维数组存放表格数据
			String head[] = new String[y];//设置一个一位数组作为表格列名
//			获取结果的列名,存储到一位数组中
			for(int i = 0; i<y; i++) {
				head[i] = rs.getMetaData().getColumnName(i+1);
			}
//			将结果集中的数据取出,存放进二维数组
			for(int i = 0; i<x; i++) {
				for(int j = 0; j<y; j++) {
					data[i][j] = rs.getString(j+1);
				}
				rs.next();
			}
			for(int i = 0; i<data[row].length; i++) {
				rowData[i] = data[row][i];
			}
		}catch(SQLException e){e.printStackTrace();}
		finally{
			try {con.close();} catch (SQLException e) {e.printStackTrace();}	
		}
		return rowData;
	}
	
	//获取表格的属性名
	public static String[] getTableHead(String tableName) throws SQLException {
		String head[] = null;
		Connection con = null;
		ResultSet rs = null;
		PreparedStatement pst = null;
		String sql = "select * from "+ tableName;
		try{
			//获取PreparedStatement变量
			con = SQL_Util.vertify();
			pst = con.prepareStatement(sql,
				     ResultSet.TYPE_SCROLL_INSENSITIVE,
				     ResultSet.CONCUR_READ_ONLY);
			//获取结果集
			rs = pst.executeQuery();
			//结果集滚动到最后一行
			rs.last();
			int x = rs.getRow();//获取行数
			String s[] = new String[x];
			int y = rs.getMetaData().getColumnCount();//获取列数
			System.out.println(x+"行"+y+"列");
			//结果集滚回第一行
			rs.first();
			String data[][] = new String[x][y];//设置一个二维数组存放表格数据
			head = new String[y];//设置一个一位数组作为表格列名
//			获取结果的列名,存储到一位数组中
			for(int i = 0; i<y; i++) {
				head[i] = rs.getMetaData().getColumnName(i+1);
			}
//			将结果集中的数据取出,存放进二维数组
			for(int i = 0; i<x; i++) {
				for(int j = 0; j<y; j++) {
					data[i][j] = rs.getString(j+1);
				}
				rs.next();
			}
		}catch(SQLException e){e.printStackTrace();}
		finally{
			try {con.close();} catch (SQLException e) {e.printStackTrace();}	
		}
		return head;
	}
	
	//获取表格
	public static String[][] getTableData(String tableName) throws SQLException {
		String data[][] = null;
		Connection con = null;//连接数据库并获得Connection变量
		ResultSet rs = null;
		PreparedStatement pst = null;
		String sql = "select * from "+ tableName;
		try{
			//获取PreparedStatement变量
			con = SQL_Util.vertify();
			pst = con.prepareStatement(sql,
				     ResultSet.TYPE_SCROLL_INSENSITIVE,
				     ResultSet.CONCUR_READ_ONLY);
			//获取结果集
			rs = pst.executeQuery();
			//结果集滚动到最后一行
			rs.last();
			int x = rs.getRow();//获取行数
			String s[] = new String[x];
			int y = rs.getMetaData().getColumnCount();//获取列数
			System.out.println(x+"行"+y+"列");
			//结果集滚回第一行
			rs.first();
			data = new String[x][y];//设置一个二维数组存放表格数据
			String head[] = new String[y];//设置一个一位数组作为表格列名
//			获取结果的列名,存储到一位数组中
			for(int i = 0; i<y; i++) {
				head[i] = rs.getMetaData().getColumnName(i+1);
			}
//			将结果集中的数据取出,存放进二维数组
			for(int i = 0; i<x; i++) {
				for(int j = 0; j<y; j++) {
					data[i][j] = rs.getString(j+1);
				}
				rs.next();
			}
		}catch(SQLException e){e.printStackTrace();}
		finally{
			try {con.close();} catch (SQLException e) {e.printStackTrace();}	
		}
		return data;
	}
}


---------------待续-----------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值