Java程序链接MySQL数据库

JDBC
java链接数据库主要分为以下五个步骤

//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建一个链接对象
Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/test1", "root", "root");
//3.创建一个sql语句的发送命令对象
Statement stmt = (Statement) conn.createStatement();
//4.执行sql语句拿到结果集
ResultSet rs = stmt.executeQuery("select * from stu");
while (rs.next()) {
     System.out.println(rs.getInt("id") + ":" +                                                                                                  rs.getString("name"));

//根据字段名拿取(推荐)
     int id = rs.getInt("id");//结果集的字段 不是物理表的字段
     String name = rs.getString("name");

//根据字段的索引(索引从1开始)
    int id = rs.getInt(1);
    String name = rs.getString(2);
    System.out.println(id+":"+name);
}
//5.关闭链接,命令对象以及结果集
rs.close();
stmt.close();
conn.close();

上述第三步时,我们一般采用预编译PreparedStatement新建对象

PreparedStatement ps = (PreparedStatement)conn
                         .prepareStatement(
    "select*from people   where name =? and password =?");
ps.setString(1, name);
ps.setString(2, password);
//查询语句返回的是一个结果集
ResultSet rs = ps.executeQuery();
//更新语句返回整形,成功为1,失败为0
ps.executeUpdate();

代码实例

public class DBUtil {
    /**
     * 1:connection :负责java程序与数据库之间的链接 2:Statement:负责执行SQL语句 3;Resultset:存放结果集
     * */
    // 链接数据库
    public static Connection getConnection() {
        Connection conn = null;// 负责JAVA程序与数据库程序之间的链接
        try {
            Class.forName("com.mysql.jdbc.Driver");// 加载mysql数据库驱动 死记硬背必须掌握
            // 链接数据库:根据数据库的URL 用户名 密码
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test1", "root", "123456");// test1是数据库的名字,根据自己数据库的名字更改
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();// 打印异常信息
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
        public static String QueryUser(String name, String password) {
        Connection conn = getConnection();// 获取数据库的链接
        try {
            PreparedStatement ps = (PreparedStatement) conn
                    .prepareStatement("select *from people where name =? and password =?");
            ps.setString(1, name);
            ps.setString(2, password);
            //执行SQL语句
            ResultSet rs = ps.executeQuery();
            if(rs.next()){//指向下一行,如果有数据返回true,没有返回false
                return "true";
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "false";
    }
    public static void InsertUser(String name,String password){
        Connection conn = getConnection();
        PreparedStatement ps;
        try {
            ps = (PreparedStatement) conn
                    .prepareStatement("insert into people(name,password) values(?,?)");
            ps.setString(1, name);
            ps.setString(2, password);
            ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值