Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
package com.example.prejtds;

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



import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
Button show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = (Button) findViewById(R.id.btn_show);

show.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

query2(); }
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public void query2()
{
Log.i("Android"," SQL Connect Example.");
Connection conn = null;
try {
String driver = "net.sourceforge.jtds.jdbc.Driver";

Class.forName(driver).newInstance();

//SQL Server name=HNI-03-PC,Computer Name=HNI-03, user=nikhil502, password=ocean502, //instance=MSSQLSERVER.

String connString ="jdbc:jtds:sqlserver://HNI-03-PC/nikhil;instance=MSSQLSERVER";
conn = DriverManager.getConnection(connString,"nikhil502","ocean502");

Log.w("Connection","open");
Toast.makeText(MainActivity.this, "connection estabilished",Toast.LENGTH_LONG).show();
Statement stmt = conn.createStatement();
ResultSet reset = stmt.executeQuery("select * from emp_details");

while(reset.next()){
Log.w("Data:",reset.getString(3));
Log.w("Data",reset.getString(2)); }
conn.close();

} catch(SQLException e){
Log.w("Error connection","" + e.getMessage());


} catch (Exception e)
{
Log.w("Error connection","" + e.getMessage());
}

}

}
Posted
Comments
NIKHIL CHAVAN 13-Mar-14 5:59am    
I had already enabled all TCP/IP addresses and Port to 1433 for all.
sql server has SQL SERVER AUTHENTICATION...
Shubhashish_Mandal 18-Mar-14 13:36pm    
stacktrace?
NIKHIL CHAVAN 24-Mar-14 0:41am    
The stacktrace is ?

03-14 11:44:03.476: W/System.err(17039): java.sql.SQLException: Unable to get information from SQL Server: localhost.
03-14 11:44:03.484: W/System.err(17039): at net.sourceforge.jtds.jdbc.MSSqlServerInfo.<init>(MSSqlServerInfo.java:97)
03-14 11:44:03.484: W/System.err(17039):at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:276)
03-14 11:44:03.484: W/System.err(17039):at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:187)
03-14 11:44:03.484: W/System.err(17039):at java.sql.DriverManager.getConnection(DriverManager.java:180)
03-14 11:44:03.484: W/System.err(17039):at java.sql.DriverManager.getConnection(DriverManager.java:145)
03-14 11:44:03.484: W/System.err(17039): at com.example.prejtds.MainActivity.query2(MainActivity.java:54)
03-14 11:44:03.484: W/System.err(17039): at com.example.prejtds.MainActivity$1.onClick(MainActivity.java:33)
03-14 11:44:03.484: W/System.err(17039):at android.view.View.performClick(View.java:2485)
03-14 11:44:03.484: W/System.err(17039):at android.view.View$PerformClick.run(View.java:9080)
03-14 11:44:03.492: W/System.err(17039):at android.os.Handler.handleCallback(Handler.java:587)
03-14 11:44:03.492: W/System.err(17039):at android.os.Handler.dispatchMessage(Handler.java:92)
03-14 11:44:03.492: W/System.err(17039):at android.os.Looper.loop(Looper.java:130)
03-14 11:44:03.492: W/System.err(17039):at android.app.ActivityThread.main(ActivityThread.java:3687)
03-14 11:44:03.492: W/System.err(17039):at java.lang.reflect.Method.invokeNative(Native Method)
03-14 11:44:03.492: W/System.err(17039):at java.lang.reflect.Method.invoke(Method.java:507)
03-14 11:44:03.492: W/System.err(17039): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
03-14 11:44:03.492: W/System.err(17039):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
03-14 11:44:03.492: W/System.err(17039):at dalvik.system.NativeStart.main(Native Method)
Shubhashish_Mandal 25-Mar-14 14:58pm    
check this http://jtds.sourceforge.net/faq.html#instanceGetInfo and make sure you did the correct set up.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900