try { Class.forName(drivername); Connection conn = DriverManager.getConnection(dbur1,dbuser,dbpwd); String sql = "select * from user where username ='"+username+"' and password ='"+password+"'"; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); if(rs.next()) { // response.sendRedirect("comd.jsp"); out.write("1"); } else { // response.sendRedirect("index.jsp"); out.write("0"); } } catch (Exception e) { System.out.println(e.getMessage()); } 无法给返回值
时间: 2024-02-10 14:10:05 浏览: 127
您可以尝试在 `out.write("1");` 和 `out.write("0");` 语句之前添加 `out.flush();` 语句,以确保数据被正确地写入响应流。如果仍然无法返回值,请检查您的前端代码是否正确处理了 AJAX 响应,并且没有发生任何错误。您可以在浏览器控制台中查看 AJAX 响应的细节,以便更好地了解问题的原因。如果仍然无法解决问题,请提供更多详细信息,以便我能够更好地帮助您。
相关问题
package T1; import java.sql.Connection; import java.sql.DriverManager; public class t1 { public static void main(String[] args) { String driverName="com.mysql.jdbc.Driver"; String userName="root"; String userPwd="1234"; String dbName="student"; String url1="jdbc:mysql://localhost:3306/"+dbName; String url2="?user="+userName+"&password="+userPwd; String url3="&useUnicode=true&characterEncoding=UTF-8"; String url=url1+url2+url3; Class.forName(driverName); Connection conn=DriverManager.getConnection(url); } }
"&password=" userPwd "&useSSL=false&serverTimezone=UTC"; //拼接连接字符串 try { //1、加载驱动 Class.forName(driverName); //2、获取连接 Connection conn=DriverManager.getConnection(url1+url2); System.out.println("数据库连接成功!"); } catch (Exception e) { e.printStackTrace(); System.out.println("数据库连接失败!"); } }
你希望了解什么关于这段Java代码片段的信息?
检查下面代码错误package T1; import java.sql.Connection; import java.sql.DriverManager; public class t1 { public static void main(String[] args) { String driverName="com.mysql.jdbc.Driver"; String userName="root"; String userPwd="1234"; String dbName="student"; String url1="jdbc:mysql://localhost:3306/"+dbName; String url2="?user="+userName+"&password="+userPwd; String url3="&useUnicode=true&characterEncoding=UTF-8"; String url=url1+url2+url3; Class.forName(driverName); Connection conn=DriverManager.getConnection(url); } }
" & "password=" userPwd "&useUnicode=true&characterEncoding=UTF8"; try { Class.forName(driverName); Connection conn=DriverManager.getConnection(url1+url2); System.out.println("连接成功!"); } catch (Exception e) { System.out.println("连接失败!"); e.printStackTrace(); } } }
您的代码错误在于一些符号的错误。
1. 第8行中,应该使用加号(+)将 dbName 拼接到 url1 后面。所以应该是:
String url1="jdbc:mysql://localhost:3306/" + dbName;
2. 第9行中,也应该使用加号(+)将 userName 和 userPwd 拼接到 url2 后面。所以应该是:
String url2="?user=" + userName + "&password=" + userPwd + "&useUnicode=true&characterEncoding=UTF8";
修正后的代码如下:
package T1;
import java.sql.Connection;
import java.sql.DriverManager;
public class t1 {
public static void main(String[] args) {
String driverName="com.mysql.jdbc.Driver";
String userName="root";
String userPwd="1234";
String dbName="student";
String url1="jdbc:mysql://localhost:3306/" + dbName;
String url2="?user=" + userName + "&password=" + userPwd + "&useUnicode=true&characterEncoding=UTF8";
try {
Class.forName(driverName);
Connection conn=DriverManager.getConnection(url1+url2);
System.out.println("连接成功!");
} catch (Exception e) {
System.out.println("连接失败!");
e.printStackTrace();
}
}
}
希望这能帮助您解决问题。
阅读全文