对数据库进行增删改查?
创新互联专注于企业营销型网站建设、网站重做改版、乐亭网站定制设计、自适应品牌网站建设、H5开发、电子商务商城网站建设、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为乐亭等各大城市提供网站开发制作服务。
以下是 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();
}
}
JAVA计算工人工资,参考例子如下:
import java.util.Scanner;
public class Demo00 {
//定义一个三维数组,用于记录每个部门、分支、绩效工资
private static final float[][][] SALARY_OF_PER_HOUR = {
{{10.75f,12.50f,14.50f},{11.75f,14.50f,17.50f}},
{{13.00f,16.00f,18.50f},{15.00f,18.50f,22.00f}},
{{16.75f,18.50f,20.50f},{19.25f,25.00f,30.00f}}
};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//输入姓名
System.out.println("请输入姓名:");
String name = sc.nextLine();
//输入部门并验证
System.out.println("请输入部门: A,B,C");
char dept = sc.nextLine().charAt(0);
if(dept'A'||dept'C')
{
System.out.println("输入有误,系统将退出");
System.exit(0);
}
//输入分支机构并验证
System.out.println("请输入分支机构: 1,2");
char div = sc.nextLine().charAt(0);
if(div'1'||div'2')
{
System.out.println("输入有误,系统将退出");
System.exit(0);
}
//输入薪绩表并验证
System.out.println("请输入薪绩表: a,b,c");
char sal = sc.nextLine().charAt(0);
if(sal'a'||sal'c')
{
System.out.println("输入有误,系统将退出");
System.exit(0);
}
//输入小时数
System.out.println("请输入本周工作时间(整小时数):");
int hours = sc.nextInt();
float salary = 0;
//每个小时的薪水
float salaryPerHour = SALARY_OF_PER_HOUR[dept-'A'][div-'1'][sal-'a'];
//分别计算40小时内和超过40小时的薪水
if(hours=40)
{
salary += salaryPerHour*hours;
}
else
{
salary += salaryPerHour*hours+(hours-40)*1.5*salaryPerHour;
}
//输出结果
System.out.println("姓名:\t"+name+"\n部门:\t"+dept+"\n分支机构:\t"+div
+"\n薪绩表:\t"+sal+"\n工作时间:\t"+hours+"\n薪水:\t"+salary);
}
}
//Best wishes!
JAVA计算工人工资,参考例子如下:
import java.util.Scanner;
public class Demo00 {
//定义一个三维数组,用于记录每个部门、分支、绩效工资
private static final float[][][] SALARY_OF_PER_HOUR = {
{{10.75f,12.50f,14.50f},{11.75f,14.50f,17.50f}},
{{13.00f,16.00f,18.50f},{15.00f,18.50f,22.00f}},
{{16.75f,18.50f,20.50f},{19.25f,25.00f,30.00f}}
};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//输入姓名
System.out.println("请输入姓名:");
String name = sc.nextLine();
//输入部门并验证
System.out.println("请输入部门: A,B,C");
char dept = sc.nextLine().charAt(0);
if(dept'A'||dept'C')
{
System.out.println("输入有误,系统将退出");
System.exit(0);
}
//输入分支机构并验证
System.out.println("请输入分支机构: 1,2");
char div = sc.nextLine().charAt(0);
if(div'1'||div'2')
{
System.out.println("输入有误,系统将退出");
System.exit(0);
}
//输入薪绩表并验证
System.out.println("请输入薪绩表: a,b,c");
char sal = sc.nextLine().charAt(0);
if(sal'a'||sal'c')
{
System.out.println("输入有误,系统将退出");
System.exit(0);
}
//输入小时数
System.out.println("请输入本周工作时间(整小时数):");
int hours = sc.nextInt();
float salary = 0;
//每个小时的薪水
float salaryPerHour = SALARY_OF_PER_HOUR[dept-'A'][div-'1'][sal-'a'];
//分别计算40小时内和超过40小时的薪水
if(hours=40)
{
salary += salaryPerHour*hours;
}
else
{
salary += salaryPerHour*hours+(hours-40)*1.5*salaryPerHour;
}
//输出结果
System.out.println("姓名:\t"+name+"\n部门:\t"+dept+"\n分支机构:\t"+div
+"\n薪绩表:\t"+sal+"\n工作时间:\t"+hours+"\n薪水:\t"+salary);
}
}
//Best wishes!
按照题目要求编写的计算员工税后工资的Java程序如下
import java.util.Scanner;
public class Tax{
public static double calculateSalary(double s){
double sat=0;
if(s=5000){
sat=s;
}else if(5000s s=6500){
sat=s-(s-5000)*0.03;
}else if(6500s s=9500){
sat=s-(s-5000)*0.1;
}else if(9500s s=14000){
sat=s-(s-5000)*0.2;
}else if(14000s s=40000){
sat=s-(s-5000)*0.25;
}else if(40000s s=60000){
sat=s-(s-5000)*0.3;
}else if(60000s s=85000){
sat=s-(s-5000)*0.35;
}
return sat;
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=0;
double s,sat;
double[] salary=new double[10];
double[] salaryAfterTax=new double[10];
System.out.print("是否添加新员工薪水 y/n?");
String add=sc.next();
while(add.equals("y")){
s=sc.nextDouble();
salary[n]=s;
sat=calculateSalary(s);
salaryAfterTax[n]=sat;
n++;
System.out.print("是否添加新员工薪水 y/n?");
add=sc.next();
}
for(int i=0;in-1;i++){
for(int j=0;jn-1-i;j++){
if(salaryAfterTax[j]salaryAfterTax[j+1]){
double temp=salaryAfterTax[j];
salaryAfterTax[j]=salaryAfterTax[j+1];
salaryAfterTax[j+1]=temp;
}
}
}
System.out.printf("税后工资高的前三名是:%.2f,%.2f,%.2f",salaryAfterTax[0],salaryAfterTax[1],salaryAfterTax[2]);
}
}