扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
对数据库进行增删改查?
专注于为中小企业提供成都网站制作、成都做网站、外贸营销网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业云冈免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了近千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
以下是 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();
}
}
class Employee {
private String name;
private String department;
private double salary;
//构造方法
public Employee(String name, String department, double salary) {
this.name = name;
this.department = department;
this.salary = salary;
}
public String toString() {
return "姓名:" + name + "\t部门:" + department + "\t工资:" + salary;
}
public void raiseSalary(double per) {
this.salary = salary + salary * per;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
public class TestEmployee {//测试类
public static void main(String[] args) {
Employee e1 = new Employee("张三", "技术开发部", 5000);
Employee e2 = new Employee("赵四", "后勤服务部", 3800);
Employee e3 = new Employee("王五", "产品营销部", 6800);
System.out.println(e1 + "\n" + e2 + "\n" + e3);
double per = 0.07;
e1.raiseSalary(per);
e2.raiseSalary(per);
e3.raiseSalary(per);
System.out.println("==============加薪7%===============");
System.out.println(e1 + "\n" + e2 + "\n" + e3);
}
}
输出
姓名:张三 部门:技术开发部 工资:5000.0
姓名:赵四 部门:后勤服务部 工资:3800.0
姓名:王五 部门:产品营销部 工资:6800.0
==============加薪7%===============
姓名:张三 部门:技术开发部 工资:5350.0
姓名:赵四 部门:后勤服务部 工资:4066.0
姓名:王五 部门:产品营销部 工资:7276.0
代码一:
private Double weeklySalary;
代码二:
public void setWeeklySalary(Double weeklySalary){
this.weeklySalary=weeklySalary;
}
代码三:
private Double salary;
private Integer piece;
public PieceWorker (String name,Integer p,Double s) {
super(name);
setSalary(s);
setPiece(p);
}
public void setPiece(Integer p){
this.piece=p;
}
public void setSalary(Double s){
this.salary=s;
}
public double earnings() {
return piece*salary;
}
代码四:
private Double salary;
private Integer hour;
public PieceWorker (String name,Integer h,Double s) {
super(name);
setSalary(s);
setHour(h);
}
public void setHour(Integer h){
this.hour=h;
}
public void setSalary(Double s){
this.salary=s;
}
public double earnings() {
return h*salary;
}
工资管理系统主要功能和特点:
1 操作简单方便
采用国际流行的下拉菜单,全WINDOWS操作形式,界面新颖,全面支持鼠标操作,功能齐全。
2 任意设定工资项目
系统除姓名、人员编号、部门编号、部门名称、应发工资、实发工资、纳税等内定项目不能删除(长度可以改变)外,其它项目(如各种补贴及各种扣款)均可由用户自行定义(包括增加、删除、修改长度及小数位等),工资项目总数可多达255项。当修改或删除工资项目时,自动检查工资计算中与该项目有关的计算公式,并作相应处理。
3 任意定义工资计算公式
用户可将某些工资项目(如加班费、应发工资、纳税等)定义为计算项目,然后定义其计算公式及使用条件,可满足各种复杂的计算。
如: 项目 公式 条件
每日工资 基本工资/26
月加班费 加班天数*每日工资 类别=
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流