大家好,大家看了上一篇小白的博客,应该已经连上了数据库吧,现在就要打开我们的eclipse进行编程了!!!
首先,先新建一个Java项目,然后在这个项目中构建我们所需要的包——MVC(model-view-control)下面我先来上个图
这里本小白一共分了5个包包哦!!
1.model层用来存储你数据表中的数据,进行基础构建运用get/set方法,tostring方法等。。。
2.view层主要用于下一步的页面设计,其中运用SWING来设计
3.Dao层用于对数据表中数据的逻辑操作,进行逻辑运算等等
注:这里的util层是判断字符串所运用的工具类,action层主要是为了进行对于各个功能所进行测试使用的。
重要的来了在你的整个项目中,需要引进jar包来进行数据库的操作
1.首先右键你自己创建的项目,找到构建路径——配置构建路径
这里已经添加完了,各位作者们没加的在模块路径里找到添加外部jar,然后进行添加和选择,然后点击应用关闭即可
下面就进行Util层的操作了,上代码:
public class Util {
private static final String url="jdbc:mysql://localhost:3306/text";
private static final String username="root";
private static final String password="dlhh1222";
Connection con=null;
Statement statement=null;
ResultSet res =null;
public static Connection getCon() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url,username,password);
return con;
}
public Util() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, username, password);
statement = con.createStatement();
} catch (ClassNotFoundException e) {
System.out.println("找不到driver");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
这里是两种方法的连接,在后期运用函数接的时候,可以构建新的对象,也可以把Connection作为函数的参数写入,随个人意愿。
注意:url,username,password要根据自己的配置进行修改!!!
在控制台中进行测试,如果未找到Driver就是失败了,但是失败是成功之母所以一定会成功的
然后进行数据库表的编写,这样我们的model层内容就可以丰富了!
直接上图navicat数据
这两个图是一个学生表,和一个登陆表,里面的数据就自己写吧!!
在model层分别新建两个类学生类和登陆类,数据类型要和表中所创数据类型相对应,然后快捷生成get/set方法进行构建
成功之后,可以在测试类中在控制台输出数据库中的数据。。。
public class Student {
private String num;
private String name;
private String sex;
private int age;
private String mobile;
private String course;
private String grade;
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public Student(String num, String name, String sex, int age,String mobile2, String course, String grade) {
super();
this.num = num;
this.name = name;
this.sex = sex;
this.age = age;
this.mobile = mobile2;
this.course = course;
this.grade = grade;
}
public Student() {}
@Override
public String toString() {
return "Student [num=" + num + ", name=" + name + ", sex=" + sex + ", age=" + age + ", mobile=" + mobile
+ ", course=" + course + ", grade=" + grade + "]";
}
}
public class User {
private String username;
private String password;
public String getUserName() {
return username;
}
public void setUserName(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public User() {
super();
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
}
希望大家能得到些许帮助,也希望大家多多批评指正,我是一只努力上进的小白,大家下次再见