CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2013
    Posts
    1

    Creating a DataSource

    Hello, can you help me?

    I have already read many tutorials and code examples but I don't manage to create a DataSource in Java!

    I have put the driver into the lib-Folder of my Eclipse workspace and Eclipse knows about that (I added the jar file to my project). The driver has the name hsqldb.jar, the classpath is org.hsqldb.jdbcDriver

    How can I create a DataSource to read and write databases? I think, the second step (dataSource.getConnection()) won't be any problem, but my problem is the first step. Just for your information: The driver is not the problem: With a DriverManager, I don't have any problem; I can create, change and delete (drop) databases without any problem, but with the DataSource, I'm going to become desparate directly at the beginning...

    Can you write my the lines for creating a DataSource? Thank you very much in advance!!!

  2. #2
    Join Date
    Oct 2013
    Posts
    13

    Re: Creating a DataSource

    Re: Creating a DataSource

    Which database is being used by you???
    If you have given the .jar files correctly then now you have to give some details to connect with database like:

    String DRIVER_NAME = "com.mysql.jdbc.Driver"; // for mysql db
    String DB_URL = "jdbc:mysql://localhost:3306/database_name"; //3306 is port number
    String USERID= "root"; //root is default username
    String PASSWORD = "root"; //root is default password

    private Connection getConnection() throws ClassNotFoundException, SQLException
    {
    // Step -1 Load the Database Driver
    Class.forName(DRIVER_NAME);
    // Step -2 Create Connection
    Connection con = DriverManager.getConnection(DB_URL,USERID,PASSWORD);
    return con;
    }

    For other databases (other than MySql) search the drivers name and port number your self.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured