Source not found in debugging mode - java

import java.sql.*;
public class DBTesting {
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
try
{
String user = "sa";
String pass = "xxx";
String jdbcURL = "jdbc:odbc:btrsDSN";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("Driver Loaded !");
Connection conn = DriverManager.getConnection(jdbcURL,user,pass);
System.out.println("Connection Obtained");
Statement stmt = conn.createStatement();
stmt.close();
conn.close();
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Error occurs in this line when the debugger tries to debug the
Connection conn = DriverManager.getConnection(jdbcURL,user,pass); . It shows source not found. I edit source lookup path but the problem still here. I also try many solutions from internet but none of them solve the problem. For convenience I also attached screen shots.
Snapshot in regular mode
Snapshot in Debugging mode

click on "edit source Lookup path" and specify the path where that particular file exists, then you will be available to see the java lines of code.
if the java file is in different project and your current project is accessing the class through jar file ,then configure your current project to work with "project dependency" instead of "jar dependency"
Hope this helps!
Good luck!

Related

Inserting Data into MySQL using Java

I try to insert data into MySQL using Java and I have error all the time.
I use this video to understand how to do to the connection.
This is my Java code:
import java.sql.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn = null;
//DriverManager.registerDriver(new com.mysql.jdbc.Driver ());
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/try1-progrem", "root", "123456");
Statement st = conn.createStatement();
String username = "kola";
String password = "24688642";
String insert = "INSERT INTO login VALUES ('"+username+"','"+password+"')";
st.executeUpdate(insert);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Get the error:
java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost:3306/try1-progrem
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Main.main(Main.java:11)
And here to SQL image: sql data:
** I learned from YouTube how to make the connection, if there is a good guild, I will glad to take him.
*EDIT1:
build
To fix this you should follow the steps below:
You need to download the correct ConnectorJ from the link below.
After downloading the correct installer for your machine you will need to run the installer to install only the correct jdbc connector for you. (A series of prompts will lead you to selecting the connector and not all the MySQL downloads)
Then you can put the .jar file (connectorJDBC file) into your classpath appropriately and this should fix your problem as I encountered a similar problem while trying to connect to a SQL server with java.
Hopefully this fixes your problem, comment if it doesn't. :)
ConnectorJ Download

JDBC JAVA No suitable driver found for jdbc:mysql://localhost:3306/voting

Hello im trying to connect to a mysql database using JDBC my code is below.I get an error as such No suitable driver found.Searching around I found that the usual error is syntax or missing the jar file from the class path.I tried both of these solutions and dont know what to do next it wont connect.Also to manage the databases I have WAMP and mySQL workbench installed not sure if its related.
package test.jdbc;
import java.sql.*;
public class jdbctester {
public static void main(String[] args)
{
try
{
Connection myconn=DriverManager.getConnection("jdbc:mysql://localhost:3306/voting","root","Vanquish123");
Statement myStmt=myconn.createStatement();
ResultSet myRs=myStmt.executeQuery("select * from electoral");
/*
while(myRs.next())
{
System.out.println(myRs.getString("state")+","+myRs.getString("perDem"));
}
*/
}
catch(Exception exc)
{
exc.printStackTrace();
}
}
}
Try this:
Class.forName("fully qualified driver class name");
Java Class.forName, JDBC connection loading driver
this post states that you should not need it but it will not hurt you to try.
you have to add "com.mysql.jdbc_5.1.5.jar" in to your project build path... go to project property>build Path> library> add external jar and add jar file.
Connection conn = null;
try {
// Register JDBC driver
Class.forName(DRIVER).newInstance();
// Open a connection
conn = DriverManager.getConnection(Local_URL + , USERNAME, PASSWORD);
System.out.println("Connected Database Successfully...\n\n");
} catch (Exception se) {
throw new AppException("Failed to create Local Database connection", se);
}
return conn;

Connecting to MySQL using JDBC [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 7 years ago.
I have searched the internet trying to find solutions on using java and JDBC to connect to MySQL. Unfortunately I have found multiple answers and none that solved my problem.
I downloaded the JDBC from MySQL, and unzipped the file to find the .jar. I placed the .jar in my C:/Program Files (X86)/Java/JDK.../JRE/lib/ext folder. I set my environmental variable classpath (maybe CLASSPATH, ClassPath ??) to the following:
%CLASSPATH%;.;C:\Program Files (x86)\Java\jdk1.8.0_65\jre\lib\ext
I use the script I composed based on all the different solutions I have seen to get this:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MySql {
public static void main(String[] args)
{
Connection con = getConnection();
if (con != null) {
System.out.println("Connection Made");
}
else {
System.out.println("Connection not made");
}
}
private static Connection getConnection() {
Connection con = null;
try {
Class.forName("com.mysql.jdbc");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila", "java", "java");
System.out.println("Connection Made");
conn.close();
}
catch (SQLException e)
{
System.out.println(e.getMessage());
System.exit(0);
}
catch (ClassNotFoundException e)
{
System.out.println(e.getMessage());
System.exit(0);
}
return con;
}
}
It compiles with javac MySql.java. Then when it runs (java MySql) I get com.mysql.jdbc. I have read that I don't need to register the driver, but when I remove Class.forName all I get is 'cannot find JDBC driver' error.
I can't narrow down my problem to either:
1). Classpath not setup correctly.
2). Improper java connection code.
3). Unable to locate MySQL server.
Any help would be appreciated.
Edit -
I placed the .jar file onto Desktop for testing purposes. Changed system classpath variable:
%CLASSPATH%;.;C:\User\User\Desktop\mysql-connector-java-5.1.38-bin.jar
Then when I add the trace for the error statement I get:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass<Unkown Source>
at java.lang.ClassLoader.loadclass (Unknown source)
at sun.misc.Launcher$AppClassLoader.loadclass(Unkown Source)
etc.
etc.
edit 2 - I have spent two days using Connect Java to a MySQL database as a resource and none of the instructions I have followed have solved my problem.
Instead of
System.out.println(e.getMessage());
Do this
e.printStackTrace();
You will see that the exception is:
java.lang.ClassNotFoundException: com.mysql.jdbc
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at MySql.getConnection(MySql.java:24)
at MySql.main(MySql.java:10)
Remove the Class.forName. You might get an access denied or some other error but it will solve the ClassNotFoundException. Here is the final edited version that should work:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MySql {
public static void main(String[] args)
{
Connection con = getConnection();
if (con != null) {
System.out.println("Connection Made");
}
else {
System.out.println("Connection not made");
}
}
private static Connection getConnection() {
Connection con = null;
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila", "java", "java");
System.out.println("Connection Made");
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
System.exit(0);
}
return con;
}
}
If you need Class.forName() you have to use correct class:
Class.forName("com.mysql.jdbc.Driver");
But with JDBC 4 that has become no longer needed.
Create full Exception stack traces and use the result to search here - most common errors have been solved here before.
NoSuitableDriverFound is a very strong indication your mysql-connector.jar (not the .zip....) is missing from your classpath when you run your code.
You could try like this:
Run java -cp .;C:\User\User\Desktop\mysql-connector-java-5.1.38-bin.jar MySql
I think you must add the path including the jar file name of the driver in your classpath. Like so:
%CLASSPATH%;.;C:\Program Files (x86)\Java\jdk1.8.0_65\jre\lib\ext\mysql-connector-java-xxx.jar
If your Java is version 6 or above you doesn't need Class.formName(...) code.
And for ensure that everything works, compile and execute your code in the follow way.
To compile:
java -cp PATH_TO_DRIVER; YOUR_CLASS.java
To execute:
java -cp PATH_TO_DRIVER; YOUR_CLASS
Change YOUR_CLASS to the name of your class and PATH_TO_DRIVER to the path where you download the MySQL driver.
Hope it helps!
Try this code!
public static void main(String[] argv) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/yourSCHEMAname,"login", "password");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}

unusual No suitable driver found for jdbc://localhost:1527/society

previously i using this code to connect my database there is non error occur.
but comes to this DA files, its unable to connect to database.
i had go through most of the post but some of it i don't understand.[i'm just new to java]
i had try to use jdbc:derby://localhost:1527/societydb;create=true
but the same error occur again.
here's the code and <<< is the line the error point to.
private String host = "jdbc:derby://localhost:1527/societydb";
private String user = "nbuser";
private String password = "nbuser";
private String tableName = "MEMBER";
private void createConnection() {
try {
conn = DriverManager.getConnection(host, user, password);
System.out.println("*** Successfully established the connection to database. ***");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error Message", JOptionPane.ERROR_MESSAGE);
}
}
public ArrayList<Member> getMember() {
ArrayList<Member> memArray = new ArrayList<>();
try {
stmt = conn.prepareStatement("SELECT * FROM " + tableName);//<<< error pointing to here
ResultSet rs = stmt.executeQuery();
while (rs.next()){
Member m = new Member(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getInt(9), rs.getString(10), rs.getString(11));
memArray.add(m);}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error Message", JOptionPane.ERROR_MESSAGE);
}
return memArray;
}
From Java documentation the drivers you need are org.apache.derby.jdbc.EmbeddedDriver and org.apache.derby.jdbc.ClientDriver.
Also it clearly states
Any JDBC 4.0 drivers that are found in your class path are automatically loaded.(However,
you must manually load any drivers prior to JDBC 4.0 with the method Class.forName.)
Note : JDBC 4.0 comes as a default package from Java 7 onwards.
As for your problem search for the classes mentioned above in your class path(Ctrl + N in Intellij Idea or Ctrl + R in Eclipse). If these classes are not present google them, download and add the jar files to your class path.
Just add these external jars:
derby.jar
derbyclient.jar
How to do it:
right click your project > Properties > Java Build Path > Libraries
click 'Add external jar'
go to C:\Program Files\Java\jdk1.8.0_65\db\lib
add the derby.jar and derbyclient.jar

Data retrieval from JAVA DB in eclipse located in jdk1.7.0_06

I am trying to retrieve data installed on the database server JAVA DB located in jdk1.7.0_06. I am able to make connection to the server and create the database. But i am getting following error for the compilation and running:
No suitable driver found for jdbc:derby:AddressBook
Please can you help me! Thank you
I stated, "I wonder if you need to set the derby.system.home property as the Java DB tutorials suggest. Have you tried this? Something like, System.setProperty("derby.system.home", DERBY_HOME_PATH); where the second parameter is the path to your database's home directory."
And you replied:
#HovercraftFullOfEels, i think i didn't but i am sure that i have set some envariable variables via command line.
#Dorji: that doesn't set the System properties in your JVM though. I still think that you need to set this property before using your database. For example,
public class Test {
public static final String DERBY_HOME = "derby.system.home";
// ***** the two Strings below will be different for you *****
public static final String DERBY_HOME_PATH = "D:/DerbyDB";
private static final String DB_NAME = "sample";
public static void main(String[] args) {
System.setProperty(DERBY_HOME, DERBY_HOME_PATH);
Connection conn = null;
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
conn = DriverManager.getConnection("jdbc:derby:" + DB_NAME);
} catch (InstantiationException | IllegalAccessException
| ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
if (conn == null) {
System.exit(-1);
}
}
// .... etc...
My derby.system.home directory is D/:DerbyDB, and my database resides in the D/:DerbyDB/sample directory:
This of course will be different for you.

Categories

Resources