• 回答数

    2

  • 浏览数

    342

雷恩哥哥
首页 > 考试培训 > 考试系统源码

2个回答 默认排序
  • 默认排序
  • 按时间排序

装修徐工

已采纳

连接MySQL数据库Java连接MySql需要下载JDBC驱动(举例,现有新版本)。然后将其解压缩到任一目录。我是解压到D盘,然后将其目录下的加到classpath里,具体如下:“我的电脑”-> “属性” -> “高级” -> “环境变量”,在系统变量那里编辑classpath,将D:\\加到最后,在加这个字符串前要加“;”,以与前一个classpath区分开。然后确定。package hqs;import .*;public class DataBasePractice { public static void main(String[] args) { //声明Connection对象 Connection con; //驱动程序名 String driver = ""; //URL指向要访问的数据库名mydata String url = "jdbc:mysql://localhost:3306/mydata"; //MySQL配置时的用户名 String user = "root"; //MySQL配置时的密码 String password = "root"; //遍历查询结果集 try { //加载驱动程序 (driver); //()方法,连接MySQL数据库!! con = (url,user,password); if(!()) ("Succeeded connecting to the Database!"); //2.创建statement类对象,用来执行SQL语句!! Statement statement = (); //要执行的SQL语句 String sql = "select * from student"; //类,用来存放获取的结果集!! ResultSet rs = (sql); ("-----------------"); ("执行结果如下所示:"); ("-----------------"); (" 学号" + "\t" + " 姓名"); ("-----------------"); String name = null; String id = null; while(()){ //获取stuname这列数据 name = ("stuname"); //获取stuid这列数据 id = ("stuid"); //首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。 //然后使用GB2312字符集解码指定的字节数组。 name = new String(("ISO-8859-1"),"gb2312"); //输出结果 (id + "\t" + name); } (); (); } catch(ClassNotFoundException e) { //数据库驱动类异常处理 ("Sorry,can`t find the Driver!"); (); } catch(SQLException e) { //数据库连接失败异常处理 (); }catch (Exception e) { // TODO: handle exception (); }finally{ ("数据库数据成功获取!!"); } }}2.添加、修改、删除操作在上面while代码段后面添加以下代码段:String name = null; String id = null; while(()){ //获取stuname这列数据 name = ("stuname"); //获取stuid这列数据 id = ("stuid"); //首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。 //然后使用GB2312字符集解码指定的字节数组。 name = new String(("ISO-8859-1"),"gb2312"); //输出结果 (id + "\t" + name); }PreparedStatement psql; ResultSet res; //预处理添加数据,其中有两个参数--“?” psql = ("insert into student values(?,?)"); (1, 8); //设置参数1,创建id为5的数据 (2, "xiaogang"); //设置参数2,name 为小明 (); //执行更新 //预处理更新(修改)数据 psql = ("update student set stuname = ? where stuid = ?"); (1,"xiaowang"); //设置参数1,将name改为王五 (2,10); //设置参数2,将id为2的数据做修改 (); //预处理删除数据 psql = ("delete from student where stuid = ?"); (1, 5); (); //查询修改数据后student表中的数据 psql = ("select*from student"); res = (); //执行预处理sql语句 ("执行增加、修改、删除后的数据"); while(()){ name = ("stuname"); id = ("stuid"); name = new String(("ISO-8859-1"),"gb2312"); (id + "\t" + name); } (); ();该代码段使用到了预处理语句:(String sql);这样生成数据库底层的内部命令,并将该命令封装在preparedStatement对象中,可以减轻数据库负担,提高访问数据库速度。 运行结果:

考试系统源码

262 评论(10)

xiaxia910000

//主类EnglishTest——import .*;import .*;import .*;public class EnglishTest extends JFrame{ TestArea testPanel=null; Container con=null; public EnglishTest() { super("模拟考试"); testPanel=new TestArea(); con=getContentPane(); (testPanel,); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { (0); } }); setVisible(true); setBounds(60,40,660,460); (); validate(); } public static void main(String args[]) { new EnglishTest(); }}//读取试题 ReadTestquestionimport .*;import .*;public class ReadTestquestion{ String filename="", correctAnswer="", testContent="" , selection="" ; int score=0; long time=0; boolean 完成考试=false; File f=null; FileReader in=null; BufferedReader 读取=null; public void setFilename(String name) { filename=name; score=0; selection=""; try { if(in!=null&&读取!=null) { (); 读取.close(); } f=new File(filename); in=new FileReader(f); 读取=new BufferedReader(in); correctAnswer=(读取.readLine()).trim(); String temp=(读取.readLine()).trim() ; StringTokenizer token=new StringTokenizer(temp,":"); int hour=(()) ; int minute=(()); int second=(()); time=1000*(second+minute*60+hour*60*60); } catch(Exception e) { testContent="没有选择试题"; } } public String getFilename() { return filename; } public long getTime() { return time; } public void set完成考试(boolean b) { 完成考试=b; } public boolean get完成考试() { return 完成考试; } public String getTestContent() { try { String s=null; StringBuffer temp=new StringBuffer(); if(读取!=null) { while((s=读取.readLine())!=null) { if(("**")) break; ("\n"+s); if(("endend")) { (); 读取.close(); 完成考试=true; } } testContent=new String(temp); } else { testContent=new String("没有选择试题"); } } catch(Exception e) { testContent="试题内容为空,考试结束!!"; } return testContent; } public void setSelection(String s) { selection=selection+s; } public int getScore() { score=0; int length1=(); int length2=(); int min=(length1,length2); for(int i=0;i

121 评论(14)

相关问答