database connectivity? - java

How to establish SQL 2005 database connectivity with Java application. Actually I don't know how to set path for JDBC. Any help would be great.

Download the jdbc driver from http://www.microsoft.com/sqlserver/2005/en/us/java-database-connectivity.aspx.
You can either download the Windows or Unix version. It does not really matter which one you use.
If you chosed the Windows version, run the downloaded exe file; this will create a directory called Microsoft SQL Server 2005 JDBC Driver in the directory you downloaded the file to.
Add the file Microsoft SQL Server 2005 JDBC Driver\sqljdbc_1.2\enu\sqljdbc.jar to your classpath (either using the -cp option of java or, if you are using an application server, by putting it in the appropriate directory).
Set your connection. Basically, the information required for this are:
the driver class name: com.microsoft.sqlserver.jdbc.SQLServerDriver
the connection url: jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
Check Connecting to SQL Server with the JDBC Driver for more details and/or the resources available in the help directory (sample code available in help/samples/connections/ConnectURL.java).

This is a HOWTO from Microsoft on where to get the driver and how to configure it.
Briefly, download the .jar from Microsoft, reference it in your classpath and use:
String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
"databaseName=AdventureWorks;user=MyUserName;password=*****;";
Connection con = DriverManager.getConnection(connectionUrl);
substituting the relevant info.

This link will help you with this
jdbc connection mssql
Code Snippet
private java.sql.Connection getConnection(){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
if(con!=null) System.out.println("Connection Successful!");
}catch(Exception e){
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
return con;
}
private String getConnectionUrl(){
return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
}

Your actual problem is thus that you don't know what to do with the phenomenon "classpath".
Actually, the classpath is kind of a collection of disk file system paths which points to the whole .jar file(s) and/or to some root folder with .class file(s) where the Java Virtual Machine should lookup for classes to be imported and loaded.
You can specify the classpath during compile and runtime using the -classpath or -cp argument of javac.exe and java.exe. The -cp is just a shorthand, it does nothing different. Then you have the mysterious %CLASSPATH% environment variable which you should just entirely forget. It is a poor thing which was intented to make starters easy to manage the classpath, but at end it just confused them more.
As you're using Class#forName() to load the driver, you only need to have it in the classpath during runtime, not during compiletime. So here's a basic example how to execute it:
java -cp .;c:/path/to/mssql-jdbc-driver.jar com.example.YourClass
You see, the classpath exist of two parts, the . which represents the current working directory and the c:/path/to/mssql-jdbc-driver.jar which should be the absolute path to the JAR file. The ; is just a path separator (in Windows; in Unix and clones it should be a colon :). Note: if a path contains spaces, e.g. c:/spacy path to/file.jar, then you need to wrap the individual path with doublequotes.
If you're using an IDE such as Eclipse, then normal practice is that you create a folder in the project where in you can drop all of those 3rd party JAR files which are required by the project. Create a project folder called lib, drop the JDBC driver in there and rightclick project > Properties > Java Build Path > Libraries > Add JARs > Select the JAR file which you dropped in project's lib > OK.
That should be it. Hope this helps.

Related

In Jmeter Cannot create PoolableConnectionFactory

I am trying to run select query in jmeter. I am getting this response in listners "Cannot create PoolableConnectionFactory" I have attached my myphpadmin page which I use to create db. Thanks in advance.
.png
JDBC jar should be in lib folder:
Fully qualified name of driver class. (Must be in JMeter's classpath - easiest to copy .jar file into JMeter's /lib directory).
I assume you use your jar for MySQL DB:
The list of the validation queries can be configured with jdbc.config.jdbc.driver.class property and are by default:
MySQL
com.mysql.jdbc.Driver
You need to put the .jar into the "lib" folder of your JMeter installation (or other folder which is in JMeter Classpath
You need to restart JMeter to pick the .jar
Check out MySQL Database and JMeter - How to Test Your Connection for more information
On JMeter side check:
driver of MySQL (correct version) is in lib folder. The Jar not a zip.
The URL format is correct based on this document, there seems to be a space before database name in your URL
Login / password are also correct, check for strange characters or bad copy/paste
If all those are ok, then you're most probably facing same issues as the ones described here:
https://stackoverflow.com/a/2985169/460802

How do I solve the: Failed to load the sqljdbc_auth.dll cause is not in the java path while working in Jboss/SQL Server?

I'm new in stack overflow, I'm new to web programming.
So, I'm making a web app using JSP/servlets and I'm using java 6/jboss server.
When I try to connect to the server (I'm using the windows authentication, this means integratedSecurity=true in the string path) throws me the exception.
I already tried to put the DLL in the bin folder in the JBoss path, in system 32, and nothing seems to work. I'm using NetBeans, by the way.
Thanks for any help.
You've not added the path where sqljdbc_auth.dll is present. Find out in the system where the DLL is and add that to your classpath.
And if that also doesn't work, add the folder where the DLL is present (I'm assuming \Microsoft SQL Server JDBC Driver 3.0\sqljdbc_3.0\enu\auth\x86) to your PATH variable.
1) Download the JDBC Driver here.
2) unzip the file and go to sqljdbc_version\fra\auth\x86 or \x64
3) copy the sqljdbc_auth.dll to C:\Program Files\Java\jre_Version\bin
4) Finally restart eclipse
to resolve this problem try to change the name of dll files like is required in IDE

Connect to a SQlite file inside a .jar file with JDBC? [duplicate]

I have created a Swing application that uses SQLite as a local database. The database file is located in project's root directory.
Project/DatabaseFile
The application runs fine on Eclipse, but when I run the packaged executable Jar, I get the following error:
No such table : table1
This means that the database is not reachable. When I examined the contents of the resulting JAR file, the database file was not there anymore.
In the code, I've linked the database as follows:
jdbc:sqlite:DatabaseFile
My question is, how to include the SQLite database in the executable Jar?
EDIT
When I placed the DB file in the source Folder Project/src/DatabaseFile and changed the path to jdbc:sqlite:src/DatabaseFile, it worked on Eclipse but again when running the Jar file as java -jar Project.jar. It said:
path to 'src/DatabaseFile': 'C:\Users\name\src' does not exist
I think I need to specify a relative path for the database.
EDIT
This is how I connect to the database:
public Connection getConnection(){
try{
Class.forName("org.sqlite.JDBC").newInstance();
con = DriverManager.getConnection("jdbc:sqlite:src/DatabaseFile");
} catch (Exception e) {
Log.fatal("Méthode: getConnection() | Class : SQLiteConnection | msg system : " + e.getMessage());
}
return con;
}
What library are you using for SQLite?
I did a search based on the connection URI you indicated and found this one. In the documentation it says:
2009 May 19th: sqlite-jdbc-3.6.14.1 released. This version supports "jdbc:sqlite::resource:" syntax to access read-only DB files contained in JAR archives, or external resources specified via URL, local files address etc. (see also the detailes)
If that is the driver you are using, then I would suggest the following connection URI:
"jdbc:sqlite::resource:DatabaseFile"
The key is that since your database is in a jar file, it can not be access as a file with FileInputStream. Instead it must be accessed through the JVM's support for it (namely with Class.getResource() or Class.getResourceAsStream()). Do note that resources contained within jar files are read-only. You won't be able to save any changes to your database.
I have found two different ways to name the filepath depending on how you are trying to access it. Assuming you are accessing the db is located in /yourproject/resource/ or /yourproject/bin/resource ( havent narrowed it down, mine is in both and I'm happy with it) you should use this as your path:
//Eclipse test path
String url = "jdbc:sqlite:resource/mydb.db";
or
//runnable jar path
String url = "jdbc:sqlite::resource:mydb.db";
then
mysqlitedatasource.setUrl(url);
Your way also works... by putting the db in /src

ClassNotFoundException: con.mysql.jdbc.Driver

Super new to JSP and just trying to access mysql
public static Connection connect(){
try{
Class.forName("con.mysql.jdbc.Driver").newInstance();
return DriverManager.getConnection("jdbc:mysql://localhost/db_name", "root", "******");
}catch(Exception e){
throw new Error(e);
}
I continue to get this error: ClassNotFoundException: con.mysql.jdbc.Driver
I understand that I need to change the "classpath" and/or that I need to move the mysql-connector jar file to tomcat7/lib but I have 2 problems:
I have absolutely no idea what the classpath is and how to change it using ubuntu and dreamweaver.
my tomcat7 folder doesn't have a lib directory. I don't know where to put the jar file.
Assuming that you have the jar on your classpath, I think that yuor problem is a misspelt fully qualified classname: con.mysql.jdbc.Driver should be com.mysql.jdbc.Driver
This seems to be a J2EE application. Within the project should be WEB-INF directory. Within that directory, create (if necessary) a lib directory and place the mysql-connector.jar file within lib. This will cause the driver to be placed on the runtime classpath.
Also as Romski pointed out change the class name to com.mysql.jdbc.Driver, however I'm not even sure this code is necessary with newer version of the driver and JDBC. If your using Tomcat7 the JDK is most likely recent enough to not require this code. See: https://stackoverflow.com/a/12933246/714969

Sqlite JDBC in Java Server Page

I am trying to use Sqlite on a web page over Java.
I am getting the following error:
java.lang.NoClassDefFoundError: org/sqlite/JDBC
model.DataBase.<init>(DataBase.java:35)
I've tried the two alternatives:
this.conn = new org.sqlite.JDBC().connect("jdbc:sqlite:" + file, new Properties());
this.stm = this.conn.createStatement();
and
Class.forName("org.sqlite.JDBC");
this.conn = DriverManager.getConnection("jdbc:sqlite:" + file);
this.stm = this.conn.createStatement();
I am using Eclipse and I've configured the jar file in the build path, also, when running as a desktop application it works fine.
Just having the sqllite.jar in your build path is not enough. If you are running it as a web application place the jar in the /WEB-INF/lib folder as well.
You can tell eclipse to export that file as well.
I know you said that you've included the jar file in your build path, but if it's not also in the $CLASSPATH of the server you're running under, then you'll end up with this exception. If the Java Classloader can't find the class inside a jar file that it knows about, you'll get NoClassDefFoundError. I suggest checking the classpath.

Categories