Kann mir jemand in die Richtung zeigen, wie man eine MYSQL-Datenbank erstellt und sie in einem Tomcat-Webserver hat? Diese Datenbank wird für eine Android-Anwendung verwendet. Danke.Wie erstelle ich mysql-Datenbank in TomCat für eine Android-App?
0
A
Antwort
1
Dies ist eine Beispielanmeldung in Android, die eine MySQL-Datenbank verwendet. Um eine Verbindung zur MySQL-Datenbank von einem Android-Gerät/Emulator herzustellen, sende ich eine HTTP-Anfrage an ein Servlet von der Anwendung, die über JSON verarbeitet wird. Es gibt also zwei verschiedene Projekte hier. Abfrage erstellen MySQL-Datenbank
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL auto_increment,
`uname` varchar(45) NOT NULL,
`password` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `users` (`id`,`uname`,`password`) VALUES
(1,'admin','123');
Hinweis: Sie müssen die folgenden zwei Gläser müssen die Projekte Klassenpfad platziert werden. 1. json-simple-1.1.1.jar 2. mysql-connector-java-5.xxx-bin.jar LoginServlet.java
import dbConnection.DBConnectionHandler;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.Enumeration;
import org.json.simple.JSONObject;
public class LoginServlet extends HttpServlet {
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//response.setContentType("text/html;charset=UTF-8");
JSONObject json = new JSONObject();
//ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
Enumeration paramNames = request.getParameterNames();
String params[] = new String[2];
int i = 0;
while (paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();
//System.out.println(paramName);
String[] paramValues = request.getParameterValues(paramName);
params[i] = paramValues[0];
//System.out.println(params[i]);
i++;
}
String sql = "SELECT uname, password FROM users where uname=? and password=?";
Connection con = DBConnectionHandler.getConnection();
try {
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, params[0]);
ps.setString(2, params[1]);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
json.put("info", "success");
} else {
json.put("info", "fail");
}
} catch (Exception e) {
e.printStackTrace();
}
//System.out.println(json);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json.toString());
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
DBConnectionHandler.java
public class DBConnectionHandler {
Connection con = null;
public static Connection getConnection() {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");//Mysql Connection
} catch (ClassNotFoundException ex) {
Logger.getLogger(DBConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
}
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname", "root", "pass");//mysql database
} catch (SQLException ex) {
Logger.getLogger(DBConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
}
return con;
}
}
activity_main .xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dip" >
<!-- View Title Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:text="LOGIN"
android:textSize="25dip"
android:textStyle="bold" />
<!-- User Name Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="User Name" />
<!-- User Name TextField -->
<EditText
android:id="@+id/txtUser"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<!-- Password Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:text="Password" />
<!-- Password TextField -->
<EditText
android:id="@+id/txtPass"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true" />
<!-- Login Button -->
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:text="Login" />
</LinearLayout>
</ScrollView>
Insgesamt Tutorial und Source-Code finden Sie hier http://www.javaknowledge.info/sample-login-app-in-android-using-servlet-and-json-parsing/
Verwandte Themen
- 1. Wie erstelle ich eine stop/start Java Anwendung wie Tomcat?
- 2. Wie erstelle ich eine Playlist für RealPlayer?
- 3. Wie erstelle ich eine Tastenkombination für SublimeREPL?
- 4. Wie erstelle ich eine Sitemap für CakePHP?
- 5. Wie erstelle ich eine Mehrfachfrage?
- 6. Wie erstelle ich eine Gemfile?
- 7. Wie erstelle ich eine Karte in Java?
- 8. Wie erstelle ich eine Transaktionsnummer?
- 9. Wie erstelle ich eine Route in WordPress?
- 10. Wie erstelle ich eine Datenbank in Android?
- 11. Wie erstelle ich eine Stoppuhr in C#?
- 12. Wie erstelle ich eine Sprachdatei für Text in Javascript-Dateien?
- 13. Wie erstelle ich eine Navigationsleiste in Xamarin Forms für iOS?
- 14. Wie erstelle ich eine optionale Eingabe für scanf in C?
- 15. Wie erstelle ich eine Suchabfrage für Teilzeichenfolgenübereinstimmungen in Mongoose?
- 16. Wie erstelle ich eine 'Suchfunktion' für eine Webseite?
- 17. Wie erstelle ich eine MKMapView?
- 18. Wie erstelle ich eine Direktive für eine Dropdown-Auswahl?
- 19. Wie erstelle ich eine Ressourcendatei für eine Konsolenanwendung?
- 20. Wie erstelle ich eine Sequenz in MySQL?
- 21. Wie erstelle ich automatisch eine GUID für eine Join-Tabelle?
- 22. Wie erstelle ich eine JNLP-Datei für eine Java-Webanwendung?
- 23. Wie erstelle ich eine benutzerdefinierte Schriftart für eine BlackBerry-Anwendung
- 24. Wie erstelle ich eine Basisseite in WPF?
- 25. Wie erstelle ich eine CGSize in Swift?
- 26. Wie erstelle ich eine Baumansicht in Android?
- 27. Wie erstelle ich eine Datei in Android?
- 28. Wie erstelle ich eine Hauptimportdatei in ES6?
- 29. Wie erstelle ich eine Beziehungstabelle in MySQL?
- 30. Wie erstelle ich eine Liste in Oz?
Vielen Dank! Ich hatte gehofft, dass jemand mich in die richtige Richtung zeigen könnte. Ich schaue mir das Tutorial an und für das "Servlet Project" kann ich das als Java-Datei im Android Studio Projekt erstellen? – TheLearner
ya, wie ich vom Blog von "javaknowledge.com" gelesen hatte, sind alle Dateien im Projekt src-Ordner, zuerst empfehle ich Ihnen, bitte gehen Sie durch das Tutorial, das ich mit Ihnen geteilt hatte, folgen Sie den genauen Schritten des Tutorials dann verwenden Sie sie in Ihrem Projekt . –
Bitte denken Sie daran, dieser Anwendung eine Art Sicherheit hinzuzufügen. Denken Sie daran, dass Sie Anfragen, die Sie aus dem offenen Web erhalten, nicht vertrauen können. –