0
任何人都可以指示我學習如何創建一個MYSQL數據庫,並將其放在Tomcat Web服務器中?這個數據庫將被用於android應用程序。 謝謝。如何在TomCat中爲android應用程序創建mysql數據庫?
任何人都可以指示我學習如何創建一個MYSQL數據庫,並將其放在Tomcat Web服務器中?這個數據庫將被用於android應用程序。 謝謝。如何在TomCat中爲android應用程序創建mysql數據庫?
這是Android中使用MySQL數據庫的示例登錄應用程序。從Android設備/仿真器連接到MySQL數據庫我正在通過JSON處理應用程序向Servlet發送HTTP請求。所以這裏有兩個不同的項目。 查詢創建MySQL數據庫
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');
注: 必須需要以下兩個罐放置到項目的類路徑。 1. JSON-簡單1.1.1.jar 2. MySQL的連接器的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>
總體教程和源代碼,你可以在這裏找到http://www.javaknowledge.info/sample-login-app-in-android-using-servlet-and-json-parsing/
非常感謝!我希望有人能指出我的方向。我正在看教程和「Servlet項目」我可以創建一個java文件在android studio項目內? – TheLearner
雅,因爲我已經從「javaknowledge.com」的博客閱讀所有文件都在項目src文件夾中,首先我建議你請通過我與你分享的教程,按照教程的確切步驟,然後在你的項目中使用它們。 –
請記住爲此應用程序添加某種安全性。請記住,您不能相信您從打開的網頁上獲得的請求。 –