java连接数据库类及取得其中数据

package DB;
import java.sql.*;

public class ConnDB {
  public Connection DBconn(String ServerName,String DBName,String User,String Pwd){
   Connection conn=null;
   try{
   Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
   String url="jdbc:microsoft:sqlserver://"+ServerName+":1433;DatabaseName="+DBName;
   conn=DriverManager.getConnection(url,User,Pwd);
   //System.out.print("数据库连接成功!");
   return conn;
   }
   catch(Exception ee)
   {
   //System.out.print(ee.toString());
   ee.printStackTrace();
    return conn;
   }
  
  }
}

**************************************************

ConnDB DBconn=new ConnDB();
    Connection conn=DBconn.DBconn("localhost","javatest","sa","");
    if(conn==null){
     MessageDialog.openInformation(null, "提示","连接数据库失败" );     
      return;
     }
    if(User_name.length()>0 && User_pwd.length()>0){
     //Statement updaters=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); // conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
     Statement updaters;
     try {
      updaters=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
      String updatesql="select xname,xpassword,xlevel from xm_member where xname='"+User_name+"' and xpassword='"+User_pwd+"'";
      ResultSet rs=updaters.executeQuery(updatesql);
      rs.last();
      int Iscount=rs.getRow();
      //Statement stm=conn.createStatement();
      //int Iscount=1;
      if(Iscount==1){
       MessageDialog.openInformation(null, "提示","登陆成功" );
      }else{
       MessageDialog.openInformation(null, "提示","登陆失败" );
      }
      updaters.close();

      
     } catch (SQLException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
     }
    }else{
     MessageDialog.openInformation(null, "提示","用户名,密码不能为空!" );
    }
    try {
     conn.close();
    } catch (SQLException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
    }

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值