Connecting Java to SQLite using IntelliJ [duplicate] - java

This question already has answers here:
How to add external library in IntelliJ IDEA?
(5 answers)
What is a classpath and how do I set it?
(10 answers)
Closed 10 months ago.
I just created a SQLite database called database.db in IntelliJ. When I'm trying to connect my code to database, I get the following error
java.lang.ClassNotFoundException: org.sqlite.JDBC
I added JDBC manually to dependencies and the connection in database window is good. This is my code:
public static void connect() {
Connection c = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:database.db");
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Opened database successfully");
}

Related

Getting ClassNotFoundException while working on derby databse from Glassfish on Netbeans [duplicate]

This question already has answers here:
How do I resolve ClassNotFoundException?
(28 answers)
Closed 2 years ago.
This is the code I've written:
package JDBC;
import java.sql.*;
public class JDBC {
public static void main(String[] args) {
try{
String driverName = "org.apache.derby.jdbc.ClientDriver";
String dbURL = "jdbc:derby://localhost:1527/Student";
String dbUser = "DV";
String dbPass = "Pass";
Class.forName(driverName);
Connection con = DriverManager.getConnection(dbURL, dbUser, dbPass);
Statement stmt = con.createStatement();
String sql;
sql = "create table Student(Enroll_No Varchar(20), Name Varchar(20), Gender Char, Age Int, CGPA Float, Email_ID Varchar(30))";
stmt.executeUpdate(sql);
stmt.close();
con.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
The error I'm getting is:
run:
java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver
BUILD SUCCESSFUL (total time: 0 seconds)
and no table is created. I'm using Glassfish 4.1.1(Derby Database) on NetBeans IDE 8.2
For a ClassNotFoundException, you might want to check about missing .jar files in your project library.
Go to your Project Folder on Netbeans (top left corner), then Libraries and then check for "derbyclient.jar" and "derby.jar" files.
If they are not present, add them manually from your GlassFish folder.
Right click on Libraries -> Add JAR/Folder... Browse to your Glassfish 4.1.1 installation folder -> javadb -> lib and select derbyclient.jar and derby.jar files.
This should work and the exception shall be removed.

Java MySQL connection not working: No suitable driver found [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 2 years ago.
So I am trying to make a connection with JDBC using XAMPP, but it doesn't work, What am I doing wrong here?
public static void main(String args[]) {
try {
Connection myConn = DriverManager.getConnection(
"http://localhost/phpmyadmin/sql.php?db=a3_eindopdracht_2&table=namen&pos=0", "", "");
Statement myStm = myConn.createStatement();
ResultSet myRs = myStm.executeQuery("SELECT * FROM namen");
while (myRs.next()) {
System.out.println(myRs.getString("voornaam") + " " + myRs.getString("achternaam"));
}
} catch (Exception exc) {
exc.printStackTrace();
}
Firstly, you don't find Driver. You have to load them, by calling the Drive class like that :
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// Cannot find driver for MySQL
}
Then, you are trying to connect to your databse with HTTP protocol. But, DB have they own (with port 3306 by default), so you have to use address like that:
jdbc:mysql://myserver.com/schema
Finally: don't forget to add username and password on last 2 fields of getConnection method

MY JDBC program compiles successfully but does not runs with exception [duplicate]

This question already has answers here:
Class has been compiled by a more recent version of the Java Environment
(33 answers)
Closed 3 years ago.
My Program runs successfully but gives an exception at the runtime.
I have followed all the 8 steps to make a JDBC program.
The code and image showing the exception are given.
I have also created my own DSN(data source name) in the admin settings in my control panel.
Anyone who would let me know a solution to this problem.
I would be highly grateful to you.
Thanks in advance.
This shows the error which I face at the run time
import java.sql.*;
public class JDBC {
public static void main(String[] args)
{
try
{
// TODO Auto-generated method stb
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
String conURL = "jdbc:odbc:PersonDSN" ;
Connection con = DriverManager.getConnection(conURL) ;
Statement st = con.createStatement() ;
String sql = "Select * from Student" ;
ResultSet rs = st.executeQuery(sql) ;
while (rs.next())
{
String sname = rs.getString("SName");
String saddress = rs.getString("SAddress");
String sno = rs.getString("SNumber");
System.out.println(sname + " " + saddress + " " + sno );
}
con.close();
}
catch (Exception a)
{
System.out.print(a);
}
}
}
It looks like a Java version mismatch.
You have a compiled class file compiled using Java 12 and are using Java version 8 at runtime.
You can also see answer;
https://stackoverflow.com/a/47457251/11226302
I'm guessing if you use jdk12, it should fix the problem.

Always getting Run Time Error #Java [duplicate]

This question already has answers here:
Removal of JDBC ODBC bridge in java 8
(5 answers)
Manipulating an Access database from Java without ODBC
(1 answer)
Closed 4 years ago.
So I'm getting run time error always when I tried to login. Below is the code. I'm using Java JRE 8 Eclipse to compile it. Can anyone please let me know what's the problem? I've omitted most of the code. Link to full code : https://codeshare.io/5gNyZw . Already tried Removal of JDBC ODBC bridge in java 8 and how to use JDBC in java 8 but nothing works
try
{
String database="StegoKeys.mdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + database + ";PWD=cegospdv";
Connection con=DriverManager.getConnection(url);
String sql="select * from keys";
PreparedStatement ps=con.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next())
{
if(txtKey.getText().equals(rs.getString("key")))
{
id=rs.getString("uname");
if(txtname.getText().equals(id))
{
int stat=Integer.parseInt(rs.getString("status"));
flag=1;
if(stat==1)
{
button1.setEnabled(true);
button2.setEnabled(true);
btnadminset.setVisible(true);
txtKey.setEnabled(false);
btnLogin.setEnabled(false);
}
else
{
button1.setEnabled(true);
txtname.setEnabled(false);
txtKey.setEnabled(false);
btnLogin.setEnabled(false);
}
}
//System.out.println(id+" in MainStego");
con.close();
break;
}
}
if(flag==0)
{
JOptionPane.showMessageDialog(this,"Invalid User & Key");
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(this,"Run Time Error");

java.sql.SQLException: No suitable driver found for jdbc::mysql://localhost:3306/asd eroor [duplicate]

This question already has answers here:
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
No suitable driver found for 'jdbc:mysql://localhost:3306/mysql [duplicate]
(8 answers)
Closed 5 years ago.
import java.sql.*;
class Mysqll{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc::mysql://localhost:3306/asd","root","qwerty");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from abc");
while(rs.next())
{
System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
I am new to jdbc programming. So plese help.
Getting this Exception while running the program.
java.sql.SQLException: No suitable driver found for jdbc::mysql://localhost:3306/asd
I had copied mysql-connector.jar file into jre/lib/ext folder.
Thanx in advance.
The connection string being passed to DriverManager is a URL. The formatting matters, and your problem is due to an extra colon between jdbc and mysql (verified by testing on my own system).
Replace jdbc::mysql: with jdbc:mysql:.

Categories

Resources