mysql - 将数据插入数据库的这段代码抛出异常。帮助我了解此代码。我是 StackOverflow 的新手
问题描述
<%
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
String o=request.getParameter("Order_ID");
String c=request.getParameter("Customer_Name");
String c2=request.getParameter("Customer_ID");
String o2=request.getParameter("Order_Amount");
String n=request.getParameter("Notes");
String o3=request.getParameter("Order_Date");
String connectionURL = "jdbc:mysql://localhost:3306/winter_internship";
PreparedStatement pstatement = null;
// Load JBBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").getDeclaredConstructor().newInstance();
int updateQuery = 0;
if(o!=null && c!=null && c2!=null && o2!=null && n!=null && o3!=null){
// check if the text box having only blank spaces
if(o!="" && c!="" && c2!="" && o2!="" && n!="" && o3!="") {
try {
connection = DriverManager.getConnection(connectionURL, "root", "misspooh2068");
// sql query to insert values in the secified table.
String queryString = "insert into order_details(Order_ID,Customer_Name,Customer_ID,Order_Amount,Notes,Order_Date) values(?,?,?,?,?,?)";
pstatement = connection.prepareStatement(queryString);
pstatement.setString(1, o);
pstatement.setString(2, c);
pstatement.setString(3, c2);
pstatement.setString(4, o2);
pstatement.setString(7, n);
pstatement.setString(8, o3);
updateQuery = pstatement.executeUpdate();
if (updateQuery != 0) { %>
<br>
<TABLE style="background-color: #E3E4FA;"
WIDTH="30%" border="1">
<tr><th>Data is inserted successfully
in database.</th></tr>
</table>
<%
}
}
catch (Exception ex) {
out.println("Unable to connect to database.");
}
finally {
// close all the connections.
pstatement.close();
connection.close();
}
}
}
%>
当我运行此代码时,它会引发异常并返回无法连接到数据库
试图找到许多解决方案,但不断抛出异常。我是 StackOverflow 的新手。
干杯伙计们!帮帮我吧伙计们:)
解决方案
推荐阅读
- ssl - 如何从以 PEM 格式编码的 CRT 创建 JWKs 密钥
- python-3.x - 列表和字典:哪个更快
- xpath - IMPORTHTML 或 IMPORTXML 从站点收集数据
- c# - 从不同项目调用时返回 null 的模拟方法
- python - 尝试使用 Python 和 BeautifulSoup 进行网络抓取的初学者
- token - ID Token 的 Lifespan 值是多少?
- javascript - 子组件在渲染时调用父函数
- amazon-web-services - 在运行 express / nginx 的 ECS 服务之前,AWS ALB 出现间歇性 502 网关错误
- javascript - 将 firestore DB 设置为 window 对象有多安全?
- c - 将数组传递给函数和显示元素时出错?