How to connect to db by piping password - java

Im trying to connect to database using java...
I used the following snipphet but am unable to connect to db
p = Runtime.getRuntime().exec("ssh -p 4645 username#example.com|send password");

for connecting a database you don't need to use Runtime.getRuntime() its is use to execute system command in java for connecting any database in java you have to use its jar file like for and example i want to connect my code to the mysql database the for that i need mysql jar file and calling its lib in my code like:
import java.sql.*;
class demo
{
public static void main(String ar[])throws ClassNotFoundException, SQLException
{
Class.forName("com.mysql.jdbc.Driver")
Connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","root", "password");
Statement st = con.createStatement();
/*
your sql query here
*/
}
}

Related

Java JDBC Driver stops working after initial execution [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 1 year ago.
I am currently making a backend reporting system (for a voting system assignment) using Java on VS Code, I am connecting to a MySQL database using the JDBC library in order to do calculations and stats and so on. So what happens is that once I create a project file and include the mysql-connector-java-8.0.25.jar in the referenced libraries, I can connect to the DB and retrieve data from the tables just fine, but after a few executions I no longer get output and it shows me the error "java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver".
Can anyone tell me why this is happening and how to fix this? There are no changes that I know of taking place in the Environment Variables (at least from what I can see in Windows path list) unless something is being overwritten somewhere or that it's a bug of some sort. Any advice would be greatly helpful, I've been unable to figure this out all day
This is what my ReportSystem.java looks like...
import java.sql.*;
public class ReportSystem
{
public static void main(String[] args)
{
//Test driver connection/registration
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ElectionDB","<username>","<password>");
Statement stmt = conn.createStatement();
ResultSet result = stmt.executeQuery("SELECT * FROM ElectionDB.Votes");
int typeColumn = 1;
int districtColumn = 2;
//Output results line by line
while(result.next())
{
System.out.println(result.getString(typeColumn));
System.out.println(result.getString(districtColumn));
}
//Remember to close the connection
conn.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
My file structure as in the directory is as follows:
ReportSystem
>src > ReportSystem.java
> ReportSystem.class
>lib
>.vscode > settings.json
The JRE system library used is: [jdk-16.0.1]
The Referenced Libraries contains: [mysql-connector-java-8.0.25.jar]
Screenshot for context Project Setup in VS Code
I'm able to get result after running code 20 times continuously by clicking the run button. The only difference is the JDBC connection string, which is copied directly by right clicking the MYSQL connection:
So in my project, the connection string is like:
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/?user=username","<username>","<password>");
OR
You could try
String url="jdbc:mysql://localhost:3306/ElectionDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"
For your reference, OS Infomation:
MySQL: 8.0 // mysql-connector-java-8.0.25.jar
VSCode: 1.56.2 //
java.home: JDK16 // Debugger for Java: 0.33.1

Why do I get a No suitable driver found error? [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
What is a classpath and how do I set it?
(10 answers)
Closed 2 years ago.
I'm trying to connect to a database on localhost. Mysql is running and the database name is employees. I confirmed that the port, username, and password are correct.
The java, class, and jar file are in the same folder. I tried adding the jar file to my CLASSPATH in the system environment variables and I tried adding it using -cp like below.
javac -cp . relearnjdbc.java
javac -cp ./mysql-connector-java-8.0.20.jar relearnjdbc.java
javac -cp *.jar relearnjdbc.java
I also tried separating the files into their own folders src, class, and bin.
javac -cp ../bin -d ../class relearnjdbc.java
This is my code
public class relearnjdbc {
public static String url = "jdbc:mysql://localhost:3306/employees";
public static String username = "root";
public static String password = "root";
public static void main(String[] args){
System.out.println("Connecting to DB...");
try{
//Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.jdbc.Driver").newInstance();
//Connection connection = DriverManager.getConnection(url, username, password);
DriverManager.getConnection(url, username, password);
System.out.println("Connected!");
}catch (Exception e){
throw new IllegalStateException("cannot connect to DB ", e);
}
}
}
These give java.lang.ClassNotFoundException. I know its deprecated, but it was in a lot of answers to similar questions.
Class.forName("com.mysql.jdbc.Driver").newInstance();
Class.forName("com.mysql.jdbc.Driver");
I'm not using an IDE, tomcat, netbeans, apache, phpadmin, or anything else. As far as I can tell a lot of other people that have asked this question were using one of these or they didn't have the jar file in their classpath.
The name of the class that implements java.sql.Driver in MySQL Connector/J has changed from com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver. The old class name has been deprecated docs
try Class.forName("com.mysql.cj.jdbc.Driver").newInstance();

Exception in thread "main" java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver

I am new in java Jdbc I am going to write simple jdbc program `
import java.sql.*;
class jdbcDemo
{
public static void main(String[] args) throws Exception
{
Class.forName("oracle.jdbc.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe","Scott","tiger");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from rk");
while(rs.next())
{
System.out.println(rs.getInt(1)+"---"+rs.getString(2)+"---"+rs.getInt(3));
}
con.close();
}
}
after Successful compiling program
At runtime i found following error
I had used editplus editor
and also i had created rk table in oracle 12c database
need help to fix issue
You need to provide the class for oracle driver. It will be in a jar which needs to be in class path. Since you are running from command prompt, this needs to be passed to run the class successfully.
As you load class of oracle driver in your code :
Class.forName("oracle.jdbc.OracleDriver");
you just need to add oracle jdbc drivers(ojdbc6 or ojdbc7 ) into your project .
you can download this jar file from thislink :
JDBC DOWNLOAD LINK

Why I obtain this SQLException (No suitable driver) when I try to create a Connection object for an Oracle database?

I have to create a very simple batch Java application (an application that run into shell) and that perform some query on an Oracle database using JDBC and I never done it.
I am following this tutorial: http://www.ntu.edu.sg/home/ehchua/programming/java/JDBC_Basic.html
So I have done in this way to allocate a new Connection object for my application:
import java.sql.*;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World !!!");
String partitaIVA = args[0];
String nomePDF = args[1];
Connection conn = null;
Statement stmt = null;
try {
// Step 1: Allocate a database "Connection" object
conn = DriverManager.getConnection("jdbc:oracle:thin:#XXX.XXX.XXX.XXX:1521:eme1", "myUserName", "myPswd"); // Oracle DB
} catch(SQLException ex) {
ex.printStackTrace();
}
}
}
The problem is that when I run the application it seems that the SQLException is thrown because enter into the catch block and print the following error message in the console:
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:#XXX.XXX.XXX.XXX:1521:eme1
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at Main.main(Main.java:16)
Why? What cause this problem and how can I fix this issue? What am I missing?
Tnx
You need to have an appropriate ojdbc.jar in you classpath. E.g. see Oracle JDBC ojdbc6 Jar as a Maven Dependency
The problem should be that the linked tutorial describes how to connect to a Mysql, but you're trying to connect to an oracle - therefore you need the oracle driver in your classpath.
How about following oracle's documentation for its Driver. http://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html
In the above code, you didn't registered the driver class.
Class.forName ("oracle.jdbc.OracleDriver");
If you are following instructions from the given link.
In chapter 2.2 you have instructions how to instal MySql drivers.
If you are using an Oracle database, then you'll need to instal Oracle JDBC drivers.
If you don't want affect your JDK installation like in tutorial, you can load driver dynamicaly

Java SQL error, no suitable driver found

I'm trying to compile this small piece of code, to help me connect to my db and retrieve some information to test it. I am using Netbeans on a Windows 7 x64 machine. This is the code:
package passwordprotector;
import java.sql.*;
public class PasswordProtector {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
String host = "jdbc:derby://localhost:1527/PasswordProtector DB";
String dbUsername = "john";
String dbPassword = "arsenal";
/*try{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
}catch(ClassNotFoundException e){
System.out.println(e);
}*/
try{
Connection con = DriverManager.getConnection(host, dbUsername, dbPassword);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM APP.PERSON");
while (rs.next()) {
String uName = rs.getString("uname");
String uPass = rs.getString("upass");
System.out.println("Username: " + uName + "/n" + "Password: " + uPass);
}
}catch(SQLException e){
System.err.println(e);
}
}
}
When I compile and run I receive this error:
java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/PasswordProtector DB
BUILD SUCCESSFUL (total time: 0 seconds)
When I right click on my db and select properties I can see it's location, like so:
Database URL: jdbc:derby://localhost:1527/PasswordProtector
I've checked with others who have posted about this and it seems they had an incorrect URL as the issue, but I can't see any other URL which I can use apart from the one posted.
I've tried with and without the ending ' DB' for the String host, neither works.
I've also already read from here and still couldn't figure out why the URl is incorrect:
Not sure the problem with the database URL connection, but in the usage of the correct driver. If the database is embedded you should use the driver commented in your post and example from my answer, there's also tutorial embedded derby.
if not then use
Class.forName("org.apache.derby.jdbc.ClientDriver");
It's a different driver to connect to the database running standalone. In this case see derby network client documentation how to configure and run derby network client.
Make sure derbyrun.jar is in your classpath. It resides in the db/lib directory of your JDK.
Doing a quick search on Maven and Derby, included the following in my pom:
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.10.2.0</version>
</dependency>
and everything worked afterwards, so it may be a library reference issue if the previous solution did not work.

Categories

Resources