每日总结 4..9
今天对我的售卖机页面设计。
实现androd的数据传输,和补货的数据更新。
package com.example.jjt; import android.util.Log; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Dbhelper { private static Connection connection; private final static String driver = "com.mysql.jdbc.Driver"; private final static String url = "jdbc:mysql://10.99.121.119/jiaqi?useUnicode=true&characterEncoding=UTF-8"; private final static String user = "root"; private final static String pass= "020907"; Connection conn=null; Statement st=null; ResultSet rs=null; static { try { Class.forName(driver); } catch (ClassNotFoundException e) { System.out.println("加载驱动错误"); } } //2. 获取连接 public static Connection getConnect() { try { connection=DriverManager.getConnection(url,user,pass); }catch(SQLException e) { e.printStackTrace(); } return connection; } //3. 释放连接资源 public static void release(Connection conn, Statement st, ResultSet rs) throws Exception { if (rs != null) { rs.close(); } if (st != null) { st.close(); } if (conn != null) { conn.close(); } } public static String getInfoByName(String name){ String s=""; // 根据数据库名称,建立连接 Connection connection = getConnect(); try { // mysql简单的查询语句。这里是根据MD_CHARGER表的NAME字段来查询某条记录 String sql = "select * from thing where name = ?"; // String sql = "select * from MD_CHARGER"; if (connection != null){// connection不为null表示与数据库建立了连接 PreparedStatement ps = connection.prepareStatement(sql); if (ps != null){ // 设置上面的sql语句中的?的值为name ps.setString(1, name); // 执行sql查询语句并返回结果集 ResultSet rs = ps.executeQuery(); if (rs != null){ int count = rs.getMetaData().getColumnCount(); Log.e("DBUtils","列总数:" + count); while (rs.next()){ // 注意:下标是从1开始的 s= rs.getString("total"); } connection.close(); ps.close(); return s; }else { return null; } }else { return null; } }else { return null; } }catch (Exception e){ e.printStackTrace(); Log.e("DBUtils","异常:" + e.getMessage()); return null; } } public static String fins(String name){ String s=""; // 根据数据库名称,建立连接 Connection connection = getConnect(); try { // mysql简单的查询语句。这里是根据MD_CHARGER表的NAME字段来查询某条记录 String sql = "select * from thing where name = ?"; // String sql = "select * from MD_CHARGER"; if (connection != null){// connection不为null表示与数据库建立了连接 PreparedStatement ps = connection.prepareStatement(sql); if (ps != null){ // 设置上面的sql语句中的?的值为name ps.setString(1, name); // 执行sql查询语句并返回结果集 ResultSet rs = ps.executeQuery(); if (rs != null){ int count = rs.getMetaData().getColumnCount(); Log.e("DBUtils","列总数:" + count); while (rs.next()){ // 注意:下标是从1开始的 s= rs.getString("num"); } connection.close(); ps.close(); return s; }else { return null; } }else { return null; } }else { return null; } }catch (Exception e){ e.printStackTrace(); Log.e("DBUtils","异常:" + e.getMessage()); return null; } } }