I am getting this error when I try compiling
ConnectDB.java:14: error: unreported exception ClassNotFoundException; must be caught or declared to be thrown
Class.forName("com.mysql.jdbc.Driver");
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectDB
{
public ConnectDB() throws SQLException
{
Class.forName("com.mysql.jdbc.Driver");
Connection dbConnect = DriverManager.getConnection("jdbc:mysql://xx.xx.xxx.xxx:3306/my_DB", "userName", "superSecurePassword");
}
}
I have downloaded and installed the driver and set the classpath but continue to get the error.
That's because the compiler is telling you that you're not catching a checked exception. You need appropriate handling:
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException e) {
// log exception, probably abort application if it can't run without a database
}
You need to place the following code
Class.forName("com.mysql.jdbc.Driver")
inside a try catch block. Because the code throws *checked exception* and compiler force to catch the checked exception.
Related
I wish to connect to SSAS 2012 using Java and have tried olap4j driver for the same.
I am working on Linux (Ubuntu 14.04) platform.
I have read that Olap4j works for Windows but will it work on Linux ?
I have written some Java code which gives runtime error. This error is because the Linux machine is unable to connect to SSAS.
I have set http access and IIS server and using msmdpump.dll to connect.
Below is the Java code and the error.:
CODE
package ssas;
import java.sql.Connection;
//import java.lang.*;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.olap4j.Cell;
import org.olap4j.CellSet;
import org.olap4j.OlapConnection;
import org.olap4j.OlapException;
import org.olap4j.OlapStatement;
import org.olap4j.OlapWrapper;
import org.olap4j.Position;
import org.olap4j.metadata.Member;
public class connexionSSAS
{
public static void main(String[] args) throws ClassNotFoundException,
OlapException {
Class.forName("org.olap4j.driver.xmla.XmlaOlap4jDriver");
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:xmla:Server=http://01XXXXXXXX/olap/msmdpump.dll;" +
"Catalog=sample_Cube");
} catch (SQLException e) {
e.printStackTrace();
}
if (null == connection) {
System.out.println("Cnul");
}
OlapWrapper wrapper = (OlapWrapper) connection;
OlapConnection olapConnection = null;
try {
olapConnection = wrapper.unwrap(OlapConnection.class);
} catch (SQLException e) {
e.printStackTrace();
}
olapConnection.setCatalog("sample_Cube");
OlapStatement statement = null;
CellSet cellSet = null;
System.out.println(olapConnection.getCatalog());
try {
statement = (OlapStatement) olapConnection.createStatement();
CellSet=statement.executeOlapQuery(
"Summarize(FactDiagnosis,FactDiagnosis[ImportDateKey])");
args}
catch(SQLException e) {
e.printStackTrace();
}
}
}
ERROR
Exception in thread "main" java.lang.RuntimeException: org.olap4j.OlapException: This connection encountered an exception while executing a query.
at org.olap4j.driver.xmla.DeferredNamedListImpl.getList(DeferredNamedListImpl.java:96)
at org.olap4j.driver.xmla.DeferredNamedListImpl.size(DeferredNamedListImpl.java:116)
at org.olap4j.driver.xmla.XmlaOlap4jConnection.getOlapDatabase(XmlaOlap4jConnection.java:451)
at org.olap4j.driver.xmla.XmlaOlap4jConnection.getOlapCatalogs(XmlaOlap4jConnection.java:527)
at org.olap4j.driver.xmla.XmlaOlap4jConnection.setCatalog(XmlaOlap4jConnection.java:483)
at ssas.connexionSSAS.main(connexionSSAS.java:49)
Caused by: org.olap4j.OlapException: This connection encountered an exception while executing a query.
at org.olap4j.driver.xmla.XmlaHelper.createException(XmlaHelper.java:43)
at org.olap4j.driver.xmla.XmlaOlap4jConnection.executeMetadataRequest(XmlaOlap4jConnection.java:878)
at org.olap4j.driver.xmla.XmlaOlap4jDatabaseMetaData.getMetadata(XmlaOlap4jDatabaseMetaData.java:137)
at org.olap4j.driver.xmla.XmlaOlap4jDatabaseMetaData.getMetadata(XmlaOlap4jDatabaseMetaData.java:67)
at org.olap4j.driver.xmla.XmlaOlap4jDatabaseMetaData.getDatabaseProperties(XmlaOlap4jDatabaseMetaData.java:1044)
at org.olap4j.driver.xmla.XmlaOlap4jConnection.makeConnectionPropertyList(XmlaOlap4jConnection.java:324)
at org.olap4j.driver.xmla.XmlaOlap4jConnection.generateRequest(XmlaOlap4jConnection.java:1037)
at org.olap4j.driver.xmla.XmlaOlap4jConnection.populateList(XmlaOlap4jConnection.java:849)
at org.olap4j.driver.xmla.DeferredNamedListImpl.populateList(DeferredNamedListImpl.java:136)
at org.olap4j.driver.xmla.DeferredNamedListImpl.getList(DeferredNamedListImpl.java:90)
... 5 more
This code will run if I comment the statements containing 'OlapConnection'.
Any help would be appreciated.
hello i am trying to load my jdbc diver through classloader
here i am code but why i get this error if possible than give me some example
i don not what to set class path variable
i am making a database application and this application need to connect database again and again and i want to give this application to my friend but my friend not know about class path he is like normal user ,
my application can connect 4 type of database MS-Access,MySQL,Oracle,SQLlite...
in user system i have to set 5 class path variable and provide 5 jar file
if i give this application 100 people than they have set set class path variable
i can include jar file with my application but how can i set class path dynamically ....
please provide some example...
package classload;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ClassLoad {
static Connection con;
public static void main(String[] args) {
File jar = new File("C:\\query\\Driver.jar").getAbsoluteFile();
if(jar.exists()){
System.out.print("File exits");
}
URL urls[] = null;
try {
urls = new URL[] {
jar.toURI().toURL()
};
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ClassLoader cl = new URLClassLoader(urls);
try {
Class.forName("com.mysql.jdbc.Driver", true, cl);
con=DriverManager.getConnection("jdbc:mysql://localhost", "root", "anil");
Statement stm=con.createStatement();
ResultSet result=stm.executeQuery("select *from actor");
while(result.next()){
System.out.print(result.getInt(1)+" "+result.getString(2)+" "+result.getString(3));
System.out.println("");
}
} catch (SQLException e) {
System.out.println(e);
}catch(ClassNotFoundException e){
System.out.println(e);
}
}
}
exception is
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost
Just use One-Jar to package the application and all of the dependencies into single fat jar. Your solution is no good. Your friend would have to use the same folder structure as you are in order for it to work.
This error is coming probably because the required jar file mysql-connector has not been included in your project. Try including jar file as shown here. And try this code to load Driver class:
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jlcstudents","root","password");
In this code class.forname it show error that class not found
so help me with this code because my is a local database application
I am using sqlite database.
package appview;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Databaseconnection {
Connection connection = null;
public static Connection connection2()
{
try
{
Class.forName("org.sqlite.JDBC");
Connection connection = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Jainam\\Java Application\\JPH\\src\\database\\jph_db.sqlite");
return connection;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
}
You have to declare the url this way: jdbc:sqlite:/DRIVE:/dirA/dirB/dbfile
DriverManager.getConnection("jdbc:sqlite:/C:/Users/Jainam/Java Application/JPH/src/database/jph_db.sqlite");
See Here for more informations
ClassNotFoundException means Java is not able to find the org.sqlite.JDBC class. You need to Download sqlite-jdbc-3.6.20.1.jar and put it in your class-path before running it.
here check database connectivity.
class not found ? then go for DDMS->FILE EXP-> SDCARD -> INSERT UR SQLITE.DB file.It will run proper.
i wrote a simple program which is in my book.
but i'm getting the MalformedURLException exception.This is my code
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class ImageGreet{
public static void main(String []args){
URL imageLocation = new URL("http://horstmann.com/java4everyone/duke.gif");
JOptionPane.showMessageDialog(null,"Hello","Title",JOptionPane.PLAIN_MESSAGE,new ImageIcon(imageLocation));
}
}
but my friend said he got it right with the same code.
what's wr9ong with my code? Is it because of the internet connection(I'm using a dial-up connection)
I
Java uses the concept of checked Exceptions. You need to put this code inside a try/catch block, since it is bound to throw a MalformedURLException. Something like
URL imageLocation = null;
try {
imageLocation = new URL("http://horstmann.com/java4everyone/duke.gif");
} catch (MalformedURLException mue) {
mue.printStackTrace();
}
Or let the main method throws the Exception like :
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class ImageGreet{
public static void main(String []args) throws MalformedURLException {
URL imageLocation = new URL("http://horstmann.com/java4everyone/duke.gif");
JOptionPane.showMessageDialog(null,"Hello","Title",JOptionPane.PLAIN_MESSAGE,new ImageIcon(imageLocation));
}
}
catch the MalformedURLException using try and catch block
try {
URL imageLocation = new URL("http://horstmann.com/java4everyone/duke.gif");
JOptionPane.showMessageDialog(null,"Hello","Title",
JOptionPane.PLAIN_MESSAGE,new ImageIcon(imageLocation));
}
catch (MalformedURLException e) {
// new URL() failed
// ...
}
Your internet connection would not cause that Exception. (If your URL did not exist, Java would throw an IOException, probably a FileNotFoundException, per URLConnection documentation.) In fact, your code isn't throwing an Exception at all! What you're seeing is a compile error:
$ javac ImageGreet.java
ImageGreet.java:7: error: unreported exception MalformedURLException; must be caught or declared to be thrown
URL imageLocation = new URL("http://horstmann.com/java4everyone/duke.gif");
^
1 error
When Java tries to turn your program from source code into machine code, it finds a problem, so it stops and asks you to fix it. Your code hasn't run yet -- Java's warning you that there's a problem with your program's source code. (If you're running an IDE, this will show up in a "problems" pane, as opposed to an error message from javac.)
The issue is that you need to catch the MalformedURLException in your code, or declare that main throws MalformedURLException. For example:
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class ImageGreet{
public static void main(String []args) throws MalformedURLException {
URL imageLocation=new URL("http://horstmann.com/java4everyone/duke.gif");
JOptionPane.showMessageDialog(null,"Hello","Title",JOptionPane.PLAIN_MESSAGE,new ImageIcon(imageLocation));
}
}
Note that I've added throws MalformedURLException to the end of your main method, which is the latter of the solutions I suggested above. That tells Java that your main method may propagate an Exception of type MalformedURLException.
Since Java has checked exception you have to add throws MalformedURLException to your methods header or you have to write the method logic inside try/catch blocks.
i Have work on Hadoop/hive. i have install hadoop and hive which is running fine on command prompt.i have also create a MySQL meta store of hive.I have define HIVE-DB database name in hive-site.xml file.The same name database is available in MySQL>HIVE-DB .but the table which is create on hive command prompt is not available in mysql command Prompt.
And when i want to create a hive jdbc Connection then get following error..First it is my Program to create a jdbc connection
package aa;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main
{
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
public static void main(String args[])
{
try {
Class.forName(driverName);
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
try
{
Connection con = DriverManager.getConnection("jdbc:hive://localhost:10001/default", "", "");
Statement stmt = con.createStatement();
String tableName = "recordssss";
stmt.executeQuery("create table"+tableName+"(id int,name string)");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
and then following error is display... because i have start hive as a hive server i.e
**$HIVE_HOME/bin/hive --service hiveserver -p 10001**
xception in thread "main" java.lang.NoSuchMethodError: org.apache.hadoop.hive.service.ThriftHive$Client.sendBase(Ljava/lang/String;Lorg/apache/thrift/TBase;)V
at org.apache.hadoop.hive.service.ThriftHive$Client.send_execute(ThriftHive.java:110)
at org.apache.hadoop.hive.service.ThriftHive$Client.execute(ThriftHive.java:102)
at org.apache.hadoop.hive.jdbc.HiveStatement.executeQuery(HiveStatement.java:187)
at org.apache.hadoop.hive.jdbc.HiveStatement.execute(HiveStatement.java:127)
at org.apache.hadoop.hive.jdbc.HiveConnection.configureConnection(HiveConnection.java:126)
at org.apache.hadoop.hive.jdbc.HiveConnection.<init>(HiveConnection.java:121)
at org.apache.hadoop.hive.jdbc.HiveDriver.connect(HiveDriver.java:104)
at java.sql.DriverManager.getConnection(DriverManager.java:620)
at java.sql.DriverManager.getConnection(DriverManager.java:200)
at aa.Main.main(Main.java:25)
enter code here
so pls help me i have describe the problem to you so pls dear anyone help me
It seems like you are not using appropriate library in your client code. The jars you are using might be of wrong versions. Please check those once.