Guest User

Untitled

a guest
Aug 10th, 2018
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. How to pass a connection code?
  2. import java.sql.Connection;
  3. import java.sql.ResultSet;
  4. import java.sql.ResultSetMetaData;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.Scanner;
  8.  
  9. import javax.sql.DataSource;
  10.  
  11. public class DisplayUsers {
  12.  
  13. ResultSet resultSet = null;
  14. Statement statement = null;
  15. Scanner input = new Scanner(System.in);
  16. DataSource ds = null;
  17.  
  18.  
  19.  
  20. public void showAll() {
  21.  
  22. System.out.println("Search User: ");
  23. String user = input.nextLine();
  24.  
  25. String query = "Select * from user";
  26. try {
  27. resultSet = statement.executeQuery(query);
  28. ResultSetMetaData metadata = resultSet.getMetaData();
  29. int columns = metadata.getColumnCount();
  30. while(resultSet.next()){
  31. for(int i = 1 ; i<=columns;i++){
  32. System.out.printf("%-8st",resultSet.getObject(i));
  33. }
  34. }
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40.  
  41. import java.sql.*;
  42.  
  43. public class Jdbc {
  44.  
  45. public void dbConn(){
  46. final String url = "jdbc:mysql://localhost:3306/payroll";
  47. Connection conn = null;
  48. try{
  49. Class.forName("com.mysql.jdbc.Driver");
  50. conn = DriverManager.getConnection(url,"root","123192");
  51. }catch(Exception e){
  52. e.printStackTrace();
  53. }
  54. }
  55. }
  56.  
  57. InitialContext ctx = new InitialContext();
  58. //Application Server will automatically bind new InitialContext() call to its
  59. //own context (where your DataSource is located).
  60. DataSource ds = ctx.lookup("jndi/mydb")
Add Comment
Please, Sign In to add comment