Home > Mobile >  Book project bosses
Book project bosses

Time:09-25

Write a library project how to connect to the database to solve

CodePudding user response:

Did you baidu? Android connect to the database baidu a lot of,,, MySQL, etc., the Android connections,

CodePudding user response:

I am a small white I saw now project is very urgent to do

CodePudding user response:

Baidu you tell Android to connect to the database case can use for reference to your own project

CodePudding user response:

What are you want to connect the database?

CodePudding user response:

Java write connect to the database, this is one of my JavaWeb small programs, I just let it connect the MySQL database, behind the Android client does not do the join operation
 
Public class DBManager {

//database connection constant
Public static final String DRIVER=". Com. Mysql. JDBC DRIVER ".
Public static final String USER="root";
Public static final String PASS="sa123456";
//public static final String PASS="# * c123456";
Public static final String URL="JDBC: mysql://10.7.70.75:3306/carloadtype";

//static members, support the singleton pattern
Private static DBManager per=null;
Private Connection conn=null;
Private Statement STMT=null;

//singleton mode - idlers
Private DBManager () {

}

Public static DBManager createInstance () {
If (per==null) {
Per=new DBManager ();
Per the initDB ();
}
The return per;
}

//the load driver
Public void initDB () {
Try {
Class.forName("com.mysql.jdbc.Driver");
} the catch (Exception e) {

}
}

/* *
* connect to the database, access to handle + object
*/
Public void connectDB () {
//System. Out.println (" Connecting to the database... ");
Try {
Conn=DriverManager. GetConnection (URL, USER, PASS);
stmt=conn.createStatement();
} the catch (SQLException e) {

}
//System. Out. Println (" SqlManager: Connect to the database successful. ");
}

/* *
* close database objects, release handle
*/
Public void closeDB () {
//System. Out.println (" Close connection to the database.. ");
Try {
STMT. Close ();
conn.close();
} the catch (SQLException e) {

}
//System. Out. Println (" the Close connection successful ");
}

/* *
* the query
*/
Public ResultSet executeQuery (String SQL) {
ResultSet rs=null;
Try {
rs=stmt.executeQuery(sql);
} the catch (SQLException e) {
}
return rs;
}

/* *
* add/delete/modify
*/
Public int executeUpdate (String SQL) {
Int ret=0;
Try {
Ret=STMT. ExecuteUpdate (SQL);
} the catch (SQLException e) {
}
return ret;
}

}
  • Related