Results 1 to 5 of 5

Thread: How to connect advanced java with sql server 2000

  1. #1
    Join Date
    Dec 2010
    Posts
    87

    How to connect advanced java with sql server 2000

    I’m making a project using front-end as a advanced java and back-end as a sql server 2000. I had made project using core java and ms- access, but I’m new in advanced java and I don’t know the connectivity between advanced java and sql server 2000. I want a code which is used to connect my front-end with back-end. Please give me the code to connect. Please help me.

  2. #2
    Join Date
    Nov 2009
    Posts
    1,269

    Re: How to connect advanced java with sql server 2000

    core java and advanced java connection is different, because the driver which use in core java is different from advanced java. For connecting with sql server 2000 You would need the connector J.
    This is a simple example...

    The minmum imports which is needed for a simple application could be:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;



    You would then have to create a connection so place this within the class

    private Connection sqlConnection;
    To test if the driver is properly installed, you could add this function. This is for debugging purposes...

    public void testDriver() throws Exception {
    System.out.println("Initializing Server... ");
    try {
    Class.forName("org.gjt.mm.mysql.Driver");
    System.out.println(" Driver Found.");
    } catch (ClassNotFoundException e) {
    System.out.println(" Driver Not Found, exiting..");
    throw (e);
    }
    }



    And to connect to the database you do this, so then we have a returned the connection from the databases...:

    public Connection getConnection(String host, String userDB, String passDB, String database) throws Exception {
    String url = "";
    try {
    url = "jdbc:mysql://" + host + "/" + database;

    Connection con = DriverManager.getConnection(url, userDB, passDB);
    System.out.println(" Database connection established to " + url+ ".");

    return con;
    } catch (java.sql.SQLException e) {
    System.out.println(" Connection couldn't be established to "+ url);
    throw (e);
    }
    }

    I believe that it is the best way to do the connection.

  3. #3
    Join Date
    Nov 2008
    Posts
    1,185

    Re: How to connect advanced java with sql server 2000

    First you have to create a package named: demo.mysql.java.connectivity
    Now create a Java source file named ConnectToMySQLSimple.java
    package demo.mysql.java.connectivity;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    public class ConnectToMySQLSimple {
    public static void main(String args[]) {
    Connection con = null;
    try {
    Class.forName(“com.mysql.jdbc.Driver”);
    String url = “jdbc:mysql://localhost:3306/mytestdb”;
    // Get a connection to the database for a
    // user named auser with the password
    // drowssap, which is password spelled
    // backwards.
    con = DriverManager.getConnection(url, “root”,”mypass”);
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery(“select * from mytable”);
    while (rs.next()) {
    System.out.println(“Id ” + rs.getString(1));
    System.out.println(“Val ” + rs.getString(2));
    }
    } catch (Exception e) {
    e.printStackTrace();
    System.out.println(“Exception: ” + e.getMessage());
    } finally {
    try {
    if (con != null)
    con.close();
    } catch (SQLException e) {

    }
    }
    }
    }
    Now compile this code and run. This will make your front-end connection with back-end.

  4. #4
    Join Date
    Nov 2009
    Posts
    1,035

    Re: How to connect advanced java with sql server 2000

    First you have to set classpath variable. add mysql-connector-java-5.1.5-bin.jar to the CLASSPATH.
    Then you should look at the code ->
    Class.forName(“com.mysql.jdbc.Driver”);
    We know that this line returns the Class object associated with the class or interface with the given string name, using the given class loader, which is mysql is this example.
    String url = “jdbc:mysql://localhost:3306/mytestdb”;
    Now we will define a JDBC URL. Now MySQL Server runs on port 3306 by default. If your server runs on another port other that 3306 then you should put that port instead of 3306. Now localhost is for your local machine. If your mysql server runs on another host then you should put that in place of localhost. Like jdbc:127.0.0.1:3306/mytestdb
    And lastly mytestdb is the database we want to connect using this code.
    And now we will gett he connection by
    con = DriverManager.getConnection(url, “root”,”mypass”);
    where root is my mysql server user name and mypass is my mysql server password for root.
    And now,
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery(“select * from mytable”);
    here we create a statement object and executes the query which will return a Resultset object.
    Now if I create the table : mytable as,
    CREATE TABLE mytable (id INT(4) AUTO_INCREMENT, Value VARCHAR(20));
    and then insert some rows there,
    INSERT INTO mytable(Value) VALUES(“Root”);
    INSERT INTO mytable(Value) VALUES(“UBK”);
    INSERT INTO mytable(Value) VALUES(“MySQL”);
    Now if you execute the source java code given earlier, then it will return all the ID and Values stored in mytable of Database mytestdb, using the portion:
    while (rs.next()) {
    System.out.println(“Id ” + rs.getString(1));
    System.out.println(“Val ” + rs.getString(2));
    }
    Try this code. Hope it make your connection.

  5. #5
    Join Date
    May 2008
    Posts
    859

    Re: How to connect advanced java with sql server 2000

    following are the example of java connectivity you can use this code for making connection with your database.:

    package demo.mysql.java.connectivity;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    public class DemoMySQLJavaAdvanced {
    public static void main(String args[]) {
    try {
    Statement stmt;
    ResultSet rs;
    // Register the JDBC driver for MySQL.
    Class.forName(“com.mysql.jdbc.Driver”);
    // Define URL of database server for
    // database named JunkDB on the localhost
    // with the default port number 3306.
    String url = “jdbc:mysql://localhost:3306/mytestdb”;
    // Get a connection to the database for a
    // user named auser with the password
    // drowssap, which is password spelled
    // backwards.
    Connection con = DriverManager.getConnection(url, “root”,”mypass”);
    // Display URL and connection information
    System.out.println(“URL: ” + url);
    System.out.println(“Connection: ” + con);
    // Get a Statement object
    stmt = con.createStatement();
    // As a precaution, delete myTable if it
    // already exists as residue from a
    // previous run. Otherwise, if the table
    // already exists and an attempt is made
    // to create it, an exception will be
    // thrown.

    //Query to drop table;
    try {
    stmt.executeUpdate(“DROP TABLE myTable”);
    } catch (Exception e) {
    System.out.print(e);
    System.out.println(“No existing table to delete”);
    }// end catch

    // Create a table in the database named
    // myTable.

    stmt.executeUpdate(“CREATE TABLE myTable(test_id int,”+ “test_val char(15) not null)”);

    // Insert some values into the table
    stmt.executeUpdate(“INSERT INTO myTable(test_id, “
    + “test_val) VALUES(1,’One’)”);
    stmt.executeUpdate(“INSERT INTO myTable(test_id, “
    + “test_val) VALUES(2,’Two’)”);
    stmt.executeUpdate(“INSERT INTO myTable(test_id, “
    + “test_val) VALUES(3,’Three’)”);
    stmt.executeUpdate(“INSERT INTO myTable(test_id, “
    + “test_val) VALUES(4,’Four’)”);
    stmt.executeUpdate(“INSERT INTO myTable(test_id, “
    + “test_val) VALUES(5,’Five’)”);
    // Get another statement object initialized
    // as shown.
    stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
    ResultSet.CONCUR_READ_ONLY);
    // Query the database, storing the result
    // in an object of type ResultSet
    rs = stmt.executeQuery(“SELECT * “
    + “from myTable ORDER BY test_id”);
    // Use the methods of class ResultSet in a
    // loop to display all of the data in the
    // database.
    System.out.println(“Display all results:”);
    while (rs.next()) {
    int theInt = rs.getInt(“test_id”);
    String str = rs.getString(“test_val”);
    System.out.println(“\ttest_id= ” + theInt + “\tstr = ” + str);
    }// end while loop
    // Display the data in a specific row using
    // the rs.absolute method.
    System.out.println(“Display row number 2:”);
    if (rs.absolute(2)) {
    int theInt = rs.getInt(“test_id”);
    String str = rs.getString(“test_val”);
    System.out.println(“\ttest_id= ” + theInt + “\tstr = ” + str);
    }// end if
    // Delete the table and close the connection
    // to the database
    //stmt.executeUpdate(“DROP TABLE myTable”);
    con.close();
    } catch (Exception e) {
    e.printStackTrace();
    }//end catch
    }//end main
    }//end class DemoMySQLJavaAdvanced
    I think this will help you to get your java connectivity.

Similar Threads

  1. how to connect core java with sql server
    By Mustafa k in forum Software Development
    Replies: 5
    Last Post: 05-01-2011, 11:04 AM
  2. how to connect advanced java with oracle
    By Mast Maula in forum Software Development
    Replies: 5
    Last Post: 05-01-2011, 11:01 AM
  3. Advanced Cache Objects in Java
    By StudyBoy in forum Tips & Tweaks
    Replies: 3
    Last Post: 31-07-2010, 03:41 PM
  4. Comparison between core java and advanced java
    By Prashobh Mallu in forum Software Development
    Replies: 5
    Last Post: 19-01-2010, 10:57 AM
  5. Advanced Server Domain - Joining Windows 7 To Windows 2000
    By Ameeryan in forum Operating Systems
    Replies: 3
    Last Post: 03-12-2009, 12:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,562,642.91352 seconds with 17 queries