Results 1 to 2 of 2
  1. #1
    perdoname is offline Member
    Join Date
    Apr 2008
    Posts
    1
    Rep Power
    0

    Exclamation ensure connectivity

    Hello,

    I need your assistance about the following code if there is anything needed to change as to ensure the connectivity of the database with its tables


    Thanks in advance for any help !


    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class createTable 
    {
    	public static void main(String[] args) throws SQLException 
    	{
    		DB db = new DB();
    		Connection conn=db.dbConnect(
    				"jdbc:qed:mynewdb://localhost:1925", "p");
    		db.createTables(conn);
    
    
    		try {
    			Class.forName("com.quadcap.jdbc.JdbcDriver");
    		} catch (ClassNotFoundException e1) {
    			e1.printStackTrace();
    		}
    		java.util.Properties p = new java.util.Properties();
    		p.setProperty("create", "true");
    		conn = java.sql.DriverManager.getConnection("jdbc:qed:mynewdb", p);
    		try{
    			Statement st = conn.createStatement();
    			BufferedReader bf = new BufferedReader
    			(new InputStreamReader(System.in));
    			System.out.println("Enter Database name:");
    			String database = bf.readLine();
    			st.executeUpdate("CREATE DATABASE "+database);
    		}
    		catch (SQLException s){
    			System.out.println("SQL statement is not executed!");
    		}
    		catch (Exception e){
    			e.printStackTrace();
    		}
    
    	}
    }
    
    class DB
    {
    	public DB() {}
    
    	public Connection dbConnect(String db_connect_string,
    			String db_userid)
    	{
    		try
    		{
    			Class.forName("com.quadcap.jdbc.JdbcDriver");
    			java.util.Properties p = new java.util.Properties();
    			p.setProperty("create", "true");
    			java.sql.Connection conn =
    				java.sql.DriverManager.getConnection("jdbc:qed:mynewdb", p);
    			return conn;
    
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    			return null;
    		}
    	} 
    
    
    	//Employee table
    	public void createTables(Connection conn)
    	{
    
    		String query;
    		Statement stmt = null;
    
    		try
    		{
    
    			query="create table employees " +
    			"(name       varchar(32), " +
    			"id          varchar(50), " +
    			"telephoneNo varchar(50), " +
    			"email       varchar(50), " +
    			"JOB_ID      integer,     " +
    			"foreign key (JOB_ID) references Job_roles (JOB_ID))";
    
    
    
    
    			stmt.executeUpdate("INSERT INTO employees " +
    			"VALUES ('John Doe', '1', '5555999944', 'Something@mail.com')");
    
    			stmt.executeUpdate("INSERT INTO employees " +
    			"VALUES ('Neil Mayhem', '2', '555588888', 'sth@mail.com')");
    
    			stmt.executeUpdate("INSERT INTO employees " +
    			"VALUES ('Alice Lovelace', '3', '6666999944', 'alice@mail.com')");
    
    			stmt.executeUpdate("INSERT INTO employees " +
    			"VALUES ('Jay Something', '4', '666669999', 'mail@mail.com')");
    
    			stmt.executeUpdate("INSERT INTO employees " +
    			"VALUES ('Terry Something', '5', '622211111', 'mail20@mail.com')");
    
    
    			stmt = conn.createStatement();
    			stmt.executeUpdate(query);
    			stmt.close();
    			conn.close();
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    		}
    	} //createTables
    
    
    
    	public void createJobRoles(Connection conn)
    	{
    
    		String query;
    		Statement stmt = null;
    
    		try
    		{
    
    			query="create table Job_roles " +
    			"(JOB_ID INTEGER NOT NULL, " +
    			"job_title varchar(32), " +
    			"salary_class varchar(50), " +
    			"job_description varchar(50), " +
    			"primary key(JOB_ID))";
    
    
    			stmt.executeUpdate("INSERT INTO Job_roles " +
    			"VALUES (1, 'Garden Worker', '4', 'Cultivates and cares for plants')");
    
    			stmt.executeUpdate("INSERT INTO Job_roles " +
    			"VALUES (2, 'Security Guard', '4', 'Takes care of security')");
    
    			stmt.executeUpdate("INSERT INTO Job_roles " +
    			"VALUES (3, 'Stage Hypnotist', '2', 'Hypnotizes people on stage')");
    
    			stmt.executeUpdate("INSERT INTO Job_roles " +
    			"VALUES (4, 'Amusement Park Guide', '3', 'Guides people through an amusement park')");
    
    
    
    			stmt = conn.createStatement();
    			stmt.executeUpdate(query);
    			stmt.close();
    			conn.close();
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    
    
    	public void printFields()
    	{
    		String query;
    		Statement stmt;
    		try {
    			query = "SELECT employees.name, job_roles.salary_class FROM employees INNER JOIN job_roles on employees.job_id = job_roles.job_id";
    
    			Connection conn = null;
    			stmt = conn.createStatement();
    			ResultSet rs = stmt.executeQuery(query);
    			while (rs.next()) {
    				//print query
    				System.out.printf("Salary of %s is %s\n", rs.getString(1), rs.getObject(2).toString());
    			}
    		}
    		catch (Exception e) {
    			e.printStackTrace();
    		}
    
    	}
    
    
    	public void addColumn()
    	{
    		String query;
    		Statement stmt;
    		Connection conn = null;
    
    
    		try {
    			query= "ALTER TABLE employees ADD JOB_ID";
    			stmt = conn.createStatement();
    			ResultSet rs = stmt.executeQuery(query);
    
    		}
    		catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    
    
    	public void EmployeeNameSearch(String[] args) throws SQLException
    	{
    		List names =  Arrays.asList(args);
    
    		List not = new ArrayList();
    
    		//Collections.sort(names); 
    
    		PreparedStatement ps = null;
    
    		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    		while (true) {
    			try {
    				String option = stdin.readLine();
    			} catch (IOException e1) {
    				e1.printStackTrace();
    			}
    
    			for (Object name : names) {
    				try {
    					ps.setString(1, name.toString());
    				} catch (SQLException e) {
    					e.printStackTrace();
    				}
    				ResultSet rs = null;
    				try {
    					rs = ps.executeQuery();
    				} catch (SQLException e) {
    					e.printStackTrace();
    				}
    				if (rs.next()) {
    					// name in dba
    				}
    				else
    				{
    					System.out.println(name + "not in db!");
    					not.add(name);
    
    				}
    			}
    		}
    	}
    
    };

  2. #2
    rico16135 is offline Member
    Join Date
    Apr 2008
    Posts
    28
    Rep Power
    0

    Default

    JDBC is recommending that the DataSource interface should be used to create database connection objects instead of using the DriverManager class.

    Implementations of the DataSource Interface

Similar Threads

  1. database connectivity
    By rilwanfit in forum Networking
    Replies: 1
    Last Post: 03-13-2009, 08:54 PM
  2. Database connectivity in Jave struts with Hibernate
    By sanjay_sharma77 in forum Advanced Java
    Replies: 1
    Last Post: 02-02-2008, 09:41 PM
  3. Java Database Connectivity
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 01-14-2008, 02:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •