Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How to pass a connection code?
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
- import javax.sql.DataSource;
- public class DisplayUsers {
- ResultSet resultSet = null;
- Statement statement = null;
- Scanner input = new Scanner(System.in);
- DataSource ds = null;
- public void showAll() {
- System.out.println("Search User: ");
- String user = input.nextLine();
- String query = "Select * from user";
- try {
- resultSet = statement.executeQuery(query);
- ResultSetMetaData metadata = resultSet.getMetaData();
- int columns = metadata.getColumnCount();
- while(resultSet.next()){
- for(int i = 1 ; i<=columns;i++){
- System.out.printf("%-8st",resultSet.getObject(i));
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- import java.sql.*;
- public class Jdbc {
- public void dbConn(){
- final String url = "jdbc:mysql://localhost:3306/payroll";
- Connection conn = null;
- try{
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(url,"root","123192");
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
- InitialContext ctx = new InitialContext();
- //Application Server will automatically bind new InitialContext() call to its
- //own context (where your DataSource is located).
- DataSource ds = ctx.lookup("jndi/mydb")
Add Comment
Please, Sign In to add comment