JAVA main方法调用数据库实现

private static JdbcTemplate jdbcTemplate;
static {
    String driver = "com.mysql.cj.jdbc.Driver";//mysql驱动
    String url ="jdbc:mysql://127.0.0.1:3306/admin";//连接地址
    String user ="root";//用户
    String password ="123456";//密码

    DriverManagerDataSource dataSource=new DriverManagerDataSource();
    dataSource.setUrl(url);
    dataSource.setDriverClassName(driver);
    dataSource.setUsername(user);
    dataSource.setPassword(password);

    jdbcTemplate=new JdbcTemplate(dataSource);
}

public static void main(String[] args) {
    String sql1= "INSERT INTO admin(user,pass) VALUES(?,?)";
    Object[] arg ={"zs", "12"};
    int a = jdbcTemplate.update(sql1,arg);
    System.out.println(a);

    String sql ="select * from  admin;";//admin 数据库表明
    List> maps = jdbcTemplate.queryForList(sql);
    System.out.println(maps);
}

你可能感兴趣的:(java,数据库,开发语言)