计算机二级

3773考试网计算机等级考试计算机二级正文

jdbc连接类

来源:fjzsksw.com 2010-4-1 10:16:05

 

  import java.sql.Connection;

  import java.sql.DriverManager;

  import java.sql.ResultSet;

  import java.sql.SQLException;

  import java.sql.Statement;

  public class TestJDBC {

  public static void main(String[] args) {

  Connection conn = null;

  Statement stmt = null;

  ResultSet rs = null;

  try{

  Class.forName("com.mysql.jdbc.Driver");

  conn = DriverManager.getConnection("jdbc:mysql://localhost/datebasename?user=username&password=password");

  stmt = conn.createStatement();

  rs = stmt.executeQuery("select * from datrbasename");

  while(rs.next()){

  //数据库操作

  }

  }catch(ClassNotFoundException e){

  e.printStackTrace();

  }catch(SQLException e){

  e.printStackTrace();

  }finally{

  try{

  if(rs!=null)

  rs.close();

  if(stmt!=null)

  stmt.close();

  if(conn!=null)

  conn.close();

  }catch(SQLException e){

  e.printStackTrace();

  }

  }

  }

  }

 

触屏版 电脑版
3773考试网 琼ICP备12003406号-1