Javabean+jsp+jdbc传值问题

DBUtil.java:

 package db;

import java.sql.*;


public class DBUtil {


    private static final String URL="jdbc:mysql://127.0.0.1:3306/hotel?useUnicode=true&characterEncoding=utf-8";。
    private static final String USER="root";
    private static final String PASSWORD="1234";
    private static Connection conn=null;
    static {


        try {
            Class.forName("com.mysql.jdbc.Driver");


            conn=DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static Connection getConnection(){
        return conn;
    }

}

message_list.jsp:

 <%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.sql.*,java.util.*,javabean.Message"%>

<%
    Message mes_temp = new Message();

    String sql_temp = "select  message_id from message ";
    String front_id_string = mes_temp.excuteTempSql(sql_temp);


    out.print(front_id_string);

%>

Message.java:

  package javabean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import db.DBUtil;

public class Message {
    private int message_id;
    private String  nickname;
    private String photo_select;
    private String qq;
    private String email;
    private String tel;
    private String content;
    private Timestamp message_addedtime;
    private String content_reply;

    private Connection conn;
    private ResultSet rs;
    private Statement stmt;

    private String front_id_string;

    public String excuteTempSql(String sql_temp){
    //sql_temp可以传进值
        //断点调试到此处时,conn为null?
        conn = DBUtil.getConnection();
        try {
            stmt = conn.createStatement();
                        //stmt为null
            rs = stmt.executeQuery(sql_temp);
            while (rs.next()) {
                front_id_string = front_id_string + rs.getInt("checkin_id") + ",";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return front_id_string;
    }

}

问题:注释部分。

2个回答

如果断点在此行:stmt = conn.createStatement(); stmt就是null,还没有执行么。

貌似你只是声明了一下,并没有用new创建该对象!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问