LIULIANG

JDBC连接SQL Server2008后四中基本操作的基本格式

下面的代码显示了连接SQL Server数据库时,
连接数据库的步骤:
1、注册驱动 (只做一次)

2、建立连接(Connection)
3、创建执行SQL的语句(Statement)

4、执行语句

5、处理执行结果(ResultSet)

6、释放资源

01 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");  

02                //加载数据库引擎,返回给定字符串名的类  

03 String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";  

04               //test为你的数据库的名称  

05 String user="用户名";  

06 String password="密码";  

07    

08 Connection conn=DriverManager.getConnection(url,user,password);  

09                 //连接数据库对象  

10 Statement stmt=conn.createStatement();  

11           //创建SQL命令对象  

12    

13    

14 String query="";//创建表SQL语句  

15 stmt.executeUpdate(query);//执行SQL命令对象  

16    

17 String a="INSERT/DELETE. INTO/FROM “表名” VALUES(.,)";  

18 .  

19 .  

20 .  

21 stmt.executeUpdate(a);//执行SQL命令对象  

22    

23 //读取数据  

24 ResultSet rs=stmt.executeQuery("SELECT * FROM “表名”");  

25 //循环输出每一条记录    rs最初指向第一条记录的前面  

26 while(rs.next){  

27        

28 }
  

29    

30 最后关闭连接  

31 stmt.close();  

32 con.close();  

33    

34 数据库连接(Connection)是非常稀有的资源,用完后必须马上释放,  

35 如果Connection不能及时正确的关闭将导致系统宕机。  

36 Connection的使用原则是尽量晚创建,尽量早的释放。 

 

转自:http://www.open-open.com/lib/view/1329999223671

posted @ 2012-04-06 09:34 BIG森林 阅读(202) | 评论 (0)编辑 收藏

JDBC连接SQL Server2008基本格式及示例代码

package pck;
import java.sql.*;
public class DBtest {

    
public static void main(String[] args){
        
         String dbDriver 
= "com.microsoft.sqlserver.jdbc.SQLServerDriver";// 声明数据库驱动名(这个是微软的驱动名)
        
// String dbUrl = "jdbc:sqlserver://localhost:1433;DatabaseName=test";// 数据库驱动程序URL,和相应的驱动配套。
         String dbUrl = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test";// 数据库驱动程序URL,和相应的驱动配套。
         String dbUser = "sa";// 定义数据库登陆用户名
         String dbPasw = "20101128";// 定义相应用户的登陆密码
         try{
             Class.forName(dbDriver);
//加载驱动程序  // 动态导入数据库的驱动   
             Connection    con = DriverManager.getConnection(dbUrl, dbUser, dbPasw);// 获取,建立数据库链接   
              java.sql.Statement sm = null;  //创建一个sql命令语句
                String Select = null;
                ResultSet rSet 
= null;   //结果集
                sm = con.createStatement();//将sql命令绑定到相应的连接上
                
//查询数据
                Select = "select * from userTB";
                rSet 
= sm.executeQuery(Select);   //执行sql语句
                System.out.println("  原始数据。");
                System.out.println(
"  编号      姓名      年龄     性别        家庭住址      学号");
                
while(rSet.next()) {
                     System.out.println(
"  "+rSet.getString(1)+"  "+rSet.getString(2)+"    "+
                            rSet.getString(
3)+"  "+rSet.getString(4)+"  "+rSet.getString(5)+"    "+rSet.getString(6)
                            );}

                    
//执行插入操作
                     String insert=" insert  userTB(name,age,sex,address,phone,email)values('张三',24,'男','北京大兴','123','123')";
                     System.out.println(
"  开始执行新增操作。");
                     sm.executeUpdate(insert); 
                     System.out.println(
"  执行插入成功。插入后数据为:");
                     
//检测插入后的效果
                     Select = "select * from userTB";
                        rSet 
= sm.executeQuery(Select);   //执行sql语句
                        System.out.println("  编号      姓名      年龄     性别        家庭住址      学号");
                        
while(rSet.next()) {
                             System.out.println(
"  "+rSet.getString(1)+"  "+rSet.getString(2)+"    "+
                                    rSet.getString(
3)+"  "+rSet.getString(4)+"  "+rSet.getString(5)+"    "+rSet.getString(6)
                                    );}

                
//执行修改操作
                     String update="update userTB set phone='12345' where  name='张三'" ;
                     System.out.println(
"  开始执行修改操作。");
                     sm.executeUpdate(update); 
                     System.out.println(
"  执行修改成功。修改后数据为:");
                     
//检测插入后的效果
                     Select = "select * from userTB";
                        rSet 
= sm.executeQuery(Select);   //执行sql语句
                        System.out.println("  编号      姓名      年龄     性别        家庭住址      学号");
                        
while(rSet.next()) {
                             System.out.println(
"  "+rSet.getString(1)+"  "+rSet.getString(2)+"    "+
                                    rSet.getString(
3)+"  "+rSet.getString(4)+"  "+rSet.getString(5)+"    "+rSet.getString(6)
                                    );}

                
//执行删除操作
                     String delete=" delete userTB where name='张三' " ;
                     System.out.println(
"  开始执行删除操作。");
                     sm.executeUpdate(delete); 
                     System.out.println(
"  执行删除成功。删除后数据为:");
                     
//检测插入后的效果
                     Select = "select * from userTB";
                        rSet 
= sm.executeQuery(Select);   //执行sql语句
                        System.out.println("  编号      姓名      年龄     性别        家庭住址      学号");
                        
while(rSet.next()) {
                             System.out.println(
"  "+rSet.getString(1)+"  "+rSet.getString(2)+"    "+
                                    rSet.getString(
3)+"  "+rSet.getString(4)+"  "+rSet.getString(5)+"    "+rSet.getString(6)
                                    );}

                 con.close();  
//关闭数据库连接
                }
 catch (ClassNotFoundException e){
                
// 当应用程序试图使用以下方法通过字符串名加载类时,抛出该异常:Class 类中的 forName 方法。
                System.out.println("无驱动!");
                e.printStackTrace();
                }
 catch (SQLException e){
                System.out.println(
"连接异常!");
                e.printStackTrace();
                }

                
//System.out.println("连接成功!");
          }

    
    
}

posted @ 2012-04-06 09:32 BIG森林 阅读(389) | 评论 (0)编辑 收藏

SQL数据库WMI 0x80041010 如何解决?

事件起因:

     由于要做第二次机房收费系统,而数据库是必不可少的.但是自己之前已经装好了VS2010,在和数据库连接的时候提醒我不支持SQL2010,支持2005以及更高的版本(不过后来开会知道并不是不可以用,需要一些设置就可以了,不过那时候我已经卸载SQL2000已经装好SQL2008),因为自己之前担心SQL2000会删不干净.于是在删除之前做了一下准备,在网上查了查相关资料.本次问题的起因和自己删除SQL2000有关,在删除SQL2000的时候有一些文件删除不掉.文件夹地址是 "C:\Program Files\Microsoft SQL Server"然后我就一时糊涂的把这里的文件给删掉了.后来再装SQL2008的时候马上就要结束的时候出现了错误,不能经常安装,我估计是我误删文件了.之后让争光帮忙把文件夹下的文件给我拷贝一份.然后再次安装.成功!

但是安装成之后当我打开配置管理选项的时候出现了以上图片显示的错误.

出现了以上的问题如何解决呢?

clip_image001

知识点:WMI(Windows Management Instrumentation,Windows 管理规范)是一项核心的 Windows 管理技术;用户可以使用 WMI 管理本地和远程计算机。这个知识仅仅做一个了解以后用到再深入学习.

那就开始从网上找吧,不过在我找之前脑子里闪现了一个想法,自己之前都是通过百度找到相关的问题解决方案,而自己一个寒假几乎都在学英语做找英语资料的工作,这次也尝试一下到国外的网站去找相关的问题解决方案.仅仅做一个尝试.

找到了相关的资源

第一种:

SQL Server - WMI Error 0x80041010 - Invalid Class

Recently I started experiencing WMI repository corruption that results in inability to open SQL Server Configuration Manager with the following error message:

Cannot connect to WMI provider. You do not have permission or the server is unreachable. Note that you can only manage SQL Server 2005 and later servers with SQL Server Configuration Manager.

Invalid class [0x80041010]

At this point if I browse WMI repository under root\microsoft\sqlserver\computermanagement11 I see no SQL Server-specific classes. Only system classes exist. To resolve this issue SQL Server WMI provider needs to be reinstalled.

Step 1 - Install classes

Navigate to C:\Program Files (x86)\Microsoft SQL Server\<SQL Server version, for example "110" for SQL Server 11>\Shared if you have x64 installation of SQL Server. On x86 machine the path is C:\Program Files\Microsoft SQL Server\<SQL Server version, for example "110" for SQL Server 11>\Shared. Make sure that file sqlmgmproviderxpsp2up.mof exists. If it doesn't then either you're in the wrong location or your installation is corrupted and only SQL Server setup can fix the problem. However if the file does exist, then resolution is as simple as running command:

mofcomp sqlmgmproviderxpsp2up.mof

WMI compiler will notify you that repository was successfully updated

Step 2 - Install localization information

After the classes are deployed localization information needs to be installed as well. From the path in the previous step navigate to the sub-folder that reflects the locale of SQL Server installation. For example 1033 (for english-US). Full path is now C:\Program Files (x86)\Microsoft SQL Server\<SQL Server version, for example "110">\Shared\<locale ID, for example "1033" for english>. When at this path run the following command:

mofcomp sqlmgmprovider.mfl

WMI resopitory should have been updated successfully again.

Now you're all set. The problem should be resolve and SQL Server Configuration Manager should be functional again. If it still isn't the I would encourage you to describe your problem at SQL Server Manageability forum - http://social.msdn.microsoft.com/Forums/en-US/sqlkjmanageability/threads

源文档 <http://scottless.com/blog/archive/2010/12/07/sql-server---wmi-error-0x80041010---invalid-class.aspx>

上面的英文主要的意思是说他也遇到了和我相同的问题.导致这个问题的原因是一个文件的问题,这个文件的名字是 sqlmgmproviderxpsp2up.mof ,看着个文件是否存在,如果不存在的话就更新一下,就在运行框运行一下代码

mofcomp sqlmgmproviderxpsp2up.mof

之后WMI将会通知更新成功.但是我按照他的方法去做出现以下错误

clip_image002

第一步没有成功,第二步就更不用去看了,于是再去找其它的方法.

找到了其它网站解决相同的问题,可以确定问题就是出现在 sqlmgmproviderxpsp2up.mof文件上.但是具体的操作不是一样.(加上了路径)

第二种:

SQL Server Configuration Manager – Cannot connect to WMI provider – Invalid class [0x80041010]

When I am trying to connect to my SQL Server 2008 box using SQL Server Configuration Manager today, the following error dialog pops up…

Cannot connect to WMI provider. You do not have permission or the server is unreachable. Note that you can only manage SQL Server 2005 and later servers with SQL Server Configuration Manager. Invalid class [0x80041010]

clip_image003

I have never seen this before!… while the following line saves me.

>> Open an elevated Command Prompt, and type the following…

mofcomp.exe "C:\Program Files (x86)\Microsoft SQL Server\100\Shared\sqlmgmproviderxpsp2up.mof"

clip_image004

问题的解决方法和第一种类似,不过更加的具体.然后我就按照他的方式执行.但是还是没有成功(其实自己现在就差一点点了就可以成功了).如下:

clip_image005

由于时间问题昨天晚上没有把这个问题解决了,感觉特别的不舒服,于是自己想着把电脑回宿舍,重新再装一下SQL2008.但是悲剧的是当自己装到十二点安装完成的时候,一运行配置管理,还是相同的问题......于是就睡觉了.(~﹃~)~zZ

晚上做梦貌似都在装数据库...... ╮(╯_╰)╭

今天早早的就起来,到机房想着一定把这个给解决掉.折腾了半个小时还是没有弄好.最后让伟东帮我看一下,提示我是不是路径的问题.一语道破天机.其实路径没有问题,关键是在(x86)果然问题就在这里,按照我自己文件的路径输入之后,更新成功!

如下:

clip_image006

反省和总结

我的错误原因:

1 (x86)不够细心.

原作者的命令是:mofcomp.exe "C:\Program Files (x86)\Microsoft SQL Server\100\Shared\sqlmgmproviderxpsp2up.mof"

而我的命令是:mofcomp.exe "C:\Program Files \Microsoft SQL Server\100\Shared\sqlmgmproviderxpsp2up.mof"

关于Windows中32位(x86)和64位(x64)解释 见链接.

2 一些自己并不清楚的文件不要乱删.

收获:

通过此次自己也收获不小是自己第一次尝试用英文来找相关的解决资料,后来再百度一下发现大部分的答案都是从国外翻译过来的,认识到英语的重要性!


转自:http://www.cnblogs.com/jnqqls/archive/2011/03/09/1979124.html

posted @ 2012-04-06 08:59 BIG森林 阅读(506) | 评论 (0)编辑 收藏

MSR系列路由器作为FTP-Server实现文件上传下载功能的配置


一、组网需求

将PC与MSR路由器以太口直连,配置相应IP地址,保证设备之间互通。 MSR路由器作为FTP Server。PC作为Client端,登录到路由器上实现文件的上传下载。

设备清单:Router(MSR系列路由器),PC 各一台

二、组网图:

    

图 1-1 路由器作为FTP-Server测试组网图

适用设备和版本:MSR系列、Version 5.20, Beta 1106及后续版本。

三、配置步骤:

1.            按照图11所示连接好路由器PC,设置路由器e0/0口的IP地址为1.1.1.1/16,PC的IP地址为1.1.1.2/16;保证相互之间可以ping通;

2.            配置路由器启动FTP Server,在Router配置以下命令:

下载文件操作

//使能被测设备FTP Server服务,设置设备的IP地址为1.1.1.1/16

[Router]ftp server enable

[Router]interface Ethernet 0/0

[Router-Ethernet0/0]ip address 1.1.1.1 16

//创建新用户ftp,设置用户ftp的登录密码为ftpftp

[Router]local-user ftp

[Router-luser-ftp]password simple ftpftp

//设置用户ftp的服务类型为FTP,登录后的工作目录为cf:/

[Router-luser-ftp]service-type ftp ftp-directory cf:/

//配置PC:以FTP方式登录FTP服务器,下载文件

C:\ >ftp 1.1.1.1

Connected to 1.1.1.1

220 FTP service ready.

User (1.1.1.1:(none)): ftp

331 Password required for ftp

Password:                  //此处输入:ftpftp

230 User logged in.

//进行文件下载到PC的操作,Source filename 是待下载文件名,Destination filename是下载成功后保存为的文件名,如果不输入,默认与源文件同名

ftp> get <Source filename>   <Destination filename>Password:                     

230 User logged in    

 

//下载成功后查看结果:

默认下载的目标文件保存在PC的C:\ 下

上传文件操作

[Router]ftp server en

//创建新用户ftp,设置用户ftp的登录密码为ftpftp

[Router]local-user ftp

[Router-luser-ftp]password simple ftpftp

//设置用户ftp的服务类型为FTP,登陆后的工作目录为cf:/

[Router-luser-ftp]service-type ftp ftp-directory cf:/

//配置PC:以FTP方式登录FTP服务器,上传文件

C:\ >ftp 1.1.1.1

Connected to 1.1.1.1

220 FTP service ready.

User (1.1.1.1:(none)): ftp

331 Password required for ftp

Password:               //此处输入:ftpftp

230 User logged in    

 

//进行文件上传操作,Source filename 是待上传文件名,Destination filename是上传成功后保存到路由器的文件名,如果不输入,默认与源文件同名

ftp> put  <Source filename>   <Destination filename>

//上传成功后显示如下结果:

200 Port command okay.

150 Opening ASCII mode data connection for xxxx

226 Transfer complete.

ftp: 406 bytes sent in xxxSeconds xxxxKbytes/sec.

ftp>

//查看配置结果,在路由器上通过DIR命令可以查看上传成功的文件名

<Router>dir

四、配置关键点

1) 保证路由器PC间的连通性;

2) 输入用户名、密码、文件名等参数是否正确;
转载:
http://www.56cto.com/html/HuaWei/1/25569.html

posted @ 2012-03-30 23:55 BIG森林 阅读(1030) | 评论 (0)编辑 收藏

仅列出标题
共9页: 1 2 3 4 5 6 7 8 9 
<2012年8月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(2)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜