2009-06-30

 

Q:第一:关于try...catch...finally,我的理解如下:try中放置的是要执行的代码,如果发生了异常代码就跳转到catch,最后finally中不管是什么情况都要执行的代码;所以我们一般把关闭文件以及关闭数据库连接的代码都放到finally中执行。

我现在的疑问是:因为finally是可选的,没有使用finally的话,那么关闭数据库的链接的这个操作要放在哪里呢? 
第二:因为在finally块中,再次可以嵌入try....catch....的,那么关闭数据库的操作conn.close()是要放在try块的吗?

try { 
  result.close(); 
  Query.close(); 
  conn.close(); 
  } catch (SQLException e) {



是不是要写成这样呢?

 

A:如果没有finally,在你每次操作结束后直接判断一下conn等是否为空,然后colse掉就行了。也就是你说的try里面,try内的代码如果出现异常会发送到catch中

A:不用finally的话,就写在try里面就可以了,你在用完一个链接的时候给关掉就可以了,没必要一定要写finally。

 

 

2:java怎样连接数据库

A://就是用jdbc-odbc桥做吧,前提是你已经配好数据源
/**
* 主要完成数据库的连接并返回该连接

*/

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;

public class GetDBConnection
{
/**
  * 返回到数据库的一个连接
  */
public static Connection getConnection()
{
      Connection con = null;
      String url = "jdbc:odbc:数据库名字";
   String userName ="sxp";
   String password ="sxp";
   String driver="sun.jdbc.odbc.JdbcOdbcDriver";
   try
   {
    Class.forName(driver);
    con = DriverManager.getConnection(url, userName, password);
   }
   catch(SQLException e)
   {
    e.printStackTrace();
   }
   catch(ClassNotFoundException ex)
   {
    ex.printStackTrace();
   }

   return con;
}

/**
  * 销毁数据库的连接,释放系统资源
  */
public static void destroyConnection(Connection con, Statement stmt)
{
  try
  {
      stmt.close();
      con.close();
  }
  catch(SQLException e)
  {
      System.out.println("数据库没有正常关闭!!!");
   e.printStackTrace();
  }
}
}

 

Q:


Java 怎样才能作一个连接数据库的登陆?




数据库用JDBC 。 我想作个用户名密码登陆的界面。
用户名是1 密码也是1 然后能登陆的到想登陆的界面、
主要是怎么判断用户名和密码 那块。 请指教下。 最好
带个例子!



A:WEB JSP:

<form name="LoginForm" action="/**.do(/servletName)" method="post"> 
  
  UserName: <input name="UserName" value="" type="text"/> 
  
  PassWord: <input name="PassWord" value="" type="password"/> 
  
     <input value="submit" type="submit"/> 
  
     <input value="reset" type="reset"/> 
  
</form> 
  
-------------------------------------------------------------------------------- 
  
Answer : 
  

  Connection Class: 
  
import java.sql.*; 
  
import java.util.*; 
  

public class DBUtil { 
  
  public static Connection CONN;  
  
  
  
  static { 
  
   Class.forName("***This is DB New***"); 
  
   CONN = DriverManager.getConnection("***This is DB type URL***","***UserName(DB)***","***PassWord(DB)***");   
  
  } 
  
  
  
  //Get a Connection 
  
  public static Connction getConnection() throws SQLException { 
  
   return this.CONN; 
  
  } 
  
  
  
  //Close a Connection 
  
  public static void closeConnection() throws SQLException { 
  
   this.CONN.close(); 
  
  } 
  
}





  Check Class:

import java.Util.*; 
  
import java.sql.*; 
  

public class CheckLogin { 
  
  public static String SELECT_SQL = ""; 
  

  public boolean checkLogin(String UserName,String PassWord) { 
  
   Boolean boolean = false; 
  
   
  
   try { 
  
    Connection con = DBUtil.getConnection(); 
  
   
  
    this.SELECT_SQL = "select * from TableName where username = ? and password = ?"; 
  
    PreparedStatenent psmt = con.prepareStatement(this.SELECT_SQL); 
  
    psmt.setString(1,UserName); 
  
    psmt.setString(2,PassWord); 
  
   
  
    ResultSet rs = psmt.execute(); 
  
    while(rs.next()) { 
  
     boolean = true; 
  
    } 
  
   }catch(Exception ex) { 
  
    //Exception title 
  
   }finally { 
  
    return boolean; 
  
   } 
  
  } 
  
} 
  



  Action execute() / Servlet (doGet() or doPost()) Method: 
  
import java.sql.*; 
  
        import java.util.*; 
  
public ActionForward/void execute() { 
  
  //get values of JSP  
  
  String UserName = request.getParemeter("UserName"); 
  
  String PassWord = request.getParemeter("PassWord"); 
  
  
  
  //check  
  
  CheckLogin check = new CheckLogin(); 
  
  Boolean boolean = check.checkLogin(UserName,PassWord); 
  
  
  
  if(boolean) { 
  
   //success 
  
  }else { 
  
   //error 
  
  } 
  
}



A:但是基本的思路都是一样的啊:
1、验证输入的数据是否符合要求(用户名不空、密码不空等此类的)
2、连接数据库
3、按用户名查询密码,然后与界面输入的密码比对是否一致。(这个地方如果要是涉及到密码加密就麻烦点)
4、返回密码比对结果