mysql保存arraylist,如何在Java中將ArrayList數據類型插入MySQL數據庫

2

There is no direct way of inserting an array list into a row in mysql. But if you want to insert the names in a single row , then instead of putting them into arraylist you could just save them into a string variable and maybe separate them with a 'character' and then use insert query to save it in Db.

沒有直接的方法將數組列表插入到mysql的行中。但是如果你想在一行中插入名稱,那么你可以將它們保存到一個字符串變量中,而不是將它們放入一個字符串變量中,然后用“字符”將它們分開,然后使用insert查詢將其保存在Db中。

Otherwise you have to make a separate method to convert this this arraylist into a string and then do the above

否則,您必須使用單獨的方法將此arraylist轉換為字符串,然后執行上述操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android中将MySQL中的数据用ListView显示,您需要执行以下步骤: 1.创建一个布局文件,其中包括一个ListView。 2.创建一个Java类来连接到MySQL数据库并检索数据。 3.使用适配器将数据填充到ListView中。 以下是一个简单的示例: 1.在布局文件中添加ListView。 ``` <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 2.创建一个Java类来连接到MySQL数据库并检索数据。 ``` public class MySQLConnector { private String url = "jdbc:mysql://localhost:3306/mydatabase"; private String user = "myuser"; private String password = "mypassword"; public Connection getConnection() { Connection connection = null; try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return connection; } public ArrayList<String> getData() { Connection connection = getConnection(); ArrayList<String> list = new ArrayList<String>(); try { Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM mytable"); while (resultSet.next()) { String data = resultSet.getString("data"); list.add(data); } } catch (SQLException e) { e.printStackTrace(); } return list; } } ``` 3.使用适配器将数据填充到ListView中。 ``` public class MainActivity extends AppCompatActivity { private ListView listView; private MySQLConnector mySQLConnector = new MySQLConnector(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView); ArrayList<String> list = mySQLConnector.getData(); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list); listView.setAdapter(adapter); } } ``` 这个例子连接到名为“mydatabase”的MySQL数据库,并从名为“mytable”的表中检索数据。然后使用ArrayAdapter将数据填充到ListView中。您需要确保在AndroidManifest.xml文件中添加Internet权限,以便应用程序可以连接到MySQL数据库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值