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
Related
I am developing a maven project with Spring mvc application with Eclipse. I have some cucumber-selenium test case to check the some behavior of app. I put the chromDriver.exe in the path target\classes\Driver.
When i want to run the application it complains about:
The project was not built due to "Could not delete '/CyberMall/target/classes/Driver'.". Fix the problem, then try refreshing this project and building it since it may be inconsistent CyberMall Unknown Java Problem
It seems that, it tries to delete the Driver folder inside the target, and it fails, so it cannot build the application.
So, is there any way to ask Eclipse to stop deleting the Driver folder?
The reason I have put the driver in this path is that, i can easily access to it using the below code.
File file = new File(CyberMallApplication.class.getClassLoader().getResource("Driver/chromedriver.exe").getFile());
String driverPath=file.getAbsolutePath();
System.out.println(driverPath);
System.setProperty("webdriver.chrome.driver",driverPath);
If i put it into the resources, i don't know how to access it?
I think it's not a good practice to put a file that should be used in the targer folder.
The target folder should be clean at every maven install so the content is deleted.
You can put your file in the resources folder insted.
Take a look at that link : https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
The target directory is used to house all output of the build.
The content of the resource directory will be put inside WEB-INF/classes. So you can adapt your resources folder to have your folders like you already have.
resources/Driver/chromedriver.exe
You should put chromedriver.exe to src/main/resources and set relative path in the system properties
System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
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
Is it possible to load a class from a jar file and then create an object from it?
Note: The jar file is not there when the program is compiled, but is added by the user later and is loaded in when the user starts the program.
My code goes likes this: The user has a jar file with nothing but a compiled java class in it. The user then puts this jar file in a directory and starts my program which looks through the directory and finds this jar file. It then loads this jar file and creates a class from it which it then creates an object from that and adds it to an array.
I have everything down except for creating a class from the jar file (loaded as a java.io File) and then creating and object from that class.
Any help? Thanks.
You're looking for Class#forNameand Class#newInstance methods.
This link provides a good example about initializing a class knowing its name (extracted from the link):
Class c = Class.forName("com.xyzws.AClass");
AClass a = (AClass)c.newInstance();
A good example for these situations is using JDBC (as the link also points out), because you initialize an object of the db engine driver you want to connect. Remember than this driver comes from an imported jar into your project, it could be a jar to MySQL, Oracle or MSSQL Server, you just provide the driver class name and let the JDBC API and the jar handles the SQL work.
Class.forName("org.gjt.mm.mysql.Driver");
Connection con = DriverManager.getConnection(url, "myLogin", "myPassword");
Also, for this specific problem loading the jar dynamically, there are question and answer:
How should I load Jars dynamically at runtime?
A simpler? way would be for the user to put the jar file in the classpath.
That way your code will have access to the class that will be loaded by the jvm
Edit: Even #Luiggi's answer assumes that the jar is in the classpath
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.
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.