赞
踩
Android Studio 连接阿里云数据库的简单方法【制作基于数据库的多人远程聊天APP】
将白名单设置的和我一样,就是允许所有人访问你的数据库,但是你们数据库是有密码的,所以不用担心别人对你的数据库干什么,但是如果你不是设置自己用的数据库,建议还是限制ip,我觉得不限制比较好
刚开始进入的时候填写基本信息,我只填了表名为:test
然后填写列信息 注意:varchar长度2等于一个字符,所以200长度的意思是100个以内的中文字符
我这里设置id这个自增是有原因的,后期代码会讲
保存变更–>直接执行【数据库就建好了】
从左往右点击
我下载的是如图所示
弹窗点击Refactor
点击,确认,才是真正的导入jar包
出现三角形,说明成功了
// An highlighted block public class MySQLConnections { private String driver = ""; private String dbURL = ""; private String user = ""; private String password = ""; private static MySQLConnections connection = null; private MySQLConnections() throws Exception { driver = "com.mysql.jdbc.Driver"; dbURL = "jdbc:mysql://你复制的外网地址:3306/库名"; user = "你之前新建的数据库账号,不是阿里云账号哦"; password = "密码"; System.out.println("dbURL:" + dbURL); } public static Connection getConnection() { Connection conn = null; if (connection == null) { try { connection = new MySQLConnections(); } catch (Exception e) { e.printStackTrace(); return null; } } try { Class.forName(connection.driver); conn = DriverManager.getConnection(connection.dbURL, connection.user, connection.password); } catch (Exception e) { e.printStackTrace(); } return conn; } }
注意第、dbURL这个很容易写错,是jdbc:mysql://外网地址/库名
外网地址是你之前复制的,库名是你自己新建的,test是表名,不是库名哦,中间用/分隔
// An highlighted block <TextView android:id="@+id/t1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="10" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <EditText android:id="@+id/msg" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:ems="10" android:hint="请输入内容" android:inputType="textPersonName" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="Button" /> </LinearLayout>
复制上面的xml代码到如下位置
// An highlighted block public class MainActivity extends AppCompatActivity { private TextView t1; //用于显示获取的信息 private Button sendmsg; //按钮 private EditText et_msg;//用户输入的信息 private String user="用户名"; private boolean T=false;//发送标志位 //数据库连接类 private static Connection con = null; private static PreparedStatement stmt = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化 t1 = findViewById(R.id.t1); et_msg = findViewById(R.id.msg); sendmsg = findViewById(R.id.button); sendmsg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { T=true; } }); //TODO 启动发送线程,用按钮控制发送标志位T,来进行发送信息【注意:连接数据库必须在线程内,不然会报错】 Threads_sendmsg threads_sendmsg = new Threads_sendmsg(); threads_sendmsg.start(); //TODO 启动获取数据线程,读取数据库里的信息【注意:连接数据库必须在线程内,不然会报错】 Threads_readSQL threads_readSQL = new Threads_readSQL(); threads_readSQL.start(); } class Threads_sendmsg extends Thread { @Override public void run() { while (true){ while (T){ try { con = MySQLConnections.getConnection(); } catch (Exception e) { e.printStackTrace(); } try { String msg =et_msg.getText().toString().trim(); //用户发送的信息 if (msg.isEmpty()){ Looper.prepare(); Toast.makeText(MainActivity.this, "请不要发送空消息", Toast.LENGTH_SHORT).show(); Looper.loop(); T=false; break; } if (msg.length()>199){ Looper.prepare(); Toast.makeText(MainActivity.this, "请不要发送200字符以上的信息", Toast.LENGTH_SHORT).show(); Looper.loop(); T=false; break; } if (con!=null) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "发送成功", Toast.LENGTH_SHORT).show(); } }); String sql = "insert into test(name,msg) values(?,?)"; stmt = con.prepareStatement(sql); // 关闭事务自动提交 ,这一行必须加上 con.setAutoCommit(false); stmt.setString(1,user); stmt.setString(2,msg); stmt.addBatch(); stmt.executeBatch(); con.commit(); } }catch (SQLException e){ System.out.println(e); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,"请输入正确的语句",Toast.LENGTH_SHORT).show(); } }); } T=false; } } } } class Threads_readSQL extends Thread { @Override public void run() { while (true){ try { con = MySQLConnections.getConnection(); } catch (Exception e) { e.printStackTrace(); } try { String sql ="Select distinct name,msg from test order by id"; if (con!=null) { stmt = con.prepareStatement(sql); // 关闭事务自动提交 ,这一行必须加上 con.setAutoCommit(false); ResultSet rs = stmt.executeQuery();//创建数据对象 //清空上次发送的信息 t1.setText(""); while (rs.next()) { t1.append(rs.getString(1) + "\n" + rs.getString(2)+ "\n\n"); } con.commit(); rs.close(); stmt.close(); } //2秒更新一次 sleep(2000); }catch (Exception e){ System.out.println(e); runOnUiThread(new Runnable() { @Override public void run() { //Toast.makeText(MainActivity.this,"请输入正确的语句",Toast.LENGTH_SHORT).show(); } }); } } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。