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");
Related
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");
}
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
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.
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:.
This question already has answers here:
What does "if (rs.next())" mean?
(7 answers)
Closed 6 years ago.
Here is the code
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\MUHAMMAD SHAHAB\\STUD1.accdb");
Statement st = conn.createStatement();
String sql="select Username,Password from SUN where Username='"+user+"'and Password='"+pass+"'";
ResultSet rs=st.executeQuery(sql);
int count=0;
while(rs.next()) {
count=count+1;
}
if(count>0) {
JOptionPane.showMessageDialog(null, "Access granted");
} else if(count<1) {
JOptionPane.showMessageDialog(null,"User Not Found\nAccess is Denied");
}
I am creating a user verification system in Java and I have connected my program with MS Access. I have inserted some records in the fields of table SUN in MS Access and it is working properly. But I just want to know the working of next() method and count variable in my program.
It moves (or tries to, returning a boolean telling whether it succeeded or not) the resultset cursor forward. The count variable is useless, since you can just write if(rs.next()) to determine which message is shown.