JDBC SQL Server errors: ClassNotFound & Not Suitable Driver Found - java

I am very new to Java and am simply trying to connect to my MSSQL database and return a list of customers. When I try the JDBC connection, I get a "no suitable driver found" error. When I add a Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver") statement, I get a ClassNotFound error. This seems like it should be a lot easier than it's turning out to be. Please help me either find a suitable driver or how to get access through the Class.forName usage.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;
public class DbConn {
public static String getConnString(){
return "jdbc:sqlserver://localhost\\SQLEXPRESS:1433;database=OhHold;";
}
public static void getConnection() {
try
{
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String user = "<USER>";
String pw = "****************";
Connection connection = DriverManager.getConnection(getConnString(), user, pw);
Statement statement = connection.createStatement();
String sql = "select txtCompanyName as company from tblCustomers where intNotActive <> 1";
ResultSet result = statement.executeQuery(sql);
while (result.next()) {
System.out.println(result.getString(1));
}
}
/*
// Handle any errors that may have occurred.
catch (ClassNotFoundException e) {
e.printStackTrace();
}
*/
catch (SQLException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
getConnection();
}
}

Related

No suitable driver found error even if the my-sql connector uploaded into the library

I am new to java and in a learning process. I'm working on connecting my JDE with MySql and I have followed every step necessary. but when I run the code, I got " No suitable driver found for jdbc.mysql://localhost:3306/dbname" error. I reviewed questions already in stackoverflow and other sources; but the provided solution didn’t work for me.
Any suggestions why i got this error even if I uploaded the mysql-connector-j-8.0.31/mysql-connector-j-8.0.31.jar.a screenshot of my code and the error message
package JDBC;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
public class JDBC {
public static void main(String[] args) throws SQLException{
String url = "jdbc.mysql://localhost:3306/University";
String username = "root";
String password = "root";
String query = "select * from EngineeringStudents";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(ClassNotFoundException e) {
//To do auto-generated catch block
e.printStackTrace();
}
try {
Connection con = DriverManager.getConnection(url, username, password);
Statement statement = con.createStatement();
ResultSet result = statement.executeQuery(query);
while(result.next()) {
String UniversityData = "";
for(int i = 1; i <= 6; i++) {
UniversityData += result.getString(i) + ":";
}
System.out.println(UniversityData);
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
Bro use String url = "jdbc:mysql://localhost:3306/University". Use have missed a colon ':' after jdbc instead you're using '.'

CData JDBC Couchbase Java app connection problem

I can't manage to get this right. I'm not sure of what is wrong. Apparently the connection is ok but can't get the result back of the query.
package probandoCouch;
import cdata.jdbc.couchbase.CouchbaseDriver;
import java.sql.Statement;
import java.util.Properties;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class App {
public static void main(String[] args) {
try {
Connection conn = DriverManager.getConnection(
"jdbc:couchbase:User=\"Administrator\";Password=\"Administrator\";Server=\"127.0.0.1\";");
Statement stat = conn.createStatement();
boolean ret = stat.execute("SELECT message FROM greeting WHERE author='foo';");
if (ret) {
ResultSet rs = stat.getResultSet();
while (rs.next()) {
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
System.out.println(rs.getMetaData().getColumnName(i) + "=" + rs.getString(i));
}
}
}
} catch (SQLException e) {
}
}
}
Can you please remove semicolon(;) from query and try
SELECT message FROM greeting WHERE author='foo';
Updated Query:
SELECT message FROM greeting WHERE author='foo'
With semicolon jdbc will fail with error
java.sql.SQLException: ORA-00933: SQL command not properly ended
In your code you may print the exception to see if any exception is thrown.

Table not found error in eclipse IDE, what to do?

I recently started working on JDBC and I got an Error as Table not found
Table TRIAL not found; SQL statement:
insert into trial values(1,'hello') [42102-73]
Here is my code, also I have already created a table in h2 DB
package trial1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Customer {
public static void main(String[] args) {
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:h2:~/test","Aziz", "password");
Statement st = conn.createStatement();
String str = "insert into trial values(1,'hello')";
st.execute(str);
System.out.println("Succes");
}
catch(Exception e) {
e.printStackTrace();
}
}
}

JDBC to hive connection fails on invalid operation isValid()

I have followed this doc to try to set up a jdbc connection to hive. But eclipse shows this error. Not seem to figure out what it exactly means and the connection with appropriate password and username works in beeline so its not the problem of authentication.Below is the error i'm facing:
> 15/11/27 13:15:41 INFO jdbc.Utils: Supplied authorities: localhost:10000
> 15/11/27 13:15:41 INFO jdbc.Utils: Resolved authority: localhost:10000
> 15/11/27 13:15:41 INFO jdbc.HiveConnection: Will try to open client transport with JDBC Uri: jdbc:hive2://localhost:10000/default
> Exception in thread "main" java.sql.SQLException: Method not supported
at org.apache.hive.jdbc.HiveConnection.isValid(HiveConnection.java:1026)
at HiveJDBC.main(HiveJDBC.java:21)
here is the code:
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveJDBC {
private static String driverName = "org.apache.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:hive2://localhost:10000/default", "hive", "PASSWORD");
if(con.isValid(0)){
System.out.println("success");
}else{
System.out.println("fail");
}
Statement stmt = con.createStatement();
String tableName = "tabledriver";
//stmt.executeQuery("create database " + tableName);
}
}
Your call to
if(con.isValid(0)){
is legal - but not implemented by the Hive JDBC Driver.
See Hive Source:
#Override
public boolean isValid(int timeout) throws SQLException {
// TODO Auto-generated method stub
throw new SQLException("Method not supported");
}
Replace the check with a simple if(con != null) and you'll be fine.

Is this can be the simplest way of executing queries in JDBC (JAVA) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
In the process of learning of JDBC , I created below interface and classes for easy programming in future use . I believe still there is a possibility of doing simpler than this . What's my program does is getting results by giving Query String instead of doing all procedures like loading driver class, creating Connection and Statement objects and getting results from them. Will there be any simpler way of doing this and enhance the features in future ??
MyConnection.java ---- Interface
package com.cherukuri.jdbc;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public interface MyConnection {
public static String DRIVER = "com.mysql.jdbc.Driver";
public static String DB_URL = "jdbc:mysql://localhost:3305/STUDENTS";
public static String USER = "root";
public static String PASSWORD = “********";
default void loadDriver() {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public Connection getConnection();
public Statement getStatement();
}
DataBaseConnection.java
package com.cherukuri.jdbc;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class DataBaseConnection implements MyConnection {
Connection connection = null;
Statement statement = null;
#Override
public Connection getConnection() {
try {
connection = (Connection) DriverManager.getConnection(DB_URL, USER,
PASSWORD);
} catch (SQLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return connection;
}
#Override
public Statement getStatement() {
connection = getConnection();
try {
statement = (Statement) connection.createStatement();
} catch (SQLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return statement;
}
public ResultSet runQuery(String Query) {
ResultSet resultSet = null;
statement = getStatement();
try {
resultSet = statement.executeQuery(Query);
} catch (SQLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return resultSet;
}
}
TestBaseConnection.java
package com.cherukuri.jdbc;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestDataBaseConnection {
public static void main(String[] args) {
DataBaseConnection dbcon = new DataBaseConnection();
ResultSet rs = dbcon.runQuery("Select * from tblStudent");
try {
while (rs.next()) {
System.out.println(rs.getInt(1) + " ------------------- "
+ rs.getString(2));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
You can try Spring JDBC which simplifies JDBC access in a way similar to your wrapper classes. It removes the need for some bolierplate code and manages the database connection for you.

Categories