有个小问题 麻烦老师看看
来源:4-4 自由编程
shuaiyi
2019-10-13 20:37:34
这块是代码: package com.imooc.jdbc.demo1; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import org.junit.Test; import com.imooc.jdbc.utils.JDBCUtils; public class zuoye2 { @Test public void demo1(){ Connection conn = null; Statement stmt = null; ResultSet rs = null; try{ //注册驱动 //Class.forName("com.mysql.cj.jdbc.Driver"); //获得连接 conn = JDBCUtils.getConnection(); //得到执行sql语句的对象 stmt = conn.createStatement(); //编写sql语句 String sql1 = "insert goods(name,price,desp) VALUES ('手机',2000.0,'黑色,存储容量32G'),('冰箱',1500.0,'银色,对开门'),('洗衣机',3000.0,'滚筒'),('空调',4000,'变频空调');"; String sql2 = "select * from goods"; String sql3 = "select * from goods where name = '冰箱'"; String sql4 = "update goods set price = 5000 where name = '手机'"; String sql5 = "delete from goods where name = '洗衣机'"; String sql6 = "select * from goods order by price DESC"; //1 首先将表格中的四条数据添加到数据库中 int i1 = stmt.executeUpdate(sql1); if(i1>0){ System.out.println("添加成功"); }else{ System.out.println("添加失败"); } //2 显示所有数据 rs = stmt.executeQuery(sql2); while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); Float price = rs.getFloat("price"); String desp = rs.getString("desp"); } //3 查询name值为冰箱的数据并显示 rs = stmt.executeQuery(sql3); while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); Float price = rs.getFloat("price"); String desp = rs.getString("desp"); } //4 将name值为手机的数据的price值改为5000 int i4 = stmt.executeUpdate(sql4); if(i4>0){ System.out.println("修改成功"); }else{ System.out.println("修改失败"); } //5 删除name值为洗衣机的数据 int i5 = stmt.executeUpdate(sql5); if(i5>0){ System.out.println("删除成功"); }else{ System.out.println("删除失败"); } //6 按价格升序排序显示所有的数据 rs = stmt.executeQuery(sql6); while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); Float price = rs.getFloat("price"); String desp = rs.getString("desp"); } }catch(Exception e){ e.printStackTrace(); }finally{ JDBCUtils.release(rs, stmt, conn); } } } 这块是工具类: package com.imooc.jdbc.utils; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; /** * JDBC的工具类 * @author PC * */ public class JDBCUtils { private static final String driverClass; private static final String url; private static final String username; private static final String password; static{ //加载属性文件并解析: Properties props = new Properties(); //任何获得属性文件的输入流 //通常情况下使用类的加载器的方式进行获取: InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); try { props.load(is); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } driverClass = props.getProperty("driverClass"); //driverClass = "com.mysql.cj.jdbc.Driver"; url = props.getProperty("url"); //url = "jdbc:mysql://localhost:3306/goods?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT"; username = props.getProperty("username"); //username = "root"; password = props.getProperty("password"); //password = "Ilululu014597"; } /** * 注册驱动的方法 * @throws ClassNotFoundException */ public static void loadDriver() throws ClassNotFoundException{ Class.forName(driverClass); } /** * 获得连接的方法 * @throws Exception */ public static Connection getConnection() throws Exception{ loadDriver();//注册驱动 Connection conn = DriverManager.getConnection(url,username,password); return conn; } /** * 资源释放 */ public static void release(Statement stmt,Connection conn){ if(stmt!=null){ try{ stmt.close(); }catch(SQLException e){ e.printStackTrace(); } stmt = null; } if(conn!=null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); } conn = null; } } public static void release(ResultSet rs,Statement stmt,Connection conn){ if(rs!=null){ try{ rs.close(); }catch(SQLException e){ e.printStackTrace(); } rs = null; } if(stmt!=null){ try{ stmt.close(); }catch(SQLException e){ e.printStackTrace(); } stmt = null; } if(conn!=null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); } conn = null; } } } 这块是properties: driverClass = "com.mysql.cj.jdbc.Driver" url = "jdbc:mysql://localhost:3306/goods?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT" username = "root" password = "Ilululu014597"
问题就是我工具类中注释掉的部分:
driverClass = props.getProperty("driverClass");
//driverClass = "com.mysql.cj.jdbc.Driver";
url = props.getProperty("url");
//url = "jdbc:mysql://localhost:3306/goods?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT";
username = props.getProperty("username");
//username = "root";
password = props.getProperty("password");
//password = "Ilululu014597";
用了properties然后出错了 然后我就不用properties直接写在工具类里面然后就运行成功了
这是为什么呢 调用properties写错了吗 麻烦老师看看
1回答
好帮手慕阿满
2019-10-14
同学你好,在jdbc.properties中,driverClass,url等后边的双引号需要去掉,如:
建议同学修改再试试。
另外关于类命名的问题,如果由一个字母组成,则首字母需要大写。如果由多个单词组成,则之后每个单词的首字母都要大写。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
相似问题