书接上文
41.关于形参
public class Test {
static void ParTest1(int a,char b,int[] c,char[] d,String e,String [] f) {
a = 2;
b = 'a';
c[1] = 4;
d[1] = 's';
e = "ZXcZXcZX";
f[1] = "adsfasd";
}
public void ParTest2(int a,char b,int[] c,char[] d,String e,String [] f) {
a = 2;
b = 'a';
c[1] = 4;
d[1] = 's';
e = "ZXcZXcZX";
f[1] = "adsfasd";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 1;
char b = 'c';
int [] c = {1,2,3};
char [] d = {'s','h','l'};
String str = "a string";
String [] strarr = {"first","second","third"};
ParTest1(a, b, c, d, str, strarr);
System.out.println(a+" "+b+" "+c[1]+" "+d[1]+" "+str+" "+strarr[1]);
// Test t = new Test();
// t.ParTest2(a, b, c, d, str, strarr);
// System.out.println(a+" "+b+" "+c[1]+" "+d[1]+" "+str+" "+strarr[1]);
}
}
1 c 4 s a string adsfasd
42. 在做项目时,经常通过继承来简化和规范编程,因为经常要从数据库中提取数据,所以经常把数据库中的一个个表在项目中定义成实体类,其中有一个BaseEntity,被其他实体继承,其中包括每个表中共有的字段,比如修改时间,修改人等
package com.Entity;
import java.util.Date;
public abstract class BaseEntity {
private String uId;//主键,用户ID
private Date creationDate;//信息录入时间
private Date modifiedDate;//最后一次修改的时间
private String modifiedUserId;//最后一次修改的人的ID
public String getuId() {
return uId;
}
public void setuId(String uId) {
this.uId = uId;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Date getModifiedDate() {
return modifiedDate;
}
public void setModifiedDate(Date modifiedDate) {
this.modifiedDate = modifiedDate;
}
public String getModifiedUserId() {
return modifiedUserId;
}
public void setModifiedUserId(String modifiedUserId) {
this.modifiedUserId = modifiedUserId;
}
}
package com.Entity;
public class T_Tel extends BaseEntity {
private String telId;
private String telNumber;
public String getTelId() {
return telId;
}
public void setTelId(String telId) {
this.telId = telId;
}
public String getTelNumber() {
return telNumber;
}
public void setTelNumber(String telNumber) {
this.telNumber = telNumber;
}
}
package com.Entity;
public class T_User extends BaseEntity {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
package com.Dao;
import com.Entity.BaseEntity;
public abstract class BaseDao <T extends BaseEntity>{
public abstract void insert(T t);
public abstract void update(T t);
public abstract T selectByPK(long PK);
public abstract void delete(T t);
public abstract void deleteByPK(long PK);
}
package com.Dao;
import com.Entity.T_Tel;
public class TelDao extends BaseDao<T_Tel> {
@Override
public void insert(T_Tel t) {
// TODO Auto-generated method stub
}
@Override
public void update(T_Tel t) {
// TODO Auto-generated method stub
}
@Override
public T_Tel selectByPK(long PK) {
// TODO Auto-generated method stub
return null;
}
@Override
public void delete(T_Tel t) {
// TODO Auto-generated method stub
}
@Override
public void deleteByPK(long PK) {
// TODO Auto-generated method stub
}
}
package com.Dao;
import com.Entity.T_User;
public class UserDao extends BaseDao<T_User> {
@Override
public void insert(T_User t) {
// TODO Auto-generated method stub
}
@Override
public void update(T_User t) {
// TODO Auto-generated method stub
}
@Override
public T_User selectByPK(long PK) {
// TODO Auto-generated method stub
return null;
}
@Override
public void delete(T_User t) {
// TODO Auto-generated method stub
}
@Override
public void deleteByPK(long PK) {
// TODO Auto-generated method stub
}
}
43.
之前总是在想,学会了sql,我要怎么做到通过java代码从数据库提取出代码并显示在控制台,因为做网页或者很多项目经常需要把数据进行增删改查,并显示在网页中,别忘了需要导入connection的jar包
employee表是这个样子的
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class GetInfoFroDB {
public static void main(String[] args) {
// TODO Auto-generated method stub
String url = "jdbc:mysql://localhost/activity";
String user = "root";
String password = "root";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement pstmt = conn.prepareStatement("select firstname,lastname from employee where id > ?");
pstmt.setString(1, "3");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString(1) + " " + rs.getString(2));
}
pstmt.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}