2

Is there any other way to connect to the database other than making a jdbc connection or using frameworks which internally use jdbc.

Woodchuck
  • 3,869
  • 2
  • 39
  • 70
Ankur
  • 53
  • 1
  • 2

2 Answers2

6

Sure, it's possible. But not likely.

You'd have to understand the protocol that the database vendor expected completely.

You'd have to write your own client to converse with the vendor's listener on the server side.

It can be done, but I doubt that you or I would be up to it.

The big question is: why?

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

You can use JDBC ODBC Bridge ... not recommended though..

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();
    // st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    ResultSetMetaData rsMetaData = rs.getMetaData();

    int numberOfColumns = rsMetaData.getColumnCount();
    System.out.println("resultSet MetaData column Count=" + numberOfColumns);

    st.close();
    conn.close();
  }

  private static Connection getConnection() throws Exception {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String url = "jdbc:odbc:northwind";
    String username = "";
    String password = "";
    Class.forName(driver);
    return DriverManager.getConnection(url, username, password);
  }
}
Waqas Memon
  • 1,247
  • 9
  • 22
  • 1
    Not available in 64 bit JVMs. And this is still JDBC. – duffymo Apr 05 '13 at 10:51
  • but we are not using any third party drivers...... – Waqas Memon Apr 05 '13 at 10:53
  • 2
    Yes you are, you just don't know it. You're using the bridge driver that Oracle/Sun wrote. They're a third party. And it's still JDBC. See the first four letters in the driver and connection URL? – duffymo Apr 05 '13 at 10:54
  • http://stackoverflow.com/questions/3771594/unix-socket-connection-to-mysql-with-java-to-avoid-jdbcs-tcp-ip-overhead – Waqas Memon Apr 05 '13 at 10:56
  • 1
    Right, but that's not what your code says. And they are agreeing with my recommendation: write your own client from scratch, understanding the protocol completely. You're making my point. – duffymo Apr 05 '13 at 10:58
  • then you know the answer... why would someone like to do that anyway... do you really need any such thing? – Waqas Memon Apr 05 '13 at 10:59
  • yes indeed i did make ur point. i just pasted the right answer ... so, one needs reinvent the wheel rather then use it if this question is ever to be answered. – Waqas Memon Nov 07 '17 at 00:38
  • Four and a half years later and you're coming back with this comment? – duffymo Nov 07 '17 at 13:05