jdbc 连接数据库并进行操作相关代码

连接数据库过程,并遍历数据库

import java.sql.*;

public class TestJDBC {

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {

            Class.forName("com.mysql.jdbc.Driver");// 1注册driver
            conn = DriverManager
                    .getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");// 2通过drivermanager.getconnection()拿到connection
            stmt = conn.createStatement();// 3创建statement对象通过statement执行sql语句
            rs = stmt.executeQuery("select * from dept");// 4拿到Resultset
            while (rs.next()) {
                System.out.print(rs.getString("deptno"));
                System.out.println(rs.getString("dname"));// 5对ResultSet进行遍历
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());

        } finally {
            try {
                if (rs != null) {
                    rs.close();// 6关掉打开的资源
                    rs = null;
                }
                if (stmt != null) {
                    stmt.close();
                    stmt = null;
                }
                if (conn != null) {
                    conn.close();// 关掉连接,后打开的先关
                    conn = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();

            }

        }
    }
}


向数据库内插入一条记录

import java.sql.*;

public class TestDML {

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;

        try {

            Class.forName("com.mysql.jdbc.Driver");// 1注册driver
            conn = DriverManager
                    .getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");// 2通过drivermanager.getconnection()拿到connection
            stmt = conn.createStatement();// 3创建statement对象通过statement执行sql语句
            String sql ="insert into dept2 values (007,'zmb','USA') ";//sql语句
            stmt.executeUpdate(sql);//执行sql语句
    
        } catch (ClassNotFoundException e) {
            e.printStackTrace();

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());

        } finally {
            try {
            
                if (stmt != null) {
                    stmt.close();
                    stmt = null;
                }
                if (conn != null) {
                    conn.close();// 关掉连接,后打开的先关
                    conn = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();

            }

        }
    }
}


通过输入向数据库插入数据

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class TestDML2 {

    public static void main(String[] args) {
        int deptno = 0;
        String dname = null;
        String loc = null;

        Connection conn = null;
        Statement stmt = null;

        try {
            deptno = Integer.parseInt(args[0]);
        } catch (NumberFormatException e) {
            System.out.println("参数错误,参数是数字型,请输入真确的参数!");
            System.exit(-1);
        }

        try {

            Class.forName("com.mysql.jdbc.Driver");// 1注册driver
            conn = DriverManager
                    .getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");// 2通过drivermanager.getconnection()拿到connection
            stmt = conn.createStatement();// 3创建statement对象通过statement执行sql语句
            String sql = "insert into dept2 values ("+deptno + ",'"+dname +",'"+ loc + "')" ;// sql语句
            stmt.executeUpdate(sql);// 执行sql语句

        } catch (ClassNotFoundException e) {
            e.printStackTrace();

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());

        } finally {
            try {

                if (stmt != null) {
                    stmt.close();
                    stmt = null;
                }
                if (conn != null) {
                    conn.close();// 关掉连接,后打开的先关
                    conn = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();

            }

        }
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值