内容发布更新时间 : 2024/12/23 3:02:06星期一 下面是文章的全部内容请认真阅读。
篇一:java实验报告五5 天津理工大学实验报告
学院(系)名称:计算机与通信工程学院源程序: 工具类:
package homework;
import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; public class dbutil {
private static string driver; private static string url;
private static string username; private static string password; static{
url=jdbc:mysql://localhost:3306/mydb?useunicode=true&characterencoding=utf-8 ;
username=root; password=null; }
public static connection open(){ try {
class.forname(driver);
return drivermanager.getconnection(url,username,password);} catch (exception e) { // todo auto-generated catch block e.printstacktrace(); }
return null; }
public static void close(connection conn){ if(conn!=null){ try {
conn.close();
} catch (sqlexception e) {
// todo auto-generated catch block e.printstacktrace(); } } } }
student类,里面包含数据库的列和set和get方法 package homework;
public class student { int id;
public int getid() {
} public void setid(int id) { } string name; this.id = id; string sex; int number; int account; string college;
public string getname() { } public void setname(string name) { } public string getsex() { } public void setsex(string sex) { } public int getnumber() { } public void setnumber(int number) { } public int getaccount() { } public void setaccount(int account) { } public string getcollege() { } public void setcollege(string college) { } @override public string tostring() {} return name; this.name = name; return sex; this.sex = sex; return number; this.number = number; return account; this.account = account; return college; this.college = college; // todo auto-generated method stub return name+-+sex+-+number+-+account++college;example类:创建一个简单的界面,使用的是borderlayout布局,然后实现增 删 改 查。 package homework;
import java.awt.borderlayout; import java.awt.flowlayout;
import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.sql.connection;
import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.util.arraylist; import java.util.list;
import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jtextfield; public class example { string [] st=null; jtextfield jt=null;
jbutton bwest=new jbutton(delete); jbutton bsouth=new jbutton(update); jbutton bnouth=new jbutton(insert); jbutton beast=new jbutton(select);
jtextfield text=new jtextfield(wake--hillsong young and free - we are young & free);
jframe fram=new jframe(java 王远志 20135632); public void createjframe(){
fram.setbounds(100, 200,470,470); fram.setvisible(true); fram.validate();
fram.setdefaultcloseoperation(jframe.dispose_on_close); fram.add(bnouth,borderlayout.north); fram.add(bsouth,borderlayout.south); fram.add(beast,borderlayout.east); fram.add(bwest,borderlayout.west); fram.add(text,borderlayout.center); }
public void setnouthlayout(){
//jframe win=createjframe();bnouth.addactionlistener(new insertlistener()); //fram.add(bnouth);//若这行存在则运行的界面会有变化,然而被我机智的找出来了 }
public void setwestlayout(){
bwest.addactionlistener(new deletelistener());
//fram.add(bwest);//若这行存在则运行的界面会有变化,然而被我机智的找出来了 }
public void setsouthlayout(){ //jframe win=createjframe(); //jbutton bsouth=new jbutton(u);
bsouth.addactionlistener(new updatelistener());
//fram.add(bsouth);//若这行存在则运行的界面会有变化,然而被我机智的找出来了 }
public void seteastlayout(){ //jframe win=createjframe(); //jbutton beast=new jbutton(select);//这样运行的结果只有一个select的按钮的图,可能是因为查询的监听器有问题
beast.addactionlistener(new selectlistener());
//fram.add(beast);//若这行存在则运行的界面会有变化,然而被我机智的找出来了 }
class insertlistener implements actionlistener{ @override
public void actionperformed(actionevent e) { // todo auto-generated method stub
jframe jf=new jframe(请按顺序输入姓名,性别,学号,帐号,学院并用空格隔开); jf.setbounds(200, 200, 222,333); jf.setvisible(true); jf.validate();
jf.setdefaultcloseoperation(jframe.dispose_on_close); flowlayout fl=new flowlayout(); fl.setalignment(flowlayout.left); jf.setlayout(fl);
jt=new jtextfield(1000);
jbutton jb=new jbutton(插入);篇二:java实验报告5 java程序设计 实验报告