【无标题】

package com.wang;

import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class Test1 {
    //创建对象读取资源文件
    private  static  final Properties p=new Properties();
    //定义
    private  static   String user,pwd,url,driver;
    static {
        try {
            //从类路径下获取读取文件到内存中
            p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("login.properties"));
           user=p.getProperty("username");
           pwd=p.getProperty("password");
           url=p.getProperty("url");
           driver=p.getProperty("driver");
//            System.out.println(user);
//            System.out.println(pwd);
//            System.out.println(url);
//            System.out.println(driver);
            Class.forName(driver);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }



    private static Connection connection;
    private static Statement statement;
    private static ResultSet resultSet;
    public static Connection getConnection() throws SQLException {
        //获取驱动
     connection= DriverManager.getConnection(url,user,pwd);
        return  connection;
    }
    public static void main(String[] args) throws SQLException {
        //执行对象
    statement=getConnection().createStatement();
        //查询条件定义
    String user="三国演义" ,user1="罗贯中";
    //sql语句
    String sql="select * from tb_book where title='"+user+"'and author='"+user1+"'";
     resultSet=statement.executeQuery(sql);
        if(resultSet.next()){
            System.out.println("成功");
        }else {
            System.out.println("失败");
        }
    }
}
package com.wang;

import java.sql.*;

public class Test {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
       Class.forName("com.mysql.cj.jdbc.Driver");

        Connection connection= DriverManager.getConnection("jdbc:mysql:///book_db","root","root");
        //执行对象
        Statement statement=connection.createStatement();
        //执行的sql语句
        ResultSet re=statement.executeQuery("select * from tb_book");
        //判断是否存在
        while(re.next()){
            System.out.println(re.getString("title"));
            System.out.println(re.getString("author"));
            System.out.println(re.getInt("price"));
        }
    }
}
package com.wang;

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

public class Test {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection=DriverManager.getConnection("jdbc:mysql:///book_db","root","root");
        //执行对象
        Statement statement=connection.createStatement();
        //执行的sql语句
        String sql="insert into tb_book(title,author,price)values('三','四',2)";
       //返回影响行数
        String delete="delete from tb_book where title='三'";
        int m=statement.executeUpdate(delete);
        if(m==1){
            System.out.println("新增");
        }else {
            System.out.println("失败");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

麻辣个烫944

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值