JAVA链接数据库 增删改查


//加载驱动
Class.forName("com.mysql.jdbc.Driver");

//获得url 账户密码 获得链接
Connection connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bookmng?useUnicode=true&characterEncoding=utf-8", "root", "root123");
System.out.println(connection);

 

// // 增
// String sql="insert into book_table(book_name,book_author,book_price)"+"values(?,?,?)";
// PreparedStatement statement=(PreparedStatement) connection.prepareCall(sql);
// statement.setString(1, "三国");
// statement.setString(2, "罗贯中");
// statement.setInt(3, 90);
// statement .executeUpdate();



     String sql1="delete from book_table where book_id=?";
  PreparedStatement statement1=(PreparedStatement) connection.prepareCall(sql1);
  statement1.setInt(1, 2);
  statement1 .executeUpdate();




//改

String sql="update book_table set book_author =? where book_id=?";
PreparedStatement statement=(PreparedStatement) connection.prepareStatement(sql);
statement.setString(1, "gg");
statement.setInt(2,10 );
statement.executeUpdate();

 

JAVA链接数据库 增删改查

上一篇:4.Mysql之Mysqldump命令


下一篇:linux通过源码安装gdb