Eclipse连接Mysql成功,然后进行增删改查操作
package Sqldata; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import com.mysql.jdbc.PreparedStatement; public class SqlMain { public static void main(String[] args) throws ClassNotFoundException, SQLException { // TODO Auto-generated method stub // 连接Mysql // 加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 填写url 用户 密码获得连接 Connection connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/book?useUnicode=true&characterEncoding=utf-8","root","root123"); System.out.println(connection); // 插入 // 构造sql语句 String sql="insert into user_table values(?,?,?,?,?,?)"; // 获得执行sql的对象(根据connection获得) PreparedStatement statement=(PreparedStatement) connection.prepareStatement(sql); // 往sql里赋值 statement.setInt(1, 4); statement.setString(2, "美羊羊"); statement.setInt(3, 21); statement.setString(4, "女"); statement.setString(5, "123412341234"); statement.setString(6, "110110"); int a=statement.executeUpdate(); System.out.println(a); // 删除 String sql1="delete from user_table where user_id=?"; PreparedStatement statement1 =(PreparedStatement) connection.prepareStatement(sql1); statement.setInt(1, 4); statement.executeUpdate(); // 修改 String sql2="update user_table set user_name=? ,user_age=? where user_id=?"; PreparedStatement statement2 =(PreparedStatement) connection.prepareStatement(sql2); statement.setString(1, "懒洋洋"); statement.setInt(2, 12); statement.setInt(3, 2); statement.executeUpdate(); } }