this is my code snippet,
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class Delete
{
public static void main(String args[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("mysql:jdbc://localhost:3306/raja","root","459805");
Statement stmt=con.createStatement();
int count=stmt.executeUpdate("DELETE GENNU WHERE USER_ID=3;");
if(count>0)
System.out.println(" Ok Deletion done");
}
catch(ClassNotFoundException e)
{
System.out.println(e.getMessage());
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
}
and when I execute it , i got like this.
actually you have an error in your DELETE statement, you lack FROM keyword. it should be
DELETE FROM GENNU WHERE USER_ID=3
see the error, it's pointing on DELETE.
UPDATE 1
try, jdbc:mysql not mysql:jdbc
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/raja"
+ "user=root&password=459805");
MySQL and JAVA JDBC
Try with
Class.forName("com.mysql.jdbc.Driver").newInstance();
The documentation says:
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
Also, the URL should be
jdbc:mysql://localhost/3306/raja
and not
mysql:jdbc://localhost/3306/raja
You need the mySQL Java Connector. Which you can find at the download page here:
https://www.mysql.com/products/connector/
Related
I want to write a code to get my queries from input and run it and show me the result.I have to connect to database in my code.
I connected to postgres by intellij database extension and my queries run in the console.
but i want to do that in my code(i mean get the queries from user and run it).
is it possible to use this database connection and run queries on it?
i got connected successfully by this code too :
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/db","postgres", "pass");
and i wrote some queries.
all query types(such as update , delete , insert , ...) run and the result is visible in the pgadmin but i want to have the result in my java code
Here is an example to run a query with JDBC. you can download the JDBC here https://jdbc.postgresql.org/download.html and added to your classpath
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class PostgreSqlExample {
public static void main(String[] args) {
try (Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/example", "postgres", "postgres")) {
System.out.println("Java JDBC PostgreSQL Example");
System.out.println("Connected to PostgreSQL database!");
Statement statement = connection.createStatement();
System.out.println("Reading car records...");
System.out.printf("%-30.30s %-30.30s%n", "Model", "Price");
ResultSet resultSet = statement.executeQuery("SELECT * FROM public.cars");
while (resultSet.next()) {
System.out.printf("%-30.30s %-30.30s%n", resultSet.getString("model"), resultSet.getString("price"));
}
} /*catch (ClassNotFoundException e) {
System.out.println("PostgreSQL JDBC driver not found.");
e.printStackTrace();
}*/ catch (SQLException e) {
System.out.println("Connection failure.");
e.printStackTrace();
}
}
}
I am using one simple code to access the SQLite database from Java application .
My code is
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ConnectSQLite
{
public static void main(String[] args)
{
Connection connection = null;
ResultSet resultSet = null;
Statement statement = null;
try
{
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:D:\\testdb.db");
statement = connection.createStatement();
resultSet = statement
.executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
while (resultSet.next())
{
System.out.println("EMPLOYEE NAME:"
+ resultSet.getString("EMPNAME"));
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
resultSet.close();
statement.close();
connection.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
But this code gives one exception like
java.lang.ClassNotFoundException: org.sqlite.JDBC
How can I slove this,please help me.
You need to have a SQLite JDBC driver in your classpath.
Taro L. Saito (xerial) forked the Zentus project and now maintains it under the name sqlite-jdbc. It bundles the native drivers for major platforms so you don't need to configure them separately.
If you are using netbeans Download the sqlitejdbc driver
Right click the Libraries folder from the Project window and select Add Library ,
then click on the Create button enter the Library name (SQLite) and hit OK
You have to add the sqlitejdbc driver to the class path , click on the
Add Jar/Folder.. button and select the sqlitejdbc file you've downloaded previously
Hit OK and you are ready to go !
If you are using Netbeans using Maven to add library is easier. I have tried using above solutions but it didn't work.
<dependencies>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version>
</dependency>
</dependencies>
I have added Maven dependency and java.lang.ClassNotFoundException: org.sqlite.JDBC error gone.
I'm using Eclipse and I copied your code and got the same error. I then opened up the project properties->Java Build Path -> Libraries->Add External JARs...
c:\jrun4\lib\sqlitejdbc-v056.jar
Worked like a charm. You may need to restart your web server if you've just copied the .jar file.
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import org.sqlite.SQLiteDataSource;
import org.sqlite.SQLiteJDBCLoader;
public class Test {
public static final boolean Connected() {
boolean initialize = SQLiteJDBCLoader.initialize();
SQLiteDataSource dataSource = new SQLiteDataSource();
dataSource.setUrl("jdbc:sqlite:/home/users.sqlite");
int i=0;
try {
ResultSet executeQuery = dataSource.getConnection()
.createStatement().executeQuery("select * from \"Table\"");
while (executeQuery.next()) {
i++;
System.out.println("out: "+executeQuery.getMetaData().getColumnLabel(i));
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
}
return initialize;
}
You have to download and add the SQLite JDBC driver to your classpath.
You can download from here https://bitbucket.org/xerial/sqlite-jdbc/downloads
If you use Gradle, you will only have to add the SQLite dependency:
dependencies {
compile 'org.xerial:sqlite-jdbc:3.8.11.2'
}
Next thing you have to do is to initialize the driver:
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException eString) {
System.err.println("Could not init JDBC driver - driver not found");
}
connection = DriverManager.getConnection("jdbc:sqlite:D:\\testdb.db");
Instead of this put
connection = DriverManager.getConnection("jdbc:sqlite:D:\\testdb");
Hey i have posted a video tutorial on youtube about this, you can check that and you can find here the sample code :
http://myfundatimemachine.blogspot.in/2012/06/database-connection-to-java-application.html
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class Connectdatabase {
Connection con = null;
public static Connection ConnecrDb(){
try{
//String dir = System.getProperty("user.dir");
Class.forName("org.sqlite.JDBC");
Connection con = DriverManager.getConnection("jdbc:sqlite:D:\\testdb.db");
return con;
}
catch(ClassNotFoundException | SQLException e){
JOptionPane.showMessageDialog(null,"Problem with connection of database");
return null;
}
}
}
Iam new to the hadoop ecosystem. Tried to access hive through jdbc. For that I have written the following code
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveConnection {
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000");
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery("select * from test.employees");
while (res.next()) {
System.out.println(res.getString(1));
} }}
and started the hiveserver2 (hive version 0.12 & hadoop version 1.1.2) through the terminal and im getting the status as "Starting HiveServer" . When i tried executing the above code from eclipse im no getting any error and any results neither(Got the same when i executed the executable "hiveserver" ).
Can any one help me out.
Thanks in advance.
Following ways are most really reason for your problem.
1.Is the hive-jdbc-*.Jar having classpath like this "org.apache.hadoop.hive.jdbc.HiveDriver" or "org.apache.hive.jdbc.HiveDriver"?
2.While connection,you need to pass the user name and password like this?
If you use hive server2 then you have to set the connection like below.
con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "", "");
Above ways are really helpful for you.
Thanks in advance.
I just got some errors in my Java oracle connectivity. Could anyone please help me with this? I have enclosed the code below. I'm getting this error:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver..
this is the code
package md5IntegrityCheck;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class MD5IntegrityCheck
{
public static void main(String[] args)
{
String fileName,Md5checksum ,sql;
Connection con;
PreparedStatement pst;
Statement stmt;
ResultSet rs;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con1 =DriverManager.getConnection("jdbc:odbc:RecordTbl","scott","tiger");
}
catch(Exception ee)
{ee.printStackTrace( );}
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
/****insert method******/
private static void setDefaultCloseOperation(String exitOnClose) {
// TODO Auto-generated method stub
}
static void setVisible(boolean b) {
// TODO Auto-generated method stub
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:RecordTbl","scott","tiger");
PreparedStatement pst = con.prepareStatement("insert into RecordTbl values(?,?)");
String fileName = null;
pst.setString(1,fileName);
String Md5checksum = null;
pst.setString(2,Md5checksum);
int i=pst.executeUpdate( );
System.out.println("recorded in database");
con.close( );
}
catch(Exception ee)
{ee.printStackTrace( );}
}
}
if (args.length <= 0)
{
Md5Gui gui = new Md5Gui();
gui.runGui();
}
else
{
DoWork runningProgram = new DoWork();
runningProgram.run(args);
}
}
}
Your question is vague:
In your exception, you're getting a ClassNotFoundException for a driver that pertains to MySQL. On your code, you're using a JDBC-ODBC Driver.
My suggestion is how did you configure your database connectivity. Let's start from there. Also, it would be better to add the exception stack trace to see exactly what's happening.
Edit: Visit this example if you want to know how to configure JDBC connection to Oracle Database. I fully recommend using the Oracle JDBC driver directly instead of connecting it to an ODBC Bridge.
I assume you might be running your program in IDE, so please add drivers jars in the classpath of project
You should look into any 3rd party library you're using whether there a MySQL database driver is needed. Although you write you are using an Oracle driver (though the JdbcOdbcDriver is provided by Java itself and has nothing to do with Oracle DB's) the exception is clearly stating that the MySQL is requested. Since you don't use it in the code you provided, there must be another database connection using MySQL.
I am using one simple code to access the SQLite database from Java application .
My code is
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ConnectSQLite
{
public static void main(String[] args)
{
Connection connection = null;
ResultSet resultSet = null;
Statement statement = null;
try
{
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:D:\\testdb.db");
statement = connection.createStatement();
resultSet = statement
.executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
while (resultSet.next())
{
System.out.println("EMPLOYEE NAME:"
+ resultSet.getString("EMPNAME"));
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
resultSet.close();
statement.close();
connection.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
But this code gives one exception like
java.lang.ClassNotFoundException: org.sqlite.JDBC
How can I slove this,please help me.
You need to have a SQLite JDBC driver in your classpath.
Taro L. Saito (xerial) forked the Zentus project and now maintains it under the name sqlite-jdbc. It bundles the native drivers for major platforms so you don't need to configure them separately.
If you are using netbeans Download the sqlitejdbc driver
Right click the Libraries folder from the Project window and select Add Library ,
then click on the Create button enter the Library name (SQLite) and hit OK
You have to add the sqlitejdbc driver to the class path , click on the
Add Jar/Folder.. button and select the sqlitejdbc file you've downloaded previously
Hit OK and you are ready to go !
If you are using Netbeans using Maven to add library is easier. I have tried using above solutions but it didn't work.
<dependencies>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version>
</dependency>
</dependencies>
I have added Maven dependency and java.lang.ClassNotFoundException: org.sqlite.JDBC error gone.
I'm using Eclipse and I copied your code and got the same error. I then opened up the project properties->Java Build Path -> Libraries->Add External JARs...
c:\jrun4\lib\sqlitejdbc-v056.jar
Worked like a charm. You may need to restart your web server if you've just copied the .jar file.
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import org.sqlite.SQLiteDataSource;
import org.sqlite.SQLiteJDBCLoader;
public class Test {
public static final boolean Connected() {
boolean initialize = SQLiteJDBCLoader.initialize();
SQLiteDataSource dataSource = new SQLiteDataSource();
dataSource.setUrl("jdbc:sqlite:/home/users.sqlite");
int i=0;
try {
ResultSet executeQuery = dataSource.getConnection()
.createStatement().executeQuery("select * from \"Table\"");
while (executeQuery.next()) {
i++;
System.out.println("out: "+executeQuery.getMetaData().getColumnLabel(i));
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
}
return initialize;
}
You have to download and add the SQLite JDBC driver to your classpath.
You can download from here https://bitbucket.org/xerial/sqlite-jdbc/downloads
If you use Gradle, you will only have to add the SQLite dependency:
dependencies {
compile 'org.xerial:sqlite-jdbc:3.8.11.2'
}
Next thing you have to do is to initialize the driver:
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException eString) {
System.err.println("Could not init JDBC driver - driver not found");
}
connection = DriverManager.getConnection("jdbc:sqlite:D:\\testdb.db");
Instead of this put
connection = DriverManager.getConnection("jdbc:sqlite:D:\\testdb");
Hey i have posted a video tutorial on youtube about this, you can check that and you can find here the sample code :
http://myfundatimemachine.blogspot.in/2012/06/database-connection-to-java-application.html
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class Connectdatabase {
Connection con = null;
public static Connection ConnecrDb(){
try{
//String dir = System.getProperty("user.dir");
Class.forName("org.sqlite.JDBC");
Connection con = DriverManager.getConnection("jdbc:sqlite:D:\\testdb.db");
return con;
}
catch(ClassNotFoundException | SQLException e){
JOptionPane.showMessageDialog(null,"Problem with connection of database");
return null;
}
}
}