做拍卖网站,网站建设公司使用图片侵权使用者有无责任,网站开发容易学吗,通辽网站公司使用李刚老师编著的《疯狂Java讲义》#xff08;第2版#xff09;学习MySql数据库与JDBC编程#xff08;使用Java 7 #xff09;#xff0c;其中第601页的ConnMySql.java 程序代码在Eclipse上运行#xff0c;出现错误。 import java.sql.*;
public class ConnMySql{publi…使用李刚老师编著的《疯狂Java讲义》第2版学习MySql数据库与JDBC编程使用Java 7 其中第601页的ConnMySql.java 程序代码在Eclipse上运行出现错误。 import java.sql.*;
public class ConnMySql{public static void main(String[] args)throws Exception{Class.forName(com.mysql.jdbc.Driver);try(Connection conn DriverManager.getConnection(jdbc:mysql://127.0.0.1:3306/select_test,root,0754);Statement stmt conn.createStatement();ResultSet rs stmt.executeQuery( select s.* ,teacher_name from student_table s, teacher_table t where t.teacher_id s.java_teacher)//这里出错{while(rs.next()){System.out.println(rs.getInt(1) \t rs.getString(2) \t rs.getString(3) \t rs.getString(4) );}}}} 根据提示把Sql语句从原来的使用双引号将多行语句连接起来改为一行语句编译通过。 import java.sql.*;
public class ConnMySql{public static void main(String[] args)throws Exception{Class.forName(com.mysql.jdbc.Driver);/** 采用了自动关闭资源的try语句来关闭各种数据库资源Java7改写了Connection、Statement、ResultSet等接口* 它们都继承了AutoCloseable接口因此它们都可以用try语句来关闭。* * */try(Connection conn DriverManager.getConnection(jdbc:mysql://127.0.0.1:3306/select_test,root,0754);Statement stmt conn.createStatement();ResultSet rs stmt.executeQuery(select s.* ,teacher_name from student_table s, teacher_table t where t.teacher_id s.java_teacher ))//这样改就没问题/** select s.* ,teacher_name from student_table s, teacher_table t where t.teacher_id s.java_teacher* */{while(rs.next()){System.out.println(rs.getInt(1) \t rs.getString(2) \t rs.getString(3) \t rs.getString(4) );}}}} 仔细查看代码后发现每一句跟下一句句首都没有留空格相当于 select s.* ,teacher_namefrom student_table s, teacher_table t 所以导致出错。 但是尝试先在ResultSet语句上边定义String类型变量s会提示“The resource type String does not implement java.lang.AutoCloseable”。在try语句前定义就没问题。 转载于:https://www.cnblogs.com/JLeight/archive/2013/02/15/2912785.html