简单增删改查java代码,javaweb实现简单的增删改查
求一段简单的JAVA代码 要求员工工资管理系统的一个增删改查代码,谢谢了
对数据库进行增删改查?
网站建设哪家好,找创新互联建站!专注于网页设计、网站建设、微信开发、微信平台小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了峨山县免费建站欢迎大家使用!
以下是 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的增删改查代码
ResultSet rs=s.executeQuery("select * from teacher"); //查询语句
s.executeUpdate("update into teacher set ....."); //插入,更新 ,删除语句
/**
*插入,更新 ,删除
*主要就是SQL语句不同
*/
java连接sql数据库。实现增删改查怎么写。谁有实例
原文链接随便找的还行网页链接
右键点击项目名依次点击new–Directory 创建文件夹lib
2.把mysql-connector-java-5.1.48-bin.jar包粘贴到lib目录中
3.把数据库连接jar包导入到项目中
JDBC步骤:
加载数据库的驱动,它是java和数据库之间的桥梁
2.获取Connection,java和数据库的一次连接
3.创建Statement对象,由Connection产生,执行sql语句
4.如果要接收返回值,创建ResultSet对象,保存Statement执行后所查到的结果
增删改代码:
package cn.web.jdbc;
import java.sql.*;
public class executeUpdate {
public static void main(String[] args) {
// 加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
// 获取连接
String url = "jdbc:mysql://localhost:3306/usejdbc?useUnicode=truecharacterEncoding=UTF-8useSSL=false";
String user = "root";
String password = "123456";
try {
// 连接对象输入三个参数
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
//定义sql语句
// 增
String sql1 = "insert into student(username,password) values ('诸葛亮','111111')";
// 删
String sql2 = "delete from student where username ='诸葛亮'";
// 改
String sql3 = "update student set username='老八' where id = 1 ";
Statement statement = connection.createStatement();
// 修改这里的sql即可
int count = statement.executeUpdate(sql1);
System.out.println(count);
// ----------------------------------------------------------------
// 释放资源
statement.close();
connection.close();
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
对数据库实现增删改查的Java程序
增删改查都有链接网页链接
粘贴着查询的:
package cn.web.jdbc;
import java.sql.*;
public class UserLogin {
public static void main(String[] args) {
// 加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
// 获取连接
String url = "jdbc:mysql://localhost:3306/usejdbc?useUnicode=truecharacterEncoding=UTF-8useSSL=false";
String user = "root";
String mysqlPassword = "123456";
//模拟前台传入的用户名和密码
String InputUsername = "老八";
String InputPassword = "123456";
try {
// 连接对象输入三个参数
Connection connection = DriverManager.getConnection(url, user, mysqlPassword);
System.out.println(connection);
//定义sql语句
// 查询
String sql1 = "select * from student where username='" + InputUsername + "' and password='" + InputPassword + "'";
System.out.println(sql1);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql1);
System.out.println(resultSet);
if (resultSet.next()) {
System.out.println("登录成功");
} else {
System.out.println("登录失败");
}
// 释放资源
statement.close();
connection.close();
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
新闻标题:简单增删改查java代码,javaweb实现简单的增删改查
本文来源:http://azwzsj.com/article/hcedsc.html