您现在的位置: 万盛学电脑网 >> 程序编程 >> 数据库 >> 数据库综合 >> 正文

Java访问Sql Server 2008 数据库样例

作者:佚名    责任编辑:admin    更新时间:2022-06-22

今天给大家带来的是Java访问Sql Server 2008 数据库样例,希望能帮助大家。

    (1),文件列表

         

     (2) java访问sql server2008R2 设置 参考:

             java开发相关资料        http://blog.csdn.net/keenweiwei/article/details/7328611

     (3) 数据库 表结构

            

     (4)运行结果

                

     (5) 完整java代码下载: http://good.gd/1965614.htm

                (DBHelper 抄的人家的,自己改了一部份)

 

1.test1.java

[java] view plaincopyprint?

  1. import java.sql.ResultSet;  

  2. //import javax.swing.JOptionPane;  

  3.   

  4. public class test11 {  

  5.     public static void main(String[] args) {  

  6.             DBHelper help=new DBHelper();  

  7. /*          if(help.TestConn()) 

  8.                 System.out.println("连接成功1"); 

  9.             else 

  10.                 System.out.println("连接失败1");*/  

  11.               

  12.             //JOptionPane.showMessageDialog(null, "hello world");  

  13.               

  14.             //String sSQL="select * from w_brand where id=? and brand_name =?";  

  15.               

  16.             String sSQL="select * from w_brand where id=? and brand_name like '%{0}%'";  

  17.             sSQL=sSQL.replace("{0}", "柔");  

  18.             Object[] oParams=new Object[1];  

  19.             oParams[0]=5;  

  20.             //oParams[1]="柔";  

  21.               

  22. /*          ResultSet rs=help.GetResultSet(sSQL, oParams); 

  23.             try 

  24.             { 

  25.                 if(rs.next()) 

  26.                     System.out.println("id: " + rs.getString(1) + " brand_name: " + rs.getString(2)); 

  27.                     ////索引从1开始 

  28.             }catch(Exception ex) 

  29.             { 

  30.                 System.out.println(ex.getMessage()); 

  31.             }finally 

  32.             { 

  33.                 try 

  34.                 { 

  35.                     rs.close(); 

  36.                 }catch(Exception ex) 

  37.                 { 

  38.                     System.out.println(ex.getMessage()); 

  39.                 } 

  40.             }*/  

  41.               

  42.             Object oResult=help.GetSingle(sSQL, oParams);  

  43.             if(oResult!=null)  

  44.                 System.out.println(oResult.toString());  

  45.             else  

  46.                 System.out.println("没有符合条件的数据");  

  47.               

  48.             //DataTable dt=help.GetDataTable(sSQL,oParams);  

  49.               

  50. /*          String sSQL="select * from w_brand"; 

  51.             DataTable dt=help.GetDataTable(sSQL);*/  

  52.               

  53. /*  &