如何插入mysql中的插入数据

时间:2017-05-04 18:13:08

标签: java mysql

我已经建立了测验网络项目。 我想将下面提到的测试问题插入数据库。我正在html textarea中写这个问题,然后使用inner.html从html获取getdata:

enter image description here

但是当我从数据库中选择这个问题时,看起来就是这样,它很难读,写得不小心:

enter image description here

帮助我使上述测试可读。

我的sql插入代码:

 public void addtest(TestModel test) throws Exception {
    Connection c = null;
    PreparedStatement ps = null;
 String sql = "INSERT INTO TEST_TABLE (QUESTION,A,B,C,D,E,QUESTION_TYPE,SCORE,SUBJECT, CORRECT, Variant) " +
                 " VALUES (?,?,?,?,?,?,?,?,?,?,?) ";

    try {

        c= DbHelper.getConnection();
        if(c != null) {
            ps = c.prepareStatement(sql);
            ps.setString(1,test.getQuestion());
            ps.setString(2,test.getOptionA());
            ps.setString(3,test.getOptionB());
            ps.setString(4,test.getOptionC());
            ps.setString(5,test.getOptionD());
            ps.setString(6,test.getOptionE());
            ps.setString(7,test.getQuestionType());
            ps.setLong(8,test.getScore());
            ps.setLong(9,test.getSubjectId());
            ps.setString(10,test.getCorrectOption());
            ps.setInt(11,test.getVariant());


            ps.execute();

        }  else {
            System.out.println("Connection is null");
        }
    }  catch (Exception ex)  {
        ex.printStackTrace();

    }  finally {

        JdbcUtility.close(c,ps,null);
    }

}

我的sql选择代码:

     public TestModel getquestionlist(long firstpage,int variant) throws Exception {
    TestModel testdata = new TestModel();

    Connection c = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    String sql =" SELECT  TE.ID,TE.QUESTION,TE.A,TE.B,TE.C,TE.D,TE.E,TE.F,TE.G,TE.QUESTION_TYPE,TE.SCORE,S.NAME as Subject,TE.CREATE_DAY,TE.CORRECT, d.value, TE.Variant FROM TEST_TABLE TE " +
            "INNER JOIN SUBJECT S ON  S.ID = TE.SUBJECT " +
            "inner join dictionary d on d.ID = TE.Variant " +
            "WHERE TE.ACTIVE =1 AND S.ACTIVE =1 AND TE.Variant = ? " +
            "LIMIT ?,1; ";

    try {

        c = DbHelper.getConnection();

        if (c != null) {

            ps = c.prepareStatement(sql);
            ps.setInt(1,variant);
            ps.setLong(2,  firstpage);

            rs = ps.executeQuery();

            while (rs.next()) {

                testdata.setId(rs.getLong("ID"));
                testdata.setQuestion(rs.getString("QUESTION"));
                testdata.setOptionA(rs.getString("A"));
                testdata.setOptionB(rs.getString("B"));
                testdata.setOptionC(rs.getString("C"));
                testdata.setOptionD(rs.getString("D"));
                testdata.setOptionE(rs.getString("E"));
                testdata.setOptionF(rs.getString("F"));
                testdata.setOptionG(rs.getString("G"));
                testdata.setQuestionType(rs.getString("QUESTION_TYPE"));
                testdata.setScore(rs.getLong("SCORE"));
                testdata.setTestSubject(rs.getString("Subject"));
                testdata.setCreateDate(rs.getDate("CREATE_DAY"));
                testdata.setCorrectOption(rs.getString("CORRECT"));
                testdata.setVariant(rs.getInt("Variant"));

            }

        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        JdbcUtility.close(c, ps, rs);
    }

    return testdata;
}

1 个答案:

答案 0 :(得分:1)

您可以使用<br/>来破解行:

"1: public class WaterBottle { <br/>"

因此,在获得结果的那一刻,您可以将结果与<br/>连接起来,例如:

testdata.setQuestion(rs.getString("QUESTION") + "<br/>");