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

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.

Related

Connecting Java to SQLite using IntelliJ [duplicate]

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");
}

How do I interface a mySQL database using BlueJ on a raspberry pi?

I have a fully setup MySQL database running on my pi and I am trying to get it to be able to be called upon in a BlueJ program. I have downloaded the Connector/j driver and set up the code in java. However, I have been banging my head against instructional youtube videos and articles trying to get it to work and nothing has seemed to make a change. Another thing I see that may be notable is that when the code compiles it has no errors, adding to my confusion. Also, the whole thing about where to place the driver is very confusing to me, as is right now I have it stored in the BlueJ project file. Here is the code :
import java.sql
public class mySQL {
public static void main (String[]args) {
try{
class.forName( "com.mysql.jdbc.Driver" ) ;
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost/gui
tarDBuser=root&password=toor" ) ;
Statement stmt = conn.createStatement() ;
ResultSet rs = stmt.executeQuery("Select * from A") ;
while(rs.next() ){
System.out.println(rs.getString(1) );
rs.close() ;
stmt.close() ;
conn.close() ;
}
}catch(SQLException se) {
System.out.println("SQL Exception:") ;
while( se != null) {
System.out.println("state: " + se.getSQLState() ) ;
System.out.println("Message: " + se.getMessage() );
System.out.println("Error: " + se.getErrorCode() );
se = se.getNextException() ;
}
}catch(Exception e)
{
System.out.println(e) ;
}
}
}
When compiled it gives me an instance that says "No fields" and gives me a button to "Show Static Fields" but when I click on the it gives the "No Fields" thing again. Im mega confused and any help would be really appreciated. Thanks!

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:.

execute query on table that contains billions of record [duplicate]

This question already has answers here:
MySQL LIMIT clause equivalent for SQL SERVER
(5 answers)
Closed 8 years ago.
I want to fetch some record(it can be 50,100 or something else that is configured by user) from database without using limit clause because our application may be work on multiple database like mysql,oracle,mssql,db2....
i did following solution
package com.test;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.util.Date;
public class BatchRetrieveTest extends Object {
private static final int FETCH_SIZE = 10;
public BatchRetrieveTest() {
}
public static void main(String[] args) {
BatchRetrieveTest batchRetrieveTest = new BatchRetrieveTest();
batchRetrieveTest.test();
}
void test() {
Connection conn = null;
Statement stmt2 = null;
Date start = null;
Date end = null;
int i = 0;
try {
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test",
"root", "root");
stmt2 = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
conn.setAutoCommit(false);
stmt2.setFetchSize(FETCH_SIZE);
stmt2.setPoolable(true);
start = new Date();
System.out.println(new Date() + "second execute start"
+ new Date().getTime());
ResultSet rs2 = stmt2
.executeQuery("SELECT * FROM sample_final_attendance limit 1000");
end = new Date();
System.out.println(new Date() + "*************second execute end"
+ (end.getTime() - start.getTime()));
rs2.absolute(200000);
i = 0;
while (rs2.next()) {
if (i++ > 100) {
break;
}
}
rs2.close();
stmt2.close();
end = new Date();
System.out.println(new Date() + "second read end"
+ (end.getTime() - start.getTime()));
conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
stmt2.close();
conn.close();
} catch (Exception e) {
}
}
}
}
Here sample_final_attendance table contains 15 columns and 3.2 millions of record
while executing this program it requires 2GB of memory and 47 seconds of execution time
here i wonder that if some table has billions of record then it fails to execute
also i used setFetchSize as suggested but problem is same
please suggest some solution
thanks in advance
Well the ASFAIK & understood, the problem is more related with the handling of data in polyglot storage. If you think, you need to resolve the same in all cases interdependent of the database type - the one common approach is to build a serving layer .
The serving layer can be a cache library or even a Map of Maps created by you. Do not attempt to query the database with large number of records at once, instead bring the data as batches, and store it as a pool of pojos. On demand of the user, you can serve the data from the serving layer.
You can make use of the memcache or hazlecast or many other cache libraries, which can be directly integrated with databases. I really don't know how complex is your situation. What I made is a suggestion. This makes up a data-grid, which can be populated with data from any databases in the background.
We have setMaxRow(int numOfRow) in Statement Object this will limit number of rows generated by the Statement Object and simply ignore the remaining.
Take the look at the doc.

Categories

Resources