标签:数据 oat 表示 nec com parameter 设置 输入 getc
连接数据库步骤:
注册驱动:Class.forName("com.mysql.jdbc.Driver");
设置数据库地址、用户名、密码:String url = "jdbc:mysql://localhost:3306/database";
创建连接:Connection conn = DriverManager.getConnection(url, username, password);
创建sql语句字符串:String sql = "insert into tb_books(name,price,bookCount,author) values(?,?,?,?)";
使用PreparedStatement处理sql语句:PreparedStatement ps = conn.prepareStatement(sql);
设置缺省数据:ps.setString(1, variable);
执行sql语句,返回受影响行数:int row = ps.executeUpdate();
释放对象:ps.close();
关闭连接:conn.close();
out.println(request.getParameter("price")); double price = Double.parseDouble(request.getParameter("price")); out.println(price); try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection conn = DriverManager.getConnection(url, username, password); String sql = "insert into tb_books(name,price,bookCount,author) values(?,?,?,?)"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, book.getName()); ps.setDouble(2, price); ps.setInt(3, book.getBookCount()); ps.setString(4, book.getAuthor()); int row = ps.executeUpdate(); if (row > 0) { out.print("成功添加了" + row + "条数据!"); } ps.close(); conn.close(); } catch (Exception e) { out.print("添加失败!"); e.printStackTrace(); }
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
if (conn != null) {
out.println("success!!");
conn.close();
} else {
out.println("fail");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
Q:输入的是小数,但是存入之后显示的是四舍五入后的整数。
A:float最多表示7位有效数据。double最多表示16位有效数据。改了double类型的位数为0,就好了。
标签:数据 oat 表示 nec com parameter 设置 输入 getc
原文地址:http://www.cnblogs.com/praytell/p/7651939.html