import java.sql.*;
import java.util.ResourceBundle;
public class JBDCTEST {
public static void main(String[] args) {
Connection c=null;
Statement s=null;
try {
//方式一
//DriverManager.registerDriver(new com.mysql.jdbc.Driver()); // 需要导入jar包!!!
//方式二,常用!!! 低耦合
ResourceBundle re=ResourceBundle.getBundle("xinxi");
String driver=re.getString("driver");
String url=re.getString("url");
String user=re.getString("user");
String password=re.getString("password");
//注册驱动
Class.forName(driver); //不需要接收返回值,类加载实现静态代码块!
//连接数据库
c=DriverManager.getConnection(url,user,password);
System.out.println(c);
//获取操作对象
s=c.createStatement();
//执行SQL语句
String sql="insert into dept values(888888,'hzx','hhh')";
int count=s.executeUpdate(sql); //返回更新的记录数
if(count==1){
System.out.println("successful");
};
String sql2="select * from emp";
//返回结果集
ResultSet rs = s.executeQuery(sql2);
while(rs.next()){
String s1=rs.getString(1);
String s2=rs.getString(2);
String s3=rs.getString(3);
System.out.println(s1+s2+s3);
}
s.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
运行结果