Marius
Marius

Reputation: 11

Java: Connect to Database and return query result

I am trying to connect to a SQL Server database in Java, execute a query, and return the result to the calling script.

class DB { public DB() {}

        public Connection dbConnect(String db_connect_string)
        {
                try
                {
                        Class.forName(
                          "com.microsoft.jdbc.sqlserver.SQLServerDriver");

                        Connection conn =
                          DriverManager.getConnection(db_connect_string);

                        System.out.println("connected");
                        return conn;

                }
                catch (Exception e)
                {
                        e.printStackTrace();
                        return true;
                }
        }
};
class getConnection
{
        public void main(String[] args)
        {
                DB db = new DB();
                Connection conn=db.dbConnect(
          "jdbc:microsoft:sqlserver://host:1433;User=user;Password=Pwd");
        }

}
return true;

As far as I can tell this tells me the connection is fine, but my Java is less than limited. Can someone tell me if this part is fine and add a simple select statement to it, which is then passed back via return?

Upvotes: 1

Views: 4059

Answers (2)

Umesh Kacha
Umesh Kacha

Reputation: 13656

May be you should brush up how to use JDBC

public Statement getStatement()
{

      return conObject.createStatement();

} 

Upvotes: 2

adarshr
adarshr

Reputation: 62573

Perhaps you need to do some reading up.

http://download.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html

Upvotes: 2

Related Questions