大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Employee employee1 = new
创新互联是一家集网站建设,轮台企业网站建设,轮台品牌网站建设,网站定制,轮台网站建设报价,网络营销,网络优化,轮台网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
Employee();
List employees = new ArrayList();
// add employee
employees.add(employee1);
// delete employee
employees.remove(employee1);
// search employee
for each ( emp : employee){
if (emp.name.equals("Ken")){
print("cool");
}
}
Employee类:
public class Employee {
private int id;
private String name;
private int age;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Manager类:
import java.util.ArrayList;
import java.util.List;
public class Manager {
public static ListEmployee employees = new ArrayListEmployee();
public static void addEmployee(Employee employee){
employees.add(employee);
}
public static void deleteEmployee(Employee employee){
employees.remove(employee);
}
public static void updateEmployee(Employee employee){
int len = employees.size();
for(int i=0;ilen;i++){
Employee e = employees.get(i);
if(e.getId() == employee.getId()){
deleteEmployee(e);
addEmployee(employee);
}
}
}
public static Employee selectEmployeeById(int id){
int len = employees.size();
if(len!=0){
Employee emp = new Employee();
for(int i=0;ilen;i++){
Employee e = employees.get(i);
if(e.getId() == id){
emp = e;
}
}
return emp;
}else{
return null;
}
}
public static Employee selectEmployeeByName(String name){
int len = employees.size();
if(len!=0){
Employee emp = new Employee();
for(int i=0;ilen;i++){
Employee e = employees.get(i);
if(e.getName().equals(name)){
emp = e;
}
}
return emp;
}else{
return null;
}
}
public static void printEmployees(){
int len = employees.size();
if(len != 0){
for(int i=0;ilen;i++){
System.out.println(employees.get(i).getId()+":\t"+
employees.get(i).getName()+"\t"+
employees.get(i).getAge()+"\t"+
employees.get(i).getEmail());
}
}else{
System.out.println("无员工");
}
}
}
测试EmpManaTest类:
public class EmpManaTest {
public static void main(String[] args) {
Employee e = new Employee();
e.setId(1);
e.setName("zz");
e.setAge(20);
e.setEmail("zz@163.com");
Manager.addEmployee(e);
Employee e2 = new Employee();
e2.setId(2);
e2.setName("scof");
e2.setAge(18);
e2.setEmail("scofield@mail.com");
Manager.addEmployee(e2);
System.out.println("添加员工后:");
Manager.printEmployees();
System.out.println("----------------------------------------------");
Employee emp = new Employee();
emp.setId(1);
emp.setName("virus");
emp.setAge(30);
emp.setEmail("virus@163.com");
Manager.updateEmployee(emp);
System.out.println("修改员工后:");
Manager.printEmployees();
System.out.println("----------------------------------------------");
System.out.println("查询员工ByID:");
Employee empSelectId = Manager.selectEmployeeById(1);
System.out.println(empSelectId.getName());
System.out.println("----------------------------------------------");
System.out.println("查询员工ByName:");
Employee empSelectName = Manager.selectEmployeeByName("virus");
System.out.println(empSelectName.getEmail());
System.out.println("----------------------------------------------");
System.out.println("删除一个员工后:");
Manager.deleteEmployee(emp);
Manager.printEmployees();
}
}
这个是不是你想要的...........
第一步:新建数据库
连接的是本地localhost,新建一个新的数据库名是jdbctest
然后建表t_emp
不会的话可通过执行下方的sql语句建表
CREATE TABLE `t_emp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`salary` double DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
第二步:新建java项目
新建完以后添加mysql驱动的jar包,jar包自己下载
在项目上右键鼠标属性,然后
添加jar包,我这里已经加载过了
第三步:编写代码
package com.gf;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class Test {
public static void main(String[] args) throws Exception {
int flag=0;
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
Connection conn=(Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbctest?user=rootpassword=123456useUnicode=truecharacterEncoding=UTF-8");
//3.创建statement
Statement sm=(Statement) conn.createStatement();
//4.执行sql语句
flag=sm.executeUpdate("insert into t_emp(name,salary) values('菲菲',34.9)");
if(flag!=0) {
System.out.println("员工信息增加成功");
}else {
System.out.println("添加失败");
}
}
}
注意点:
---------------------------------------------------------------------------------
DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbctest?user=rootpassword=123456useUnicode=truecharacterEncoding=UTF-8");
这里需要修改自己本机的连接信息,不然会出现连接失败
最后的执行结果
从无到有这么编写比较好。我老师给了我一堆东西。叫我往里面添加。我现在都弄不明白里面哪里有错。eclipse太高级了太难学了。
对数据库进行增删改查?
以下是 sql server 2013+java.实现的是对MSC对象的增删改查.
需要下载连接驱动程序:com.microsoft.sqlserver.jdbc.SQLServerDriver
网上搜一下就行
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
class MSC
{
public String MscID;
public String MscName;
public String MscCompany;
public float MscLongitude;
public float MscLatitude;
public float MscAltitude;
public MSC(String MscID, String MscName, String MscCompany,
float MscLongitude, float MscLatitude,float MscAltitude){
this.MscID = MscID;
this.MscName = MscName;
this.MscCompany = MscCompany;
this.MscLongitude =MscLongitude;
this.MscLatitude = MscLatitude;
this.MscAltitude = MscAltitude;
}
}
public class sqlserverjdbc {
public Connection getConnection(){
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=gsm"; //连接服务器和数据库sample
String userName = "sa"; //默认用户名
String userPwd = "123"; //密码
Connection dbConn = null;
try {
Class.forName(driverName);
dbConn =DriverManager.getConnection(dbURL, userName, userPwd);
} catch (Exception e) {
e.printStackTrace();
}
return dbConn;
}
public void printUserInfo(){
Connection con = getConnection();
Statement sta = null;
ResultSet rs = null;
System.out.println("打印表格MSC信息");
try {
sta = con.createStatement();
rs = sta.executeQuery("select * from MSC信息");
System.out.println("MscID\tMscName\tMscCompany\tMscLongitude\tMscLatitude\tMscAltitude");
while(rs.next()){
System.out.println(rs.getString("MscID")+"\t"+
rs.getString("MscName")+"\t"+
rs.getString("MscCompany")+"\t"+
rs.getFloat("MscLongitude")+"\t"+
rs.getFloat("MscLatitude")+"\t"+
rs.getFloat("MscAltitude"));
}
con.close();
sta.close();
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("打印完成\n");
}
public void delete(String MscID){
Connection con = getConnection();
String sql = "delete from MSC信息 where MscID = " + MscID;
PreparedStatement pst;
System.out.println("删除表格MSC信息中 ID = "+MscID+"的记录");
try {
pst = con.prepareStatement(sql);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("记录删除失败!!!");
}
System.out.println("记录删除成功!!!\n");
}
public void insert(MSC msc){
Connection con = getConnection();
String sql = "insert into MSC信息 values(?,?,?,?,?,?)";
PreparedStatement pst;
System.out.println("插入一条记录");
try {
pst = con.prepareStatement(sql);
pst.setString(1, msc.MscID);
pst.setString(2, msc.MscName);
pst.setString(3, msc.MscCompany);
pst.setFloat(4, msc.MscLongitude);
pst.setFloat(5, msc.MscLatitude);
pst.setFloat(6, msc.MscAltitude);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("插入失败!!!");
}
System.out.println("插入成功!!!\n");
}
//更新MscID的MscName
public void updateMscName(String MscID, String MscName){
Connection con = getConnection();
String sql = "update MSC信息 set MscName = ? where MscID = ?";
PreparedStatement pst;
System.out.println("修改一条记录");
try {
pst = con.prepareStatement(sql);
pst.setString(1, MscName);
pst.setString(2, MscID);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改失败!!!");
}
System.out.println("修改成功!!!\n");
}
public static void main(String args[]){
sqlserverjdbc sql = new sqlserverjdbc();
sql.printUserInfo();
sql.delete("1111");
sql.printUserInfo();
sql.updateMscName("5215", "联想");
sql.printUserInfo();
sql.insert(new MSC("1111", "中兴" ," 中兴", (float)12.2, (float)3.4,(float)45.5));
sql.printUserInfo();
}
}
传不上去。文字太多。
我把代码写到博客上了,你到博客看吧。
jsp+servlet