Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have problem to connect database that connect throw jtds webservice, I have include my coding please anyone rectify my problem...


Java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import net.sourceforge.jtds.jdbc.*; 

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



public class Createaccount extends Activity {
	Button btncreate;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
       btncreate=(Button)findViewById(R.id.btncreate);
    
       btncreate.setOnClickListener(new OnClickListener() {    	    
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//String connectionUrl = "jdbc:sqlserver://10.0.2.2:1433;"+"databaseName=click4grocer;integratedSecurity=true;";
			String connectionUrl="jdbc:jtds:sqlserver://server_ip_address:10.0.0.27:1433//databaseName=click4grocer;encrypt=fasle;user=sa;password=sa;instance=SQLEXPRESS;";
	          // Declare the JDBC objects.
	          Connection con = null;
	          Statement stmt = null;
	          
	          try {
	              // Establish the connection.
	        	  String driver = "net.sourceforge.jtds.jdbc.Driver";
	              Class.forName(driver);
	              //Class.forName("net.sourceforge.jtds.jdbc.Driver");
	              //Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	              
	              con = DriverManager.getConnection(connectionUrl);
	            //Get information from EditText
		             EditText txtusername = (EditText)findViewById(R.id.txtusername);
		             EditText txtpassword = (EditText)findViewById(R.id.txtpassword);
		             EditText txtconpass = (EditText)findViewById(R.id.txtconpass);
		             EditText txtemail = (EditText)findViewById(R.id.txtemail);
		             EditText txtaddress = (EditText)findViewById(R.id.txtaddress);
		             EditText txtphone = (EditText)findViewById(R.id.txtphone);
		             EditText txtblood = (EditText)findViewById(R.id.txtblood);
		             
		           //get values from Textbox
		             String username = txtusername.getText().toString();
		             String password = txtpassword.getText().toString();
		             String conpassword = txtconpass.getText().toString();
		             String email = txtemail.getText().toString();
		             String address = txtaddress.getText().toString();
		             String phone = txtphone.getText().toString();
		             String blood = txtblood.getText().toString();
		             
		             //if required field is missing this msg will show.
		             if(username.equals("")||password.equals("")||conpassword.equals(""))
		             {
		                     Toast.makeText(getApplicationContext(), "Fill the empty field", Toast.LENGTH_LONG).show();
		                     return;
		             }
		             // check if both password matches
		             if(!password.equals(conpassword))
		             {
		                 Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
		                 return;
		             }
		             
		             
		          // Create and execute an SQL statement that returns some data.
		             String SQL = "insert into login values('" + username + "', '" + password + "','" + conpassword + "','" + email + "','" + address + "','" + phone + "','" + blood + "');";
		             stmt = con.createStatement();
		             stmt.executeUpdate(SQL);
		             Log.e("Success", "Success");		             
		             //shows the message if account created succesfully. 
		             Toast.makeText(getApplicationContext(),"Account created", Toast.LENGTH_SHORT).show();
		          }
	          
	             // Handle any errors that may have occurred.
	                 catch (Exception e) {
	                 e.printStackTrace();
	                 Log.e("Error", e.toString());
	                 Toast.makeText(getApplicationContext(),"Account is not created", Toast.LENGTH_SHORT).show();
	              }
	          
	                 finally {
	                   if (stmt != null) try { stmt.close(); } catch(Exception e) {}
	                    if (con != null) try { con.close(); } catch(Exception e) {}
	           }
	          
		}
	});
    }  
}
Posted
Updated 19-Sep-14 21:50pm
v2
Comments
Richard MacCutchan 20-Sep-14 3:51am    
Please show where the error occurs, and provide the complete text of the exception.
Vijay2106 20-Sep-14 8:00am    
sir i check my log there is no error msg is shown.when i compile all are compiled successfully but i click the createaccount button it comes to trigger the catch part shows the account is not created message throws my project.

1 solution

Try this Code snippet:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
 
import net.sourceforge.jtds.jdbc.*;
 
public void query2()
{
Log.i("Android"," MySQL Connect Example.");
Connection conn = null;
try {
String driver = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver).newInstance();
//test = com.microsoft.sqlserver.jdbc.SQLServerDriver.class;
String connString = "jdbc:jtds:sqlserver://server_ip_address :1433/DBNAME;encrypt=fasle;user=xxxxxxxxx;password=xxxxxxxx;instance=SQLEXPRESS;";
String username = "xxxxxx";
String password = "xxxxxxxxxx";
conn = DriverManager.getConnection(connString,username,password);
Log.w("Connection","open");
Statement stmt = conn.createStatement();
ResultSet reset = stmt.executeQuery("select * from TableName");
 
//Print the data to the console
while(reset.next()){
Log.w("Data:",reset.getString(3));
//              Log.w("Data",reset.getString(2));
}
conn.close();
 
} catch (Exception e)
{
Log.w("Error connection","" + e.getMessage());
}
}
 
Share this answer
 

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