How to resolve sqlite communication error in java IntelliJ - java

I'm trying to connect to a sql database with java but this isn't really working out. normally people say they have a user and password but I never created anything like that. I downloaded sqlite studio and you can pretty much just make a database without having to create some kind of account but when i'm trying to connect it gives me an error that it can't connect com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure.
How do I resolve this issue? This is the code I have right now:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class MyJDBC {
public static void main(String[] args) {
try {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/JDBC-video");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from people");
while (resultSet.next()) {
System.out.println(resultSet.getString("firstname"));
}
}
catch (Exception e){
e.printStackTrace();
}
}
}

Related

connecting to database(postgres) with java in intellij

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

Too many parameter error in jdbc

I am using jdbc connectivity over ms access database
and here is my code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ExcelConnectivity
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:db");
String query="update validation set validation.rackid=rack.rackid where rack.bookid=validation.bookid";
PreparedStatement ps=con.prepareStatement(query);
ps.executeUpdate();
System.out.println("doneeeeeeeeeeeeeeeeeeeeee");
}
catch(SQLException | ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
now the database is as follow
now the error occurring as
[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2.
I have checked the table name in database and jdbc code, checked connection
any anyone help me in error
The query you have used is incorrect, it has nothing to do with the connectivity or connection. The error is trying to convey that you are using parameters in your query but not supplying the values at the execution.
update validation set validation.rackid=rack.rackid where rack.bookid=validation.bookid
where would it pick the rack values from ?

Hive server not starting

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.

Java Connect to a MySQL's sites DB

I have a site with a MySql Database, and I would like to retrieve the information via a Java Program. The problem is that I'm not sure about how to do it. I have tried a few methods, but none works.
Long story short, I need help with the following :
finding the IP of the server
connecting to the database via IP
creating a new connection with the details
I have tried the following : DriverManager.getConnection("jdbc:mysql://DOMAIN:3306/DB_NAME", "USER", "PASSWORD"); but doesn't work.
Thanks in advanced, and I apologize if the question is stupid, but I have no Java experience with DB's, and I can't understand how can a link between those 2 entities be established.
edit
The Class is the following
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class test {
/**
* #param args
*/
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://IP:3306/DB","USER", "PASS");
Statement statement = connection.createStatement();
ResultSet resultSet = statement .executeQuery("SELECT * FROM `categorii`");
System.out.println( resultSet.getNString(3));
}
catch (Exception M)
{
System.out.println(M.getMessage());
}
}
}
An Exception is thrown which says :
'Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.'
So it can't connect to the server, but I can't see why.
I've always had problems using this:
DriverManager.getConnection("jdbc:mysql://DOMAIN:3306/DB_NAME", "USER", "PASSWORD");
Try this instead
DriverManager.getConnection("jdbc:mysql://DOMAIN:3306/DB_NAME?user=USER&password=PASSWORD");

Connecting Berkely DB in java through sqlitejdbc-v056.jar

i am connecting berkely database through sqlitejdbc-v056.jar & i got the following error and not performing insert ,update ,delete opration
java.sql.SQLException: database is locked
at org.sqlite.DB.throwex(DB.java:288)
at org.sqlite.NativeDB.prepare(Native Method)
at org.sqlite.DB.prepare(DB.java:114)
at org.sqlite.Stmt.executeUpdate(Stmt.java:102)
at testdb.TestProgram.main(TestProgram.java:37)
download jar from following link http://www.zentus.com/sqlitejdbc/
i am using following code for performing
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import org.sqlite.*;
/**
*
* #author dhananjay.joshi
*/
public class TestProgram
{
public static void main(String args[])
{
Connection con=null;
Statement smt=null;
ResultSet rs = null;
try
{
Class.forName("org.sqlite.JDBC");
System.out.print("Connnection req");
con = DriverManager.getConnection("jdbc:sqlite:C:\\Prog\\Emp.db");
smt = con.createStatement();
System.out.println("\n Connected");
String que = "insert into student values(2,'kiran')";
smt.executeUpdate("insert into student values(2,'kiran');");
System.out.print("Insert Data");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
SQLite is a light weight SQL database. Berkley-DB is not an SQL database. They both have nothing to do with each other.
If you want to read/write a Berkley DB file you need a jar file with the helper classes for Berkley-DB.
A google search for berkeley db java should help you.

Categories

Resources