使用mysql数据库,现在项目中导入mysql的夹包,并添加到环境路径中去,具体的办法是新建一个文件夹在把夹包放在他下面,也可以直接右键单击添加到执行路径,在eclipse里,具体连接如下;
package com.mysql.jdbc;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args){
demo1();
}
private static void demo1() {
Connection conn=null;
Statement stmt=null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql-me","root","123456");
stmt=conn.createStatement();
String sql2="delete from emp where empno=9999";
int count2= stmt.executeUpdate(sql2);
System.out.println("sql删除了"+count2+"条记录");
}catch (SQLException e) {
System.out.println("与数据库通信出问题");
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.out.println("驱动类没有找到");
}finally{
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}