教程集 www.jiaochengji.com
教程集 >  脚本编程  >  java  >  正文 java更新数据库几种方法总结

java更新数据库几种方法总结

发布时间:2016-11-30   编辑:jiaochengji.com
教程集为您提供java更新数据库几种方法总结 等资源,欢迎您收藏本站,我们将为您提供最新的java更新数据库几种方法总结 资源
在java中更新数据库的方法有很多,我们经常会直接rs.updateRow()来更新数据库了,这是一种办法,当然还有其它方法,下面介绍介绍。

讲到了如何用java连接mysql数据库,并读取数据库里某字段的值,这次需要涉及的是更新数据库操作。对于入门级的学习者来说,最简单的方法有两种。 先来看下之前如何读取数据库的:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7785')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7785>

Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM user where name = '" name "'");

是的,我们用的select语句,而更新数据库,用的是update语句(谁扔的石头,NND我都说了这是入门级的教程)。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5089')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5089>

Statement st = con.createStatement();
ResultSet rs = st.executeQuery("UPDATE user set password = '" password "'" where name = '" name "'");

其实这里还有另外一种方法,利用select先找到数据匹配的那条记录,然后直接用updateRow()
来更新记录:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4385')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4385>

Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = st.executeQuery("SELECT * FROM user where name = '" name "'");
rs.updateString("password",password);
rs.updateRow();

个人觉得第2种方法的好处是不太容易由于少个引号而出现语法错误。注意第2种方法的createStatement()是有参数的,必须是updatable才行,否则re.updateRow()是会报错的。

这里顺便说一下删除的语法:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3997')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3997>

Statement st = con.createStatement();
st.execute("DELETE FROM user where name = '" name "'");


另一种办法

建一个简单的数据库如下:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9471')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9471>

                  


import java.sql.*;
public class GetConnection {
    public static void main(String[] args){
        try{
            //调用Class.forName()方法加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("成功加载MySQL驱动!");
        }catch(ClassNotFoundException e1){
            System.out.println("找不到MySQL驱动!");
            e1.printStackTrace();
        }
       
        String url="jdbc:mysql://localhost:3306/mysql";    //JDBC的URL   
        //调用DriverManager对象的getConnection()方法,获得一个Connection对象
        Connection conn;
        try {
            conn = DriverManager.getConnection(url,    "root","");
            //创建一个Statement对象
            Statement stmt = conn.createStatement(); //创建Statement对象
            System.out.print("成功连接到数据库!");
            stmt.close();
            conn.close();
        } catch (SQLException e){
            e.printStackTrace();
        }
    }
}

2. 查询数据表

  在询数据表时,需要用到ResultSet接口,它类似于一个数据表,通过该接口的实例可以获得检索结果集,以及对应数据表的接口信息。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6596')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6596>


import java.sql.*;

public class SelectTable {
   
    public static void main(String[] args){
        try{
            //调用Class.forName()方法加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("成功加载MySQL驱动!");
               
            String url="jdbc:mysql://localhost:3306/aniu";    //JDBC的URL   
            Connection conn;

            conn = DriverManager.getConnection(url,    "root","");
            Statement stmt = conn.createStatement(); //创建Statement对象
            System.out.println("成功连接到数据库!");

            String sql = "select * from stu";    //要执行的SQL
            ResultSet rs = stmt.executeQuery(sql);//创建数据对象
                System.out.println("编号" "t" "姓名" "t" "年龄");
                while (rs.next()){
                    System.out.print(rs.getInt(1) "t");
                    System.out.print(rs.getString(2) "t");
                    System.out.print(rs.getInt(3) "t");
                    System.out.println();
                }
                rs.close();
                stmt.close();
                conn.close();
            }catch(Exception e)
            {
                e.printStackTrace();
            }
    }
}

修改和删除数据库

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3723')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3723>

//修改删除数据
import java.sql.*;
public class UpdateDeleteDemo {
    public static void main(String[] args)throws Exception{
        try{
            //调用Class.forName()方法加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("成功加载MySQL驱动!");
               
            String url="jdbc:mysql://localhost:3306/aniu";    //JDBC的URL   
            Connection conn;

            conn = DriverManager.getConnection(url,    "root","");
            Statement stmt = conn.createStatement(); //创建Statement对象
            System.out.println("成功连接到数据库!");

            //查询数据的代码
            String sql = "select * from stu";    //要执行的SQL
            ResultSet rs = stmt.executeQuery(sql);//创建数据对象
                System.out.println("编号" "t" "姓名" "t" "年龄");
                while (rs.next()){
                    System.out.print(rs.getInt(1) "t");
                    System.out.print(rs.getString(2) "t");
                    System.out.print(rs.getInt(3) "t");
                    System.out.println();
                }
               
            //修改数据的代码
            String sql2 = "update stu set name=? where number=?";
            PreparedStatement pst = conn.prepareStatement(sql2);
            pst.setString(1,"8888");
            pst.setInt(2,198);
            pst.executeUpdate();
               
            //删除数据的代码
            String sql3 = "delete from stu where number=?";
            pst = conn.prepareStatement(sql3);
            pst.setInt(1,701);
            pst.executeUpdate();
               
            ResultSet rs2 = stmt.executeQuery(sql);//创建数据对象
            System.out.println("编号" "t" "姓名" "t" "年龄");
            while (rs.next()){
                System.out.print(rs2.getInt(1) "t");
                System.out.print(rs2.getString(2) "t");
                System.out.print(rs2.getInt(3) "t");
                System.out.println();
            }
               
            rs.close();
            stmt.close();
            conn.close();
            }catch(Exception e)
            {
                e.printStackTrace();
            }
    }
}

您可能感兴趣的文章:
php和java哪个好?
教你如何成为一名Java初级程序员
Java数据库编程中的几个常用技巧
java 数据库基本操作
php和java差在哪里?
1、java数据库操作基本流程
Java分层 service/action/DAO 总结
indexedDB 数据库
php和jsp之间有哪些区别
学习J2SE过程中的30个基本概念

[关闭]
~ ~