1.下载mysql的驱动包
2.加载驱动到eclipse
方式一:创建目录,将msql的jar包放入,build path,
方式二:将下载好的jar包的路径复制,然后将路径放到配置好的ClASSPATH中,格式是 .;路径
3.开始连接,加载驱动。
Class.forName("com.mysql.jdbc.Driver");
4.建立连接
Connection conn=DriverManager(url,usename,password);
url:是要连接的数据库的路径 jdbc:mysql://localhost:3306/数据库名字?useUnicode=true&character=UTF8 绿色的部分是防止中文乱码而设置的字符编码可以不要。
usename:是指连接的数据库的名称
password:是连接的数据库的密码
5.发送sql语句
Statement st=conn.createStatement();
int result=st.executeUpdate(sql); //要发送的sql语句,返回的结果是int型,就相当于几行受影响
6。处理结果
if(result>0) {
System.out.println("删除数据成功!");
}else {
System.out.println("删除失败...");
}
7.关闭连接
finally{
if(conn!=null){
conn.close;
}
}