JDBC简单查询实例

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.neusoft.java275.dao.DBUtil;

public class StudentManager
{
    public static void main(String[] args)
    {
        StudentManager manager=new StudentManager();
        List<StudentVO> resultList=manager.queryStudentList();
        for(StudentVO student:resultList)
        {
            System.out.println(student.getId()+","+student.getName());
        }
    }
    public List<StudentVO> queryStudentList()
    {
        List<StudentVO> resultList=new ArrayList<StudentVO>();
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.137.23:1521:orcl", "java", "java");
            stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT id,name FROM student order by id asc");
            StudentVO studentVO=null;
            while (rs.next())
            {
                studentVO=new StudentVO();
                studentVO.setId(rs.getString("id"));
                studentVO.setName(rs.getString("name"));
                resultList.add(studentVO);
            }
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            DBUtil.closeQuietly(rs);
            DBUtil.closeQuietly(stmt);
            DBUtil.closeQuietly(con);
        }
        return resultList;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我很乐意为您解答。首先,您需要了解JDBCJava Database Connectivity)是一种Java编程语言中用于执行SQL语句的API。它可以帮助我们连接到数据库并执行SQL查询,插入,更新和删除等操作。 以下是使用JDBC实现查询和添加功能的简单示例代码: ### 查询功能 ```java import java.sql.*; public class JdbcQueryExample { static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/EMP"; static final String USER = "yourusername"; static final String PASS = "yourpassword"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ Class.forName("com.mysql.jdbc.Driver"); System.out.println("连接数据库..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); System.out.println(" 实例化Statement对象..."); stmt = conn.createStatement(); String sql; sql = "SELECT id, name, age FROM Employees"; ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); System.out.print("ID: " + id); System.out.print(", 名字: " + name); System.out.println(", 年龄: " + age); } rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ se.printStackTrace(); }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ } try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } } System.out.println("查询完成!"); } } ``` ### 添加功能 ```java import java.sql.*; public class JdbcInsertExample { static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/EMP"; static final String USER = "yourusername"; static final String PASS = "yourpassword"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ Class.forName("com.mysql.jdbc.Driver"); System.out.println("连接数据库..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); System.out.println(" 实例化Statement对象..."); stmt = conn.createStatement(); String sql; sql = "INSERT INTO Employees (id, name, age) VALUES (101, '张三', 23)"; stmt.executeUpdate(sql); sql = "INSERT INTO Employees (id, name, age) VALUES (102, '李四', 24)"; stmt.executeUpdate(sql); sql = "INSERT INTO Employees (id, name, age) VALUES (103, '王五', 25)"; stmt.executeUpdate(sql); System.out.println("数据已经成功添加到数据库表中!"); stmt.close(); conn.close(); }catch(SQLException se){ se.printStackTrace(); }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ } try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } } System.out.println("添加完成!"); } } ``` 请注意,您需要将上述代码中的“yourusername”和“yourpassword”替换为您的MySQL用户名和密码,并且需要将“com.mysql.jdbc.Driver”替换为您使用的数据库的驱动程序。 希望这可以帮助您开始使用JDBC实现您的查询和添加功能!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值