Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. import java.sql.*;
  2. public class DAO {
  3.  
  4.     /**
  5.      * @param args
  6.      */
  7.     /**
  8.      * @param args
  9.      */
  10.     public boolean AbreBanco(){ //pode ser void também, mas eu prefiro boolean, assim podemos controlar melhor o resultado
  11.         // TODO Auto-generated method stub
  12.         Connection conexao = null;
  13.         Statement stmt = null;
  14.         ResultSet rs = null;
  15.         try{
  16.             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  17.             String driver = "jdbc:sqlserver://P10-LAB01-PC05:1433;databaseName=lp2;user=sa;password=fmu;";
  18.             conexao = DriverManager.getConnection(driver);
  19.             stmt = conexao.createStatement();
  20.             rs=stmt.executeQuery("SELECT * FROM dbo.taluno");
  21.             while(rs.next()){
  22.                 System.out.print(rs.getInt("Id")+"\t");
  23.                 System.out.print(rs.getString("Nome")+"\t");
  24.                 System.out.println(rs.getString("email"));
  25.             }
  26.             rs.close();
  27.             stmt.close();
  28.             conexao.close();
  29.  
  30.         }
  31.         catch(Exception ex){
  32.             ex.printStackTrace();
  33.         }
  34.     return AbreBanco;
  35.     }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement