利用JDBC编写能实现数据库连接与断开的程序段

类别:Java 点击:0 评论:0 推荐:

程序代码如下:

import java.net.url;
import java.sql.*import java.net.url;
import java.sql.*
class_jdbc_connect
{
           public static void main(string argv[])
             {
                if(argv.length==0
                     {
                           system.err.println("检测到空的URL!");
                      }
                try
                     {
                         class.forName("textfiledriver");
                         class.forName("jdbc.odbc.jbdcodbcdriver");
                         class.forName("imaginary.sql.imsqldriver");
                        string url=argv[0];
                         string urser,pwd;
                        if(argv.lenght>1)
                         {
                           user=argv[1];
                          }
                        else
                           {
                                  user=""';
                           }
                        if(argev.length>2)
                          {
                                  pwd=argv[2];
                          }
                        else
                          {
                                     pwd="";
                           }
                        connection con=drivermanager.getconnection (url,user,pwd);
                        con.close();
                        system.out.println("连接失败!");
            }
     //处理连接异常
           catch(Exception e)
              { 
                    system.out.println(e.getMessage());
                    printstacktrace();
               }
   }}}

不知道以上代码编写得是否很好,如果您觉得有更好的方式,请多多指教我一下:-)

本文地址:http://com.8s8s.com/it/it13929.htm