import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class Demo01 { public static void main(String[] args) throws ClassNotFoundException, SQLException { //1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2.连接数据库(获取连接对象) String url="jdbc:mysql://127.0.0.1:3306/java 1018?characterEncoding=UTF-8"; String user="root"; String password="123456"; Connection con = DriverManager.getConnection(url,user,password); //System.out.println(con); //3.获得语句执行对象 Statement sta = con.createStatement(); String sql = "insert into sort(sname) values('小裙子')"; //4.执行sql语句 int row= sta.executeUpdate(sql); System.out.println(row); //5.释放资源(先开的后关) sta.close(); } }
.