JDBC开发

java操纵mysql;

自己实现的第一个程序,小激动!

在WebContent中布置jar包

package jdbcdemo1;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.sql.Connection;

/**
 * @author Administrator
 * 演示示例1:使用纯Java方式连接数据
 */
/**
 * @author Administrator alt+shift+j
 */
public class demo1 {

    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("建立连接失败");
        }

        try {
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/hello?useUnicode=true&characterEncoding=utf-8", "root", "123456");
            System.out.println("建立连接成功");
            // 3.创建ps ,代表预编译的sql对象
            ps = conn.prepareStatement("select* from student");
            // 4.执行
            rs = ps.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getInt("id") + " " + rs.getString("name") + rs.getFloat("Chinese"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("建立连接失败");
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e2) {
                    // TODO: handle exception
                    e2.printStackTrace();
                }
                rs = null;
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (Exception e2) {
                    // TODO: handle exception
                    e2.printStackTrace();
                }
                ps = null;
            }

        }

    }
}
View Code

 

 1.加载驱动

try {
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("建立连接成功");
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
        System.out.println("建立连接失败");
    }
View Code

2.建立连接

3.创建sql对象

PreparedStatement ps=null;  (预处理对象)

4.执行

5.关闭资源 先开后关闭,

DriverManager用于加载驱动,创建与数据库的链接

Connection接口用于代表数据库的链接,Connection是数据库编程中最重要的一个对象,客户端与数据库的所有交互都是通过Connection进行的

createStatement():创建向数据库发送sql的statement对象

prepareStatement(sql):创建向数据库发送预编译的sql的PrepareStatement对象

prepareCall(sql):创建执行存储过程的callableStatement对象

SetAutoCommit(boolean autoCommit):设置事务是否自动提交

commit() 在链接上提交事务

rollback() 在此链接上提交事务

eg:什么时候需要把setAutoCommit设置成false?

当有多个dml同时执行,将其作为一个整体提交,则使用事务管理,则需要设置成为false

    ps.executeUpdate("update student set Chinese=Chinese-10 where name='王志'");
    int i=9/0;
    ps.executeUpdate("update student set Chinese=Chinese+10 where name='李进'");

可能会出现上面的异常,怎么解决呢?

游标:ResultSet  rs=null;

rs=ps.executeQuery();//专门用来查询的

ps.executeUpdate("update student set Chinese=Chinese-10 where name='王志'");

Result.  后面还有类型,根据实际需要设定

Connection.  后面也有方法与类型

假设我们希望 rs结果可以滚动(可以向前 也可以向后)

System.out.println(rs.getInt(1)+" "+rs.getString(2));

System.out.println(rs.getInt("id")+" "+rs.getString("name"));

et=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

设置游标类型可回滚,并发性的话为只读

//回滚使用

rs.beforeFirst();
System.out.println();
while(rs.next()){
System.out.println(rs.getInt("id")+" "+rs.getString("name"));
}

对于Statement来说默认的不可回滚的,要回滚必须加上

et=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

rs.absolute(3);
while(rs.next()){
System.out.println(rs.getInt("id")+" "+rs.getString("name"));
}

Connection得释放 cmd输入 netstat -an 查看连接情况

及时关闭资源,尽量晚创建,尽量早释放,

关闭资源的代码放在finally中

原文地址:https://www.cnblogs.com/helloworld2019/p/10798439.html