Problems connecting to sqlite database in java - java

I am following a tutorial on connecting my SQLite database to a Java application.
When I run the program I get the following error in the console of NetBeans:
run:
Error connecting to the databasejava.sql.SQLException: No suitable
driver found for jdbc:C:\Users\lawman\Documents\Java Working
Directory\LoginSql\src\project123.sqlite
BUILD SUCCESSFUL (total time: 0 seconds)
Here is my directory:
I have code to connect to the database in class tobecalledbymain.
I have main in mainclass which creates an instance of tobecalledbymain.
In my library file, I have sqlite-jdbcs.jar imported.
Here is the code for tobecalledinmain:
import java.sql.*;
public class tobecalledinmain {
public tobecalledinmain(){
Connection con = null;
Statement st=null;
ResultSet rs=null;
try
{
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:C:\\Users\\lawman\\Documents\\"
+ "Java Working Directory\\LoginSql\\"
+ "src\\project123.sqlite");
st=con.createStatement();
//select all records from the table employee
//table has three firlds: employeeid,name and surname
rs=st.executeQuery("SELECT * FROM Employee;");
while (rs.next())
{
int id = rs.getInt("Employeeid");
String name = rs.getString("Name");
System.out.println("id = " + id);
System.out.println("name= " + name);
System.out.println();
}
rs.close();
st.close();
con.close();
}catch(Exception e)
{
System.out.println("Error connecting to the database" + e);
}
}
}
Here is the mainclass code:
public class mainClass {
public static void main(String[] args){
new tobecalledinmain();
}
}
;;
I am not sure why we need to semi-colons!
Anyway, when the tutorial concludes he gets a result from the console. I get the said error message.
What are the drivers in the error message referring to and how do I get them?

Your jdbc connection string does not specify sqlite. Try this, and use forward slashes.
Connection con = DriverManager.getConnection("jdbc:sqlite:C:/PATH/TO/database.db");

This was the WRONG answer, pls disregard.
You need to add the .jar for the SQLite database driver to your classpath when you run your code. See https://bitbucket.org/xerial/sqlite-jdbc
You can see how to do that in Netbeans here: How to add a JAR in NetBeans

Related

Error When Trying Run SQL Server Query through Netbeans

My application is building successfully but won't run the query. Here's what I've done so far:
Downloaded JDBC Driver and used to successfully create connection to database, which is showing in services tab
Added CLASSPATH variable and added C:\Program Files\Java\jre1.8.0_221\bin;C:\Program Files\Java\jdk1.8.0_221\bin;.;C:\Program Files\Java\Microsoft JDBC Driver 7.4 for SQL Server\sqljdbc_7.4\enu\mssql-jdbc-7.4.1.jre8.jar as well as my JRE and JDK bin folders and .
Run below code
import java.sql.*;
public class TestCode {
public static void main(String[] args)
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://mysqlserver:1433;databaseName=mydatabase;integratedSecurity=false;";
Connection conn = DriverManager.getConnection(url, "username", "password") ;
Statement stmt = conn.createStatement();
ResultSet rs;
rs = stmt.executeQuery("SELECT TOP 10 UserID FROM dbo.Users");
while (rs.next())
{
String userID = rs.getString("UserID");
System.out.println(userID);
}
}
catch (Exception e)
{
System.err.println("Error");
System.err.println(e.getMessage());
}
}
}
The error I'm getting is:
Error
com.microsoft.sqlserver.jdbc.SQLServerDriver
Which is the just the name of my driver. I got this from right clicking on the driver, clicking customize and copying the Driver Class from this Window.
Fixed.
Needed to add JDBC driver file to project library

The import oracle.jdbc cannot be resolved

I am currently working on a project where we have to connect to a database and through Java insert values into our database. Our professor gave us code in order to help us see how to carry that out. I am new to Java and have very little experience in it but I have been watching videos and researching online. My problem is the following: I am working in eclipse and have created a class called _DataGenerator_ all the code that is there is from my professor.
import java.sql.*;
import oracle.jdbc.driver.*;
public class TestDataGenerator {
public static void main(String args[]) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String database = "jdbc:oracle:thin:#131.130.122.4:1521:lab";
String user = "a+MatrNr";
String pass = "Oracle-Passwort";
// establish connection to database
Connection con = DriverManager.getConnection(database, user, pass);
Statement stmt = con.createStatement();
// insert a single dataset into the database
try {
String insertSql = "INSERT INTO person VALUES ('012345678902', 'Erich', 'Schiküta', 'Wien', 1010, 'Rathausstrasse 19', '12-FEB-2000', 'Wien')";
stmt.executeUpdate(insertSql);
} catch (Exception e) {
System.err.println("Fehler beim Einfuegen des Datensatzes: " + e.getMessage());
}
// check number of datasets in person table
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM person");
if (rs.next()) {
int count = rs.getInt(1);
System.out.println("Number of datasets: " + count);
}
// clean up connections
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
The only error I get when trying to run the code is from
import oracle.jdbc.driver.*;
When I put my cursor over the error symbol it says
the import oracle.jdbc cannot be resolved
When I try to run the code the only message I get back is
the statement in Red oracle.jdbc.driver.OracleDriver
I don't know what the problem is. I don't know if it helps to know that I have created my database in oracle SQL developer.
In eclipse, right click your project->Build Path->Config Build Path->find the Libraries tab and press the Add External Jars, locate your oracle jdbc driver in your hard driver and select it. Make sure it appears in the jars list, and then press apply and close.
you can find the oracle jdbc driver in the offical website:
https://www.oracle.com/technetwork/database/application-development/jdbc/downloads/index.html.
After that this issue should disappear.
Well,the jdbc lib of official oracle is not in maven repo,you should download it from Oracle Website and maven install your path.And if you use maven build your project,you can do this:
mvn install:install-file -Dfile=E:/app/Administrator/product/11.2.0/dbhome_1/jdbc/lib/ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar
Then add to dependency like :
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>

Showing error-no suitable driver found for jdbc:oracle:thin:#localhost:1521:orcl

I am using jdk1.8 and I am trying to execute a jdbc program manually on oracle 8i. My code is compiling without any error but at the run time its showing error-no suitable driver found for jdbc:oracle:thin:#localhost:1521:orcl. I have already set the class path for jar file. I am using ojdbc7.jar file.
My code is:
import java.sql.*;
class Database
{
public static void main(String arg[])
{
try
{
String url="jdbc:orcl:thin:#localhost:1521:orcl";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(url,"scott","tiger");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from aj1");
while(rs.next())
{
System.out.println("\n"+rs.getInt(1)+" "+rs.getString(2));
}
}
catch(Exception e){e.printStackTrace();}
}
}
Kindly give the solution for this:
You url String must be
String url="jdbc:oracle:thin:#localhost:1521:orcl";
instead of
String url="jdbc:orcl:thin:#localhost:1521:orcl";
and try.

Java "Connection" Class does not connect to the DB

I'm trying to connect my DB with a Java Application i'm creating. What I got so far:
public class DBConnect {
public void DBConnect() {
try {
DBConnect DBConnect = null;
String url = "jdbc:mysql://localhost:3306/ähs_system";
String uName = "**";
String uPass = "**";
// Connection conn = DriverManager.getConnection(url, uName, uPass);
System.out.println("DB Connected");
}
catch (Exception Err) {
System.out.println("Error while connecting: " + Err.getMessage());
System.exit(0);
}
}
}
It's a runnable code although i'm still able to run the code without any error messages if I change my uName and/or Upass. So based on that information i'm gonna say that it's not actually connecting to the database at all...
Anyone with a few tips or tricks I can use?
I've loaded the DB in services and I am able to reach it and add data and run other SQL commands within netbeans but that's basically it. I've also loaded the mysql-connector-java-5.1.35 driver.
Run Code:
public static void main(String args[]) {
try {
DBConnect DBConnect = new DBConnect ();
DBConnect.DBConnect();
}
catch (Exception e){
System.out.println("Cannot connect to DB. Error: " + e.getMessage());
}
Let me know if you need any furthur information!
Updating property file: C:\Users\Johan\Documents\NetBeansProjects\KiltenRos\build\built-jar.properties
Compiling 1 source file to C:\Users\Johan\Documents\NetBeansProjects\KiltenRos\build\classes
C:\Users\Johan\Documents\NetBeansProjects\KiltenRos\src\kiltenros\DBConnect.java:23: error: incompatible types: java.sql.Connection cannot be converted to kiltenros.Connection
Connection conn = DriverManager.getConnection(url, uName, uPass);
1 error
C:\Users\Johan\Documents\NetBeansProjects\KiltenRos\nbproject\build-impl.xml:923: The following error occurred while executing this line:
C:\Users\Johan\Documents\NetBeansProjects\KiltenRos\nbproject\build-impl.xml:263: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)
the error log is clear
java.sql.Connection cannot be converted to kiltenros.Connection
it seems that you imported the wrong class in the beginning of your class
DriverManager.getConnection(url, uName, uPass) return you instance of java.sql.Connection.
btw, change your local variable DBConnect to dbConnect so it won't has the same name as your class DBConnect and it will follow the java convention (lowercase on 1st letter of a variable)
I've very little experience with MySQL, but I avoid using "ä" in a database name. Perhaps, the underscore isn't allowed.
As per your stacktrace it seems you have not imported the correct Connection class. Delete the Connection file in your project and import java.sql.Connection.
The problem seemed to be a corrupted rs2xml.jar file. Once I reloaded it and it worked.

Java database connection issue

I have the following issue with my code :
import java.sql.*;
public class App {
public static void main(String[] args){
String url = "jdbc:mysql://localhost:3306" ;
try{
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException e)
{ System.out.println("Eroare incarcare driver!\n" + e);
return;
}
try{
Connection con = DriverManager.getConnection(url);
// Golim tabelul persoane
String sql = "DELETE FROM persoane";
Statement stmt = con.createStatement();
stmt.executeUpdate(sql);
stmt.execute("CREATE DATABASE IF NOT EXISTS test");
stmt.execute("USE test");
i get the exception...any idea how i can make this work? thx.
enter code here
You need to download and add the jdbc connector to your classpath.
http://dev.mysql.com/downloads/connector/j/
java.lang.ClassNotFoundException occured due to "class not found" in your project/war/ear. Exception is very self explanatory, How to solve it.
In your case:
Add com.mysql.jdbc.Driver driver class/jar in your build/deployment/lib path you can
download it HERE
Read here
Offical
Make sure you have the MySQL driver on your application classpath.
Change
Connection con = DriverManager.getConnection(url);
to
Connection con = DriverManager.getConnection(url,"username","password");
and replace it with yourusername and password

Categories