•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 427,361 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,024 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 4438 | Replies: 3
![]() |
•
•
Join Date: Jun 2004
Location: Colorado
Posts: 120
Reputation:
Rep Power: 5
Solved Threads: 1
I need a little help connecting to a database in Java.
The example in the book I have uses the following:
It goes on to use it:
First off the book uses a Cloudscape DB, I'm using mysql. My question is, where does the information for the JDBC_DRIVER and DATABASE_URL come from? Are the drivers included in the Java software or do I need to download it?
Thanks
Ken
The example in the book I have uses the following:
static final String JDBC_DRIVER = "com.ibm.db2j.jdbc.DB2jDriver"; static final String DATABASE_URL = "jdbc:db2j:books";
It goes on to use it:
System.setProperty("db2j.system.home", "C:/mysql");
Class.forName(JDBC_DRIVER);
connect = DriverManager.getConnection(DATABASE_URL);First off the book uses a Cloudscape DB, I'm using mysql. My question is, where does the information for the JDBC_DRIVER and DATABASE_URL come from? Are the drivers included in the Java software or do I need to download it?
Thanks
Ken
•
•
Join Date: Sep 2004
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
Hello Squires,
Creating a JDBC connection can be a bit confusing the first few times through. The values for the JDBC_DRIVER and DATABASE_URL (Which I commonly see referred to as the 'connection string') are dependant on which type of database you're using. Each database vendor that supports JDBC distributes the drivers that make it work. I have yet to use mysql, but I can only assume that they are distributed with the database software. Just in case they aren't, I believe that you can download them here
Creating a JDBC connection can be a bit confusing the first few times through. The values for the JDBC_DRIVER and DATABASE_URL (Which I commonly see referred to as the 'connection string') are dependant on which type of database you're using. Each database vendor that supports JDBC distributes the drivers that make it work. I have yet to use mysql, but I can only assume that they are distributed with the database software. Just in case they aren't, I believe that you can download them here
•
•
Join Date: Sep 2004
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
Sorry about that, I accidentally submitted and then took too long editing, so here's the entire reply.
Hello Squires,
Creating a JDBC connection can be a bit confusing the first few times through. The values for the JDBC_DRIVER and DATABASE_URL (Which I commonly see referred to as the 'connection string') are dependant on which type of database you're using. Each database vendor that supports JDBC distributes the drivers that make it work. I have yet to use mysql, but I can only assume that they are distributed with the database software. Just in case they aren't, I believe that you can download them here.
The first step is usually to add the drivers to your classpath. They are commonly distributed as either a .jar or .zip file, so when you edit your classpath variable, make sure it looks something like $CLASSPATH=.:/path/to/jar/drivers.jar. or on Windows you right click on My computer, click properties, then the advanced tag, then environment variables. Then edit or create your CLASSPATH variable with "C:\path\to\jar\drivers.jar"
Now, as far as where those values for JDBC_DRIVER AND DATABASE_URL come from. The JDBC_DRIVER variable will be completely vendor dependant, and at this stage in the game, your best bet for finding it out would be to type something like "mysql jdbc" into Google and see what comes up. But just for kicks I think it's "com.mysql.jdbc.Driver". What this refers to is the directory structure inside the .jar or .zip file that you added to your CLASSPATH, ending with the class name of responsible for creating the connection. The DATABASE_URL, which I'm much more comfortable calling the "connection string" is also highly vendor dependant, but follows a certain format. The connection string will look like "jdbc:drivertype:databaselocation:connectioninfo". Lets look at the connection string for a couple of different vendors.
Oracle thin client:
jdbc:oracle:thin:@machine_name:port_number:instance_name
which would look like
jdbc:oracle:thin:@mycomputernameorIP:1521:TestDB
MySQL:
jdbc:mysql://host_name/dbname
which would look like:
jdbc:mysql://mycomputernameorIP/TestDB.
You can also supply the username/password connection parameters as part of the connection string. This would make the above string look like this.
jdbc:mysql://mycomputernameorIP/TestDB?user=scott&password=tiger
but I usually just pass that information as parameters to the DriverManager.getConnection method.
Whenever you're going to be using JDBC the best advice I could give you is to just look online for this info and as long as you're not using a DB that you wrote yourself you're almost guaranteed to find everything you're looking for in the first few tries. I hope this helps, happy coding.
Hello Squires,
Creating a JDBC connection can be a bit confusing the first few times through. The values for the JDBC_DRIVER and DATABASE_URL (Which I commonly see referred to as the 'connection string') are dependant on which type of database you're using. Each database vendor that supports JDBC distributes the drivers that make it work. I have yet to use mysql, but I can only assume that they are distributed with the database software. Just in case they aren't, I believe that you can download them here.
The first step is usually to add the drivers to your classpath. They are commonly distributed as either a .jar or .zip file, so when you edit your classpath variable, make sure it looks something like $CLASSPATH=.:/path/to/jar/drivers.jar. or on Windows you right click on My computer, click properties, then the advanced tag, then environment variables. Then edit or create your CLASSPATH variable with "C:\path\to\jar\drivers.jar"
Now, as far as where those values for JDBC_DRIVER AND DATABASE_URL come from. The JDBC_DRIVER variable will be completely vendor dependant, and at this stage in the game, your best bet for finding it out would be to type something like "mysql jdbc" into Google and see what comes up. But just for kicks I think it's "com.mysql.jdbc.Driver". What this refers to is the directory structure inside the .jar or .zip file that you added to your CLASSPATH, ending with the class name of responsible for creating the connection. The DATABASE_URL, which I'm much more comfortable calling the "connection string" is also highly vendor dependant, but follows a certain format. The connection string will look like "jdbc:drivertype:databaselocation:connectioninfo". Lets look at the connection string for a couple of different vendors.
Oracle thin client:
jdbc:oracle:thin:@machine_name:port_number:instance_name
which would look like
jdbc:oracle:thin:@mycomputernameorIP:1521:TestDB
MySQL:
jdbc:mysql://host_name/dbname
which would look like:
jdbc:mysql://mycomputernameorIP/TestDB.
You can also supply the username/password connection parameters as part of the connection string. This would make the above string look like this.
jdbc:mysql://mycomputernameorIP/TestDB?user=scott&password=tiger
but I usually just pass that information as parameters to the DriverManager.getConnection method.
Whenever you're going to be using JDBC the best advice I could give you is to just look online for this info and as long as you're not using a DB that you wrote yourself you're almost guaranteed to find everything you're looking for in the first few tries. I hope this helps, happy coding.
•
•
Join Date: Sep 2004
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
If u use Microsoft Oracle, Microsoft Access or u using sun Microsoft Products means then use the following instruction,
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); :cry:
//if Oracle means
Connection con=DriverManager.getParameter("Jdbc:odbc:"+db);
//db is ur database name
//this can connect ur database
//for using query
Statement st=con.executeQuery("insert into tablename value(xxxxxx)");
}
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); :cry:
//if Oracle means
Connection con=DriverManager.getParameter("Jdbc:odbc:"+db);
//db is ur database name
//this can connect ur database
//for using query
Statement st=con.executeQuery("insert into tablename value(xxxxxx)");
}
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- problem in installing jdbc... (Java)
- hi,urgent-jdbc doubt (MS SQL)
- Can´t make JDBC connection to MS SQL from home? (MS SQL)
- JDBC connection (Java)
Other Threads in the Java Forum
- Previous Thread: who wants to be a millionare!!!!!!!!
- Next Thread: JAVA help ACTIONLISTENERS


Linear Mode