| | |
JDBC connection
![]() |
•
•
Join Date: Jun 2004
Posts: 126
Reputation:
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:
Java Syntax (Toggle Plain Text)
static final String JDBC_DRIVER = "com.ibm.db2j.jdbc.DB2jDriver"; static final String DATABASE_URL = "jdbc:db2j:books";
It goes on to use it:
Java Syntax (Toggle Plain Text)
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:
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:
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:
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)");
}
![]() |
Similar Threads
- JDBC Connection with MySQL problem "Ubuntu 7.10" (Java)
- possible to initialize jdbc connection within jar. (Java)
- jdbc connection w/ config file (Java)
- JDBC Connection Pooling : Network Adapter could not establish the connection (Java)
- 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
| Thread Tools | Search this Thread |
actuate add android api applet application applications array arrays automation balls bank binary bluetooth business chat class clear client code codesnippet collections component database defaultmethod development dice digit dragging ebook eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health hql html hyper ide idea image infinite int integer invokingapacheantprogrammatically j2me java javame javaprojects jni jpanel julia linux list main map method methods mobile myregfun mysql netbeans nonstatic openjavafx parameter pearl php problem program project recursion repositories scanner scrollbar server set sms sort sorting spamblocker sql sqlserver state storm string sun superclass swing swt thread threads tree windows





