As I mentioned on the subject, I am facing the warning "The value of the local variable conn is not used"
I clearly used the variable on my coding but it shows me that type of error.
It will be highly appreciated if you can advise me on this.
[CODE]
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class example {
public static void main(String[] ar) {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Success to find Driver");
}catch(ClassNotFoundException e) {
System.err.println("error = Failed to find driver");
}//*Find MYSQL driver
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/class", "root", "****");
System.out.println("Connected to MySql");
} catch (SQLException e) {
System.err.println("error = Failed to Create connection");
}//* Creating Connection
}
}
[RESULT AFTER RUN]
Success to find Driver
error = Failed to Create connection
In your code you have assigned a reference to conn but you are not using that object anywhere, hence the warning.
To get rid of the warning either use it somewhere (maybe do a sysout somewhere) or add the #SuppressWarnings("unused") annotation
Related
I wrote this code but it doesn't work.
It throws an error that says that this is not suitable Driver.
Can anyone explain what I am doing wrong?
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionUtil {
public static Connection getConnection() {
try {
Class.forName("org.h2.jdbc.Driver");
Connection con = DriverManager.getConnection("org.h2:database/javadb", "root", "");
return con;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
I used this URL for a connection to my DB "jdbc:h2:file:./database/mydatabase"
I am writing this because I created a simple Login GUI App to test sqlite database as I am a student of Database Systems and new to it, I used java through eclipse, whenever I run the Application this message
java.sql.SQLException path to c:user//path does not exist
Error Screenshot
I have searched a lot on google but couldn't find the solution there is a similar question on stackoverflow but there were not enough answer related to my problem, I want to know how to change the code to make the Application work and connect to database?
Any help will be much appreciated.Thanks
Here is the code:
package dbms;
import java.sql.*;
import javax.swing.*;
public class dbConnection {
Connection conn = null;
public static Connection dbConnector(){
try{
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\chusm\\workspace\\DBMS\\SQlite\\DBMS.sqlite");
JOptionPane.showMessageDialog(null, "Connection Successful!!!");
return conn;
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
It looks like you've got some weird whitespace action going on in your url (between "jdbc:sqlite" and "C:"
Please copy paste this exact code in your project and run it (I only removed the weird whitespace, the rest is exactly like your code)
package dbms;
import javax.swing.*;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
public class StackOverflowExample {
Connection conn = null;
public static Connection dbConnector() {
try {
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\chusm\\workspace\\DBMS\\SQlite\\DBMS.sqlite");
JOptionPane.showMessageDialog(null, "Connection Successful!!!");
return conn;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
public static void main(String[] args) {
Connection connection = dbConnector();
}
}
I'm creating a program where (among other things) from the space for the user to change the url, name and password for the database connection.
In a class I get the values that are saved in a file and up to here everything ok. The problem arises when I'm in class concerning the connection of the batabase. The fault is not the OutProp method (which fetches the data in the properties file) because I tried it in another class and it works perfectly. So I guess there is a problem of writing code maybe in public static method Connection ConnectDb () as being static you have to behave differently, and since I have started recently to study Java I think I missed something.
PS. Writing Connection conn = DriverManager. getConnection ("jdbc: mysql://localhost/databaseprogetto/root/root"); connects to the database.
Thank you for any suggestion or solution and I hope that I explained well.
import java.io.FileInputStream;
import java.sql.*;
import javax.swing.*;
import java.io.IOException;
import java.util.Properties;
public class JavaConnect {
Connection conn = null;
static String url_database;
static String username;
static String password;
public void OutProp (){
Properties prop = new Properties();
try {
prop.load(new FileInputStream("config.properties"));
url_database = prop.getProperty("Url");
username = prop.getProperty("Username");
password = prop.getProperty("Password");
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static Connection ConnectDb(){
try{
Class.forName ("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection (url_database,username,password);
return conn;
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
1. Please make sure you have No space on either side of the 2 operands in Property file.
Eg:
Property1=value1
Property2=value2
2. Try putting this Property file in the bin folder of your Project.
I have a few DB's in mysql and all of them contain some tables with a few columns. I got the code below from a stack overflow answer.
The answer is at:
How can I detect a SQL table's existence in Java?
The code gives the output-
Driver Loaded.
Got Connection.
Code-
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
public static void main(String[] args) throws Exception {
DatabaseMetaData md = conn.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {
System.out.println(rs.getString(3));
} }
static Connection conn;
static Statement st;
static {
try {
// Step 1: Load the JDBC driver.
System.out.println("Driver Loaded.");
// Step 2: Establish the connection to the database.
String url = "jdbc:mysql://localhost:3306/";
conn = DriverManager.getConnection(url, "cowboy", "123456");
System.out.println("Got Connection.");
st = conn.createStatement();
} catch (Exception e) {
System.err.println("Got an exception! ");
e.printStackTrace();
System.exit(0);
}
}
}
In your Code you only
System.out.println("Driver Loaded.");
That's not enough. You have to load the driver first!
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver Loaded.");
I'm surprised that this works
conn = DriverManager.getConnection(url, "cowboy", "123456");
and that you come to this line of code.
System.out.println("Got Connection.");
with the following code, it will go, but you will not get a list of tables
static {
try {
// Step 1: Load the JDBC driver.
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver Loaded.");
// Step 2: Establish the connection to the database.
String url = "jdbc:mysql://localhost";
conn = DriverManager.getConnection(url,"user","passw");
System.out.println("Got Connection.");
....
}
}
set the database name correctly
static {
try {
// Step 1: Load the JDBC driver.
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver Loaded.");
// Step 2: Establish the connection to the database.
String url = "jdbc:mysql://localhost/myDataBase";
conn = DriverManager.getConnection(url,"user","passw");
System.out.println("Got Connection.");
....
}
}
and you can see a list of your myDataBase tables.
This code is used to display the tables of a specific database, not all tables of all DB. You don't specify any database in your url String so there is nothing to display.
If you look carefully at the link used to answer the linked question, you can see String url = "jdbc:hsqldb:data/tutorial"; So you have to be connected to a database first.
PS : You may have to load the driver if you use driver prior to jdbc4, use : Class.forName("com.mysql.jdbc.Driver"); and be sure the driver is available in the classpath.
I am really new to JAVA but need to call a SQL Server function which I have been given access to.
I have built a JAVA call into a pl/sql function and am successfully calling it from one of my environments. When I move to another environment I get the error
ORA-29532: Java call terminated by uncaught Java exception: java.lang.ClassNotFoundException: com/microsoft/sqlserver/jdbc/SQLServerDriver
I have researched this to death and checked the correct installation of JAVA which seems fine but I'm obviously missing something. I need to somehow trace what is different on this environment, the fact that it runs in the other envionmnet proves that the class is correct so it has to be a config issue.
JAVA Class
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.net.Socket;
import java.io.IOException;
public class xxiceHJ
{
protected static String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
protected static String DB_URL = "jdbc:sqlserver://999.999.99.99:1433";
protected static String USER = "xxxx";
protected static String PWD = "xxxxx123$";
public static String getOrderStatus (String OrderNumber) throws SQLException, Exception
{
Connection conn = null;
CallableStatement cs = null;
ResultSet rs = null;
String Message = null;
String WarehouseId = "01";
try
{
// Register JDBC driver
Class.forName(JDBC_DRIVER);
//Open a connection
conn = DriverManager.getConnection(DB_URL, USER, PWD);
cs = conn.prepareCall("{call usp_get_order_status(?,?,?)}");
cs.setString(1, WarehouseId);
cs.setString(2, OrderNumber);
cs.setString(3, Message);
rs = cs.executeQuery();
//if prodeure return a value
if (rs.next())
{
Message = rs.getString(1);
}
//Clean-up environment
rs.close();
cs.close();
conn.close();
// }
// catch (SQLException se)
// {
// //Handle errors for JDBC
// cfFileNumber = "SQLException" + se.toString();
// se.printStackTrace();
// }
// catch (Exception e)
// {
// //Handle errors for Class.forName
// cfFileNumber = "Exception" + e.toString();
// e.printStackTrace();
}
finally
{
//finally block used to close resources
try
{
if (rs!=null)
{
rs.close();
}
}
catch (SQLException se2)
{
//nothing we can do
}
try
{
if (cs!=null)
{
cs.close();
}
}
catch (SQLException se2)
{
//nothing we can do
}
try
{
if (conn!=null)
{
conn.close();
}
}
catch (SQLException se)
{
se.printStackTrace();
}
}
return Message;
}
}
The CLASSPATH variable is the search string that Java Virtual Machine (JVM) uses to locate the JDBC drivers on your computer. If the drivers are not listed in your CLASSPATH variable, you receive the following error message when you try to load the driver:
java.lang.ClassNotFoundException: com/microsoft/jdbc/sqlserver/SQLServerDriver
The JDBC driver is not part of the Java SDK. If you want to use it, you must set the classpath to include the sqljdbc.jar file or the sqljdbc4.jar file. If the classpath is missing an entry for sqljdbc.jar or sqljdbc4.jar, your application will throw the common "Class not found" exception.
The sqljdbc.jar file and sqljdbc4.jar file are installed in the following location:
\sqljdbc_\\sqljdbc.jar
\sqljdbc_\\sqljdbc4.jar
The following is an example of the CLASSPATH statement that is used for a Windows application:
CLASSPATH =.;C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\sqljdbc.jar
The following is an example of the CLASSPATH statement that is used for a Unix/Linux application:
CLASSPATH =.:/home/usr1/mssqlserverjdbc/Driver/sqljdbc_4.0/enu/sqljdbc.jar
You must make sure that the CLASSPATH statement contains only one Microsoft JDBC Driver for SQL Server, such as either sqljdbc.jar or sqljdbc4.jar.
For more information, please see:
http://support.microsoft.com/kb/313100
http://msdn.microsoft.com/en-us/library/ms378526.aspx
first Please download correct sql driver and then check your are using correct connection driver as per operating system. then once you have to test your connection if its working fine then you will go to next .
so please check this url
microsift sql server driver for linux
https://msdn.microsoft.com/en-us/library/hh568451(v=sql.110).aspx
my sql server driver for linux
https://dev.mysql.com/downloads/connector/j/5.0.html