首页 文章 精选 留言 我的

精选列表

搜索[增删改查],共8405篇文章
优秀的个人博客,低调大师

安卓操作sqlite3,增删改

创建 layout <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/create_database" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Create database" /> </LinearLayout> main package demo.jq.com.databasetest; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; /** * @author jim */ public class MainActivity extends AppCompatActivity { /** * 引入数据库助手类 */ private MyDatabaseHelper dbHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2); // 初始化创建按钮 Button createDatabase = (Button) findViewById(R.id.create_database); // 设置点击事件 createDatabase.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { // 执行写入数据库操作 dbHelper.getWritableDatabase(); } }); } } helper package demo.jq.com.databasetest; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.widget.Toast; /** * @author Jim */ public class MyDatabaseHelper extends SQLiteOpenHelper { public static final String CREATE_BOOK = "create table Book(" + "id integer primary key autoincrement," + "author text," + "price real," // 浮点型 + "pages integer," + "name text)"; // 文本类型 public static final String CREATE_CATEGORY = "create table Category (" + "id integer primary key autoincrement," + "category_name text," + "category_code integer)"; private Context mContext; public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,int version) { super(context,name,factory,version); mContext = context; } /** * 数据库创建的时候,才执行 * 如果数据库已经存在,将不会执行这个方法 * @param db */ @Override public void onCreate(SQLiteDatabase db) { // 执行sql语句 db.execSQL(CREATE_BOOK); db.execSQL(CREATE_CATEGORY); Toast.makeText(mContext,"Create succeeded",Toast.LENGTH_SHORT).show(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("drop table if exists Book"); db.execSQL("drop table if exists Category"); onCreate(db); } } 添加数据 <Button android:id="@+id/add_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Add data" /> // 初始化添加数据按钮 Button addData = (Button) findViewById(R.id.add_data); addData.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); // 开始组装第一条数据 values.put("name","The Da Vinci Code"); values.put("author","Dan Brown"); values.put("pages",454); values.put("price",16.96); // 插入第一条数据 db.insert("Book",null,values); // 开始组装第二条数据 values.put("name","The Lost Symbol"); values.put("author","Dan Brown"); values.put("pages",510); values.put("price",19.95); // 插入第二条数据 db.insert("Book",null,values); } }); 修改数据 <Button android:id="@+id/update_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Update data" /> // 更改数据 Button updateData = (Button) findViewById(R.id.update_data); updateData.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("price",18.95); // 修改数据 db.update("Book",values,"id = ?",new String[] {"1"}); } }); 删除数据 <Button android:id="@+id/delete_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Delete data" /> // 删除数据 Button deleteData = (Button) findViewById(R.id.delete_data); deleteData.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { SQLiteDatabase db = dbHelper.getWritableDatabase(); // 删除数据 db.delete("Book","pages > ?",new String[] {"500"}); } }); 查询数据 <Button android:id="@+id/query_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Query data" /> // 查询数据 Button queryData = (Button) findViewById(R.id.query_data); queryData.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { SQLiteDatabase db = dbHelper.getWritableDatabase(); // 查询Book表中所有的数据 Cursor cursor = db.query("Book",null,null,null,null,null,null); if (cursor.moveToFirst()) { do { // 遍历Cursor对象,取出数据 String name = cursor.getString(cursor.getColumnIndex("name")); String author = cursor.getString(cursor.getColumnIndex("author")); int pages = cursor.getInt(cursor.getColumnIndex("pages")); double price = cursor.getDouble(cursor.getColumnIndex("price")); Log.d(TAG,"book name is "+name); Log.d(TAG,"book author is "+author); Log.d(TAG,"book pages is "+pages); Log.d(TAG,"book price is "+price); } while (cursor.moveToNext()); } cursor.close(); } }); 本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/7698573.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

JDBC(三)数据库连接和数据增删改

加载JDBC驱动 只需要在第一次连接数据库时加载,java6以后我们可以直接这样加载: *我在本系列教程中用mysql示例。 需要导入jar包:mysql-connector-java-5.0.8-bin.jar(版本和下载网站自己挑) 如果是web程序,把jar包放到WebRoot/WEB-INF/lib/下 如果是普通java项目,将jar包导入到自己项目的lib库中。 然后加载驱动如下 Class.forName("com.mysql.jdbc.Driver"); 打开连接 打开连接的话需要调用DriverManager类中的getConnection()方法,该方法有三个重载方法。如下所示 public static Connection getConnection(String url, java.util.Properties info) throws SQLException { return (getConnection(url, info, Reflection.getCallerClass())); } public static Connection getConnection(String url, String user, String password) throws SQLException { java.util.Properties info = new java.util.Properties(); if (user != null) { info.put("user", user); } if (password != null) { info.put("password", password); } return (getConnection(url, info, Reflection.getCallerClass())); } public static Connection getConnection(String url) throws SQLException { java.util.Properties info = new java.util.Properties(); return (getConnection(url, info, Reflection.getCallerClass())); } 大概看下它的参数名字应该知道它需要什么吧。在这里我只解释第一个方法。Properties info 这个参数其实也是user和password的打包。其实和方法二一样。 实例: public class Main { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/user"; String user = "root"; String password = "root"; Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { // 1.加载驱动//com.mysql.jdbc.Driver Class.forName("com.mysql.jdbc.Driver"); // 2.获取连接 connection = DriverManager.getConnection(url, user, password); // 3.获取用于向数据库发送SQL的Statement对象 statement = connection.createStatement(); // 4.执行sql,获取数据 resultSet = statement.executeQuery("SELECT * FROM user;"); // 解析数据 while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("username"); String psd = resultSet.getString("birthday"); String email = resultSet.getString("sex"); String birthday = resultSet.getString("address"); System.out.println(" " + name + " " + psd + " " + email + " " + birthday); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { //5.关闭连接,释放资源 if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } resultSet = null; } if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } statement = null; } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } connection = null; } } } } 数据查询 数据查询需要我们将sql发送给数据库,我们需要创建一个Statement对象。 Statement statement = connection.createStatement(); 因为Statement对象可以执行sql String sql = "select * from user"; ResultSet resultSet = statement.executeQuery(sql); 当执行一个sql查询的时候,会得道一个ResultSet对象,它里面放着查询的结果。 ResultSet resultSet = statement.executeQuery("SELECT * FROM user;"); 那怎样获取user表中对应列的数据呢? resultSet .getString ("columnName"); resultSet .getLong ("columnName"); resultSet .getInt ("columnName"); resultSet .getDouble ("columnName"); resultSet .getBigDecimal("columnName"); 或者通过第几列进行查询。 resultSet .getString (1); resultSet .getLong (2); resultSet .getInt (3); resultSet .getDouble (4); resultSet .getBigDecimal(5); 如果你想知道对应列名的index值(位于第几列),可以这样 int columnIndex = resultSet .findColumn("columnName"); 数据修改/删除 为什么要把修改和删除放一块说呢,因为他们和查询调用不一样的方法。 查询我们调用executeQuery()方法,修改和删除我们需要用executeUpdate()方法。 举个简单的例子: //更新数据(也叫修改数据) String sql = "update user set name='fantj' where id=1"; int rowsAffected = statement.executeUpdate(sql); //rowsAffected是影响行数的意思 //删除数据 String sql = "delete from user where id=123"; int rowsAffected = statement.executeUpdate(sql); 关闭连接 为了安全性和项目性能,我们尽量在执行完操作之后关闭连接(虽然高版本jvm会自动关闭它,但是这也需要检测浪费cpu资源)。 关闭连接分为三个部分。 resultSet.close(); statement.close(); connection.close(); 注意先后问题。

优秀的个人博客,低调大师

code-gen 1.2.1 发布,新增 vue-element-admin 增删改模板

code-gen 1.2.1发布,本次更新内容如下: 修复保存模板时的错误 模板编辑器新增行号 新增admin-vue-CRUD模板doc 新增的vue模板可配合vue-element-admin使用,效果图: 关于code-gen 一款代码生成工具,可自定义模板生成不同的代码,支持MySQL、Oracle、SQL Server、PostgreSQL 只需要一个Java8环境,下载后即可运行使用。 步骤简单,只需配置一个数据源,然后勾选模板即可生成代码。 默认提供了通用的实体类、mybatis接口、mybatis配置文件模板,可以快速开发mybatis应用。

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Apache Tomcat

Apache Tomcat

Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。

JDK

JDK

JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。