Recently embarked on public platform development, need to use the server, the company temporarily did not have its own server, only to go to the SAE registered one, Sina to use how long. Today, I need to use the SAE MY SQL database to build a user information table, before the server, MY SQL this block has not touched, want to do a function, even small function, also feel impossible. For example, today you want to create a user information table, just three fields, create a database, create a table, loop to join the data is done, and then a few steps, if you operate on Android locally, just a few lines of code, more than 10 minutes to fix things. But because of unfamiliar, make a database for three hours, now will use a basic operation summed up, convenient for later use. Note: Some of the knowledge points come from the network.
1. Database creation and linking
Database creation is simple, click "Manage My SQL" database management interface, enter the table name, field name, value size, click Submit.
Database link: Directly on the code:
/** * Get MySQL database connection * * @return Connection */private Connection getconn () {Connection conn = null; String dbdriver = "Com.mysql.jdbc.Driver"; Load MySQL drive String dburl = "Jdbc:mysql://w.rdc.sae.sina.com.cn:3307/app_tecsunweixin"; App_tecsunweixin for Sina app database name String dbUser = "n2xl2ozow5"; Application first page of access key String Dbpassword = "53whjwyzx33yw3hliw5y31jhmkzh11j054j433y2"; Application first page of secret keytry {class.forname (dbdriver);//Get database Connection conn = Drivermanager.getconnection (Dburl, DbUser, Dbpassword );} catch (Exception e) {e.printstacktrace ();} Return conn;}
2. Add data: Look directly at the code:
/** * Save User Information * * @param Request Object * @param number * @param name * @param OpenID encrypted name */public static void SA Veuserinfo (int number, string name, String OpenID) {String sql = "INSERT into user (number, name, OpenID) VALUES (?,?,?) try {Connection conn = new Mysqlutil (). Getconn ();//Get connection PreparedStatement PS = conn.preparestatement (sql);p S.setlong ( 1, number);p s.setstring (2, name);p s.setstring (3, OpenID);p s.executeupdate ();//Release Resources ps.close (); Conn.close ();} catch (Exception e) {e.printstacktrace ();}}
3. Database query: Directly look at the code:
/** * Query user specific information according to user name * * @param name username * @return UserInfo */public static UserInfo getuserinfo (String name) {UserInfo UserInfo = null; String sql = "Select number, name, OpenID from user where name=?"; try {Connection conn = new Mysqlutil (). Getconn (); PreparedStatement PS = conn.preparestatement (sql);p s.setstring (1, name); ResultSet rs = Ps.executequery (), if (Rs.next ()) {userInfo = new userInfo (); Userinfo.setnubmer (Integer.parseint ( Rs.getstring ("number")), Userinfo.setname (rs.getstring ("name")), Userinfo.setopenid (rs.getstring ("OpenID")); Release Resources rs.close ();p s.close (); Conn.close ();} catch (SQLException e) {e.printstacktrace ();} return userInfo;}
The database can be added and censored on the SAE, in the database management interface Click "Browse", you can see the database details:
Operating the My SQL database on the SAE via Java