大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
对数据库进行增删改查?
创新互联专注于德保网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供德保营销型网站建设,德保网站制作、德保网页设计、德保网站官网定制、成都小程序开发服务,打造德保网络公司原创品牌,更为您提供德保网站排名全网营销落地服务。
以下是 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();
}
}
原文链接随便找的还行网页链接
右键点击项目名依次点击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();
}
}
}
class NewAction implements ActionListener{
String url="jdbc:odbc:s";
String user="sa";
String passwd="";
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb){
Find();
}
else if(e.getSource()==jb1){
Write();
JOptionPane.showMessageDialog(null, "添加成功");
}
else if(e.getSource()==jb3){
Del();
JOptionPane.showMessageDialog(null, "删除成功");
}
else if(e.getSource()==jb4){
Update();
JOptionPane.showMessageDialog(null, "修改成功");
}
else if(e.getSource()==jb5){
Build();
}
}
public void Find(){
boolean f=false;
String lk="select * from std";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
java.sql.Connection con= DriverManager.getConnection(url,user,passwd);
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery(lk);
while(rs.next()){
String p=rs.getString(1);
f=(jf13.getText().trim()).equals(p.trim());
if(f){
jf1.setText(p);
jf1.setEditable(false);
jf2.setText(rs.getString(2));
jf2.setEditable(false);
jf3.setText(rs.getString(3));
jf3.setEditable(false);
jf4.setText(rs.getString(4));
jf4.setEditable(false);
jf5.setText(rs.getString(5));
jf5.setEditable(false);
jf6.setText(rs.getString(6));
jf6.setEditable(false);
jf7.setText(rs.getString(7));
jf7.setEditable(false);
jf8.setText(rs.getString(8));
jf8.setEditable(false);
jf9.setText(rs.getString(9));
jf9.setEditable(false);
jf10.setText(rs.getString(10));
jf10.setEditable(false);
jta.setText(rs.getString(11));
jta.setEditable(false);
jf12.setText(rs.getString(12));
jf12.setEditable(false);
jf13.setText(rs.getString(13));
jf13.setEditable(false);
}
}
if(!f){
JOptionPane.showMessageDialog(null,"此人不存在!");
}
rs.close();
sql.close();
con.close();
}catch(SQLException p){}
catch(Exception d){}
}
public void Write(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
java.sql.Connection con= DriverManager.getConnection(url,user,passwd);
PreparedStatement sql=con.prepareStatement("insert into std values(?,?,?,?,?,?,?,?,?,?,?,?)");
sql.setString(1,jf1.getText());
sql.setString(2, jf2.getText());
sql.setString(3,jf3.getText());
sql.setString(4, jf4.getText());
sql.setString(5, jf5.getText());
sql.setString(6, jf6.getText());
sql.setString(7, jf7.getText());
sql.setString(8, jf8.getText());
sql.setString(9, jf9.getText());
sql.setString(10, jf10.getText());
sql.setString(11, jta.getText());
sql.setString(12, jf12.getText());
sql.executeUpdate();
con.close();
sql.close();
}catch(Exception p){
p.printStackTrace();
}
}
public void Del(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
java.sql.Connection con= DriverManager.getConnection(url,user,passwd);
PreparedStatement sql=con.prepareStatement("delete from std where 姓名=?");
sql.setString(1, jf13.getText());
sql.executeUpdate();
sql.close();
con.close();
}catch(Exception p){
p.printStackTrace();
}
}
public void Update(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
java.sql.Connection con= DriverManager.getConnection(url,user,passwd);
PreparedStatement sql=con.prepareStatement("update std set 姓名=?,班级=?,性别=?,宿舍=?,出生年月=?,移动电话=?,民族=?,电子邮件=?,籍贯=?,备注=?,家庭详细住址=?,邮政编码=? where 姓名=?");
sql.setString(1,jf1.getText());
sql.setString(2, jf2.getText());
sql.setString(3,jf3.getText());
sql.setString(4, jf4.getText());
sql.setString(5, jf5.getText());
sql.setString(6, jf6.getText());
sql.setString(7, jf7.getText());
sql.setString(8, jf8.getText());
sql.setString(9, jf9.getText());
sql.setString(10, jf10.getText());
sql.setString(11, jta.getText());
sql.setString(12, jf12.getText());
sql.setString(13, jf13.getText());
sql.executeUpdate();
con.close();
sql.close();
}catch(Exception p){
p.printStackTrace();
}
}
}这是我以前写的 修改表对应的列项 就好了
首先你得确定你的数据库连接是通过什么形式连接的,hibernate还是原生态的jdbc 还是spring;
如果是只有hibernate,那么你得通过加载配置文件得到sessionFactory,然后得到session
如果spring,那么同样也需要注入sessionfactory到你的dao
如果是jdbc方式,那么你就按照原生态jdbc写法
总之,在你构造DAO时,得有数据源。这样才能操纵你的数据库
如果搞懂了这些问题,那么你的第一个,第三个问题就迎刃而解了。至于第二问题,我没明白你什么意思!
代码如下:
public class Main {
public static void main(String[] args) {
int[] a = new int[]{92, 87, 2, 3, 4, 6, 7, 8, 22, 9, 12, 16, 20, 66, 23};
findNum(a, 6);
findNum(a, 300);
}
private static void findNum(int[] a, int num) {
for (int i = 0; i a.length; i++) {
if (a[i] == num) {
System.out.println("在数组中找到了" + num + ",位于数组的" + i + "位置");
return;
}
}
System.out.println("数组中没有" + num + "这个数字");
}
}
运行结果:
ResultSet rs=s.executeQuery("select * from teacher"); //查询语句
s.executeUpdate("update into teacher set ....."); //插入,更新 ,删除语句
/**
*插入,更新 ,删除
*主要就是SQL语句不同
*/