public static void main(String[] args) {
Connection conn = null;
Statement stmt = null ;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
Driver driver = new com.mysql.jdbc.Driver();
DriverManager.registerDriver(driver);
String url = "jdbc:mysql:localhost:3366/huae";
String user = "root";
String password = "1234";
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
String sql = "Select e.ename,d.dname from emp e join dept d on e.deptno = d.deptno";
rs = stmt.executeQuery(sql);
while(rs.next()){
String ename = rs.getString("ename");
String dname = rs.getString("dname");
System.out.println(ename+" "+dname);
}
} catch (Exception e) {
// TODO: handle exception
}finally{
if(rs!= null){
try {
rs.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
if(stmt!=null){
try {
stmt.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
if(conn!=null){
try {
conn.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
}
// 1. 注册驱动
Class.forName("com.mysql.jdbc.Driver");
// 2. 获得连接
// uri:数据库地址 jdbc:mysql://连接主机ip:端口号//数据库名字
String url = "jdbc:mysql://localhost:3306/itheima";
// static Connection getConnection(String url, String user, String password)
// 返回值是java.sql.Connection接口的实现类,在MySQL驱动程序中
Connection conn = DriverManager.getConnection(url, "root", "root");
System.out.println(conn);// com.mysql.jdbc.JDBC4Connection@10d1f30
// 3. 获得语句执行平台,通过数据库连接对象,获取到SQL语句的执行者对象
//conn对象,调用方法 Statement createStatement() 获取Statement对象,将SQL语句发送到数据库
//返回的是Statement接口的实现类对象,在MySQL驱动程序中
Statement stat = conn.createStatement();
System.out.println(stat);//com.mysql.jdbc.StatementImpl@137bc9
// 4. 执行sql语句
//通过执行者对象调用方法执行SQL语句,获取结果
//int executeUpdate(String sql) 执行数据库中的SQL语句,仅限于insert,update,delete
//返回值int,操作成功数据库的行数
int row = stat.executeUpdate("INSERT INTO sort(sname,sprice,sdesc) VALUES('汽车用品',50000,'疯狂涨价')");
System.out.println(row);
// 5. 释放资源
stat.close();
conn.close();
String url = "jdbc:mysql:localhost:3366/huae"; 你这个写的不对吧
有错误提示吗。。错误提示贴出来