不需要框架,楼主也不能把这么多java代码写到jsp里。你写个util类,然后通过jsp调用util,返回的结果再显示到jsp

解决方案 »

  1.   

    util 是DButil么?  我有这个包   我主要不知道具体代码应该怎么调用,package zt.util;import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    public class DButil2 { private static String url="jdbc:mysql:///orderfoods";
    private static String driver;
    private static String user="root";
    private static String psw="root";
     // 获得数据库连接
    static {
    try {
    // mysql com.mysql.jdbc.Driver
    //Class.forName("com.mysql.jdbc.Driver");
    DriverManager.registerDriver(new com.mysql.jdbc.Driver());
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } // DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    //
    // System.setProperty("jdbc.drivers",
    // "oracle.jdbc.driver.OracleDriver");
    } public static Connection getConnection() {
    Connection conn = null;
    try {
    conn = DriverManager.getConnection(url, user, psw);
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return conn;
    } /**
     * 
     * @param conn
     * @return Statement 语句对象,语句对象执行SQL
     */
    public static Statement getStatement(Connection conn) {
    Statement stmt = null;
    try {
    stmt = conn.createStatement();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return stmt;
    } /**
     * 
     * @param conn
     * @param sql
     *            insert into t_user values (?,?,?)
     * @return PreparedStatement 预编译语句对象 性能较Statement好,解决SQL注入问题
     */
    public static PreparedStatement getPreparedStatement(Connection conn,
    String sql) {
    PreparedStatement pstmt = null;
    try {
    pstmt = conn.prepareStatement(sql);
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return pstmt;
    } public static void closeConnection(Connection conn) {
    if (conn != null) {
    try {
    if (!conn.isClosed()) {
    conn.close();
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    } public static void closeStatement(Statement stmt) {
    if (stmt != null) {
    try {
    stmt.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
     /* 读属性文件
     */
    private void readProperties(){
    Properties pro = new Properties();
    File file = null;
    InputStream in = null;
    try {
    file = new File("src/db.properties");
    in = new FileInputStream(file);
    pro.load(in);
    if(pro != null){
    url = pro.getProperty("url");
    driver = pro.getProperty("driver");
    user = pro.getProperty("user");
    psw = pro.getProperty("psw");
    }

    } catch (Exception e) {
    e.printStackTrace();
    }finally{
    try {
    in.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    // public static void main(String[] args) {
    // new DButil();
    // }
    }
    这是我的util
      

  2.   

    再写个类,调用你这个util,要把sql传进去,没看到你返回结果集的方法啊,请加q:779815835
      

  3.   

    问题已解决https://blog.csdn.net/yelongacui/article/details/88249830