What are the steps for connecting jdbc with mysql - java

I'M NOT ASKING ABOUT THE CODE. I just want to know what are and all the steps involved in the connection other than coding. I'm using j2sdk1.4.0 and MySQL Server 4.1. Am very new to this area.
Thanks in advance

If your code cannot find the class, then that's always going to be an issue with your classpath (and this isn't specific to JDBC in any way). Make sure you have the MySQL JDBC JAR on your classpath at runtime (it's probably called mysql-connector-java-3.0.17-ga.jar; if you have an IDE, it can probably tell you where the class in question lives).

I maybe my advice will not be as useful as that from Vinegar, but to keep it simple you can always do it like this:
First download the'mysql-connector-java.jar' -- I suppose you already have this -- if you're in Linux a yum install mysql-connector-java will suffice.
Compile your code normally
Now when you need to run your app do something like this on the command line:
java -classpath .:“/someplace in your computer/mysql-connector-java.jar” your app
I hope this can help you get started. Good luck!

Related

Add missing driver to legacy project in production

I am migrating a legacy project to a new server. Previously the project used a Oracle DB but now i want it to use Postgress. The queries are simple enough and work the same in Postgres.
However the project is missing a Postgres jdbc-driver. Can i somehow add this dependency sideways to the jar without recompiling?
Can i somehow add this dependency to the jar without recompiling?
It depends.
If you are running the server as java -jar myserver.jar ..., then you will at least need to modify the manifest in the JAR file. Strictly speaking this doesn't entail recompiling, but you do need to explode, modify and repack the JAR file.
If the server uses Class.forName to explicitly load an Oracle Driver class, then you will need to change that code to load the Postgres Driver class instead. (There are other ways to use JDBC that avoid this, but this depends on how your legacy server is implemented.)
If your server uses Oracle specific database classes, or Oracle specific SQL features (or it needs to do the same in the Postgres world) then more extensive changes will be required.
But without actually examining your codebase in detail, we can't predict what is required.
My advice is to replace the Oracle driver JAR with a Postgres driver JAR, and see what happens when you run your server against a Postgres database with the appropriate schemas and data.
But I wouldn't do this "in production". Do it in a test environment. If you can't set up a suitable test environment ... forget it.
And if you don't have the source code for your server, I would forget it too. If anything goes wrong you will most likely need source code to figure out the problem and fix it.

Derby, Java: Trouble with "CREATE_TYPE" statement

I've been messing around with Apache Derby inside Eclipse. I've booted up a Network Server, and I've been working with servlets. In my Eclipse project, I have a class called "User", inside the package "base.pack". I have an SQL script open, and I've been trying to convert User, which implements Serializable, into a custom type. When I run the following lines, everything works fine:
CREATE TYPE CARTEBLANCHE.bee
EXTERNAL NAME 'base.pack.User'
LANGUAGE JAVA
This follows the general format they identify here: http://db.apache.org/derby/docs/10.7/ref/rrefsqljcreatetype.html#rrefsqljcreatetype
Now, when I try to create a table using this new type, I get an error. I run the following line:
CREATE TABLE CARTEBLANCHE.TestTabel (ID INTEGER NOT NULL, NAME CARTEBLANCHE.bee, PRIMARY KEY(ID));
And I receive the following error:
The class 'base.pack.User' for column 'NAME' does not exist or is inaccessible. This can happen if the class is not public.
Now, the class is in fact public, and as I noted before, it does implement Serializable. I don't think I'm stating the package name incorrectly, but I could be wrong. I'm wondering, is this an issue with my classpath? If so, how would you suggest I fix this? I admit that I do not know much about the classpath.
Thank you.
(For reference, I have configured my project build path to include derby.jar, derbyclient.jar, derbytools.jar, and derbynet.jar, and I have put these files into my project's lib folder as well).
As politely as I can, may I suggest that if you are uncomfortable with Java's CLASSPATH notion, then writing your own custom data types in Derby is likely to be a challenging project?
In the specific case you describe here, one issue that will arise is that your custom Java code has to be available not only to your client application, but also to the Derby Network Server, which means you will need to be modifying the server's CLASSPATH as well as your application's CLASSPATH.
It's all possible, it's just not a beginner-level project.
To get started with customizing your Derby Network Server, the first topic involves how you are starting it. Here's an overview of the general process: http://db.apache.org/derby/docs/10.11/adminguide/tadmincbdjhhfd.html
Depending on how precisely you are starting the Derby Network Server, you'll possibly be editing the CLASSPATH settting in the startNetworkServer or startNetworkServer.bat script, or you'll be editing the CLASSPATH setting in your own script that you have written to start the server.
If it's a tool like Eclipse or Netbeans which is starting the Derby Network Server, you'll need to dig into the details of that tool to learn more about how to configure its CLASSPATH.
And if you've written a custom Java application to start the Derby Network Server (e.g., as described here: http://db.apache.org/derby/docs/10.11/adminguide/tadminconfig814963.html) then you'd be configuring the CLASSPATH of your custom application.
Regardless, as a basic step, you're going to want to be deploying your custom Java extension classes in the Derby Network Server's classpath, which means you'll want to build them into a .jar file and put that .jar file somewhere that the Derby Network Server has access to, and you'll want to make that build-a-jar-and-copy-it-to-the-right-location process straightforward, so you should integrate it into whatever build tool you're using (Apache Ant?).
And, you'll need to consider Java security policy, because the default security policy will prevent you from trivially loading custom Java classes into your Derby Network Server as that would seem like a malware attack and the Derby Network Server is going to try to prevent that. So study this section of the Security manual: http://db.apache.org/derby/docs/10.11/security/tsecnetservrun.html

Torquebox will not load with Oracle

I have a Rails 3.2.13 application written in JRuby (1.7.12). I'm developing on Mac OS X 10.10 (though the problem was also evident on 10.9). I have both JDK 1.6 and 1.7 installed. I'm serving the application via torquebox 3.1.1.
If I start the application connecting to my local postgres database, I have no issues. But I need to debug a problem that requires connecting to Oracle. I then switch my adapter to oracle-enhanced and making sure I have ojdbc6.jar and ojdbc7.jar in $JRUBY_HOME/lib. When I start the application via torquebox, I get errors like this:
14:47:58,242 ERROR [org.torquebox.core.runtime] (pool-8-thread-1) Error during execution: ENV['RAILS_ROOT']=RACK_ROOT
ENV['RAILS_ENV']=RACK_ENV
require %q(org/torquebox/web/rails/boot)
: org.jruby.exceptions.RaiseException: (NoMethodError) undefined method `blame_file!' forjava.lang.StackOverflowError:Java::JavaLang::StackOverflowError
I realize the blame_file! error is a separate thing. The StackOverflowError that's the cause of that error is what I care about. It's getting into a failing loop when trying to load necessary files, like rails/boot in this case.
However! When I run "rails console" it works fine and I can load data from the database.
I'm convinced as a result of being able to load the console that this is a problem with torquebox somehow, but I don't know what, nor how to fix it.
Is there any helps anyone can give me? Has anyone seen something like this before? Thanks.
(Edit, I found a locally cached copy of ojdbc7.jar in the app, which explains my original errors with 1.6. I've edited this post to reflect the current situation.)
Try looking at https://github.com/jruby/jruby/issues/976 to work around the blame_file problem and then hopefully you'll get a more useful error message.
Maybe then you'll get something be able to diagnose something similar to:
Error while loading java Class in Jruby application
I got some help on irc.freenode.net/#torquebox. After some confusion about the stack output vs. my stack size, it was suggested that I scale that back some. I set it from 4096m (yes, I realize now that that's ridiculous) to 2048k, and it works.
We don't know why Java crapped out in this specific way, but the end result is that with a smaller stack size, it works now.

java service could not be initialized (debugging help)

I have a jar package that I wrote using netbeans. This package is called from other java file. The jar calls a webservice and is supposed to do something with it. Now everything works fine locally. I compiled the files and locally and uploaded them to the server and when I run it, I get the "Service could not be initialized".I am not sure how to debug this. I am pretty new to java. What is the best approach here to solve the issue?
I would start by implementing logging (I like log4J) in your project so you can get some better details of what is actually going wrong. This will be very useful not only now but in the future as things go wrong (they inevitably will) you will be able to solve them based on how good of a job you did logging what is happening in your application. Right now it sounds like an error is bubbling up and you're not getting much detail about it. Logging should help you determine not only what went wrong but where it happened and what the application was doing at the time.
Try this short introduction to log4j to get started.

How do I use interact with MySQL using Java?

I'm assuming I need to use Connector\J and JDBC to achieve this, but I can't seem to 'install' Connector\J using the CLASSPATH thing. How do I do that ? I use the IntelliJ IDE if thats relevant.
I'm looking for a way to talk to a mysql database and execute and print out a few basic queries but I'm not getting anywhere because I can't even talk to the database.
Any help is appreciated.
You might follow an example such as this one:
http://www.kitebird.com/articles/jdbc.html
But, you might want to consider using Hibernate unless you're doing only a couple basic queries.
See Using MySQL with Java -- the first link from Google.
I like this tutorial because it's very detailed and thorough. Pretty much every step from downloading and installing MySQL and Connector/J to writing and running queries is covered.
Which version of IntelliJ?
You need to add the MySQL Connector-J JAR to your project dependencies.
Open your IntelliJ settings (the "wrench" in the top menu bar), go to "Libraries", click on "Attach Classes", and add the MySQL Connector-J JAR that you downloaded above. That puts it in your CLASSPATH.
If you still have problems, it means you don't have the driver class name right. It should be com.mysql.jdbc.Driver.

Categories

Resources