Home > Mobile >  Android Studion 4.0 connect to the database Sqlserver2008R2 problem
Android Studion 4.0 connect to the database Sqlserver2008R2 problem

Time:09-20

Hello, I'm a beginner, there is a question, use the Android Sqlserver2008r2 Studio 4.0 connections, database, stuck in
DriverManager. GetConnection (" JDBC: JTDS: essentially://XXX. XXX, XXX, XXX: 1433/db; Charset=utf8 ", "sa", "111111111"); This line has no return value, compiled, now also can't connect to the database, (already on the Internet looking for a long time not solve, idiopathic this post), thank you, first screenshot below:


MainActivity. Java, content:
Package com. Example. H2sqlserverconnect;

The import androidx. Appcompat. App. AppCompatActivity;

The import android. Content. Intent;
The import android. OS. Bundle;
The import android. View. The view;
The import android. Widget. The Button;
The import android. Widget. The EditText;
The import android. Widget. TextView;
The import android. Widget. Toast;
Import the Java. SQL. DriverManager;
Import the Java. SQL. The ResultSet;
Import Java, SQL Statement;
Import the Java. SQL. Connection;
Import the Java. SQL. SQLException;

Public class MainActivity extends AppCompatActivity {

TextView textView2;
The EditText editTextTextPersonName;
The EditText editTextTextPersonName2;
The Button Button;
The Button buton2;
The Button button3;

@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView2=the findViewById (R.i which extView2);
EditTextTextPersonName=the findViewById (R.i d.e ditTextTextPersonName);
EditTextTextPersonName2=the findViewById (R.i d.e ditTextTextPersonName2);
The button=the findViewById (R.i db utton);
Buton2=the findViewById (R.i db utton2);
Button3=the findViewById (R.i db utton3);

Button. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View View) {
String useful=editTextTextPersonName. GetText (). The toString ();
Intent Intent=new Intent ();
Intent. SetClass (MainActivity. This, MainActivity2. Class);
Intent. PutExtra (" data ", useful);
startActivity(intent);
}
});

Buton2. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View View) {

Statement stmt=null;
The Connection con=null;

Try {
//SQL server registration drive
Class.forname (" com. Microsoft. Essentially. JDBC. SQLServerDriver ");
} the catch (a ClassNotFoundException e) {

Toast. MakeText (MainActivity. This "JDBC connection exception", 0), show ();
}
//Toast. MakeText (MainActivity. This "2, essentially a registration drive success", 0), show (); Pass

Try {
//access database connection
Toast. MakeText (MainActivity. This, "31, into the access database connection", 0), show ();

Con=DriverManager. GetConnection (" JDBC: JTDS: essentially://XXX. XXX, XXX, XXX: 1433/XXDB; Charset=utf8 ", "sa", "111111");
//con=DriverManager. GetConnection (" JDBC: essentially://XXX. XXX. XXX. XXX: 1433; DatabaseName=XXDB ", "sa", "111111);
//con=DriverManager. GetConnection (" JDBC: JTDS: essentially://XXX. XXX, XXX, XXX: 1433/XXDB ", "sa", "111111");
//con=DriverManager. GetConnection (" JDBC: Microsoft: essentially://XXX. XXX, XXX, XXX, 1433; DatabaseName=XXDB ", "sa", "111111");
Toast. MakeText (MainActivity. This "3, con=" + con, 0), show ();//card here

//create the Statement object
STMT=con. CreateStatement ();
//data insert, delete, update, DML language can be operating)
String SQL="insert into t_casu (casu_id casu_name, casu_pw, casu_grant) values (' 3333 ', 'sa', '111111', '11111111111111111111111111111111111111')";
//data query
String SQLL="select * from t_rm_casu";
//iterate through the result set
The ResultSet rs=STMT. ExecuteQuery (SQLL);
While (rs), next ()) {//get characters

Toast. MakeText (MainActivity. This "casu_id=" + rs. Get string (" casu_id "), 0), show ();
Toast. MakeText (MainActivity. This "SQL=" + SQL, 0), show ();
Toast. MakeText (MainActivity. This "SQLL=" + SQLL, 0), show ();
}
//executing SQL statements
STMT. ExecuteUpdate (SQL);
} the catch (SQLException e) {
Toast. MakeText (MainActivity. This "4, sqlException e", 0), show ();
e.printStackTrace();
} the finally {
If (STMT!=null)
Try {
STMT. Close ();
} the catch (SQLException e) {
Toast. MakeText (MainActivity. This "abnormal STMT close", 0), show ();
}
If (con!=null)
Try {
Con. The close ();

} the catch (SQLException e) {

Toast. MakeText (MainActivity. This "con close exception", 0), show ();
}
}
}
});

Button3. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View View) {

}
});

}
}

CodePudding user response:

Have a look at the details of the error

CodePudding user response:

I'm using the JTDS driver, test pass, you consider,
However, you seemed to be Microsoft SQL driver,
Public static Connection getSQLConnection () {
Connection conn=null;
Try {
IP_port="192.168.0.157:1433";//cell phones and SQL server to be in the same local area network (LAN)
//IP_port="10.0.2.2:1433";//emulator with this
Class.forname (".net. Sourceforge. JTDS. JDBC. The Driver ");
Conn=DriverManager. GetConnection (//and useunicode prevent Chinese garbled
"JDBC: JTDS: essentially://" + IP_port +/XXDB; "" +
"Useunicode=true; CharacterEncoding=utf-8 ", "sa", "111111"); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related