java mysql 操作_java mysql 简单操作

package javatest;

import java.sql.*;

public class DBconn

{

// 驱动程序名

final String driver = "com.mysql.jdbc.Driver";

// URL指向要访问的数据库名test

final String url = "jdbc:mysql://127.0.0.1:3306/udp";

// MySQL配置时的用户名

final String user = "root";

// MySQL配置时的密码

final String password = "root";

public Connection conn =null;

public Statement statement=null;

public DBconn()

{

try {

// 加载驱动程序

Class.forName(driver);

// 连续数据库

conn = DriverManager.getConnection(url, user, password);

statement = conn.createStatement();

// if(!conn.isClosed())

// System.out.println("Succeeded connecting to the Database!");

} catch(Exception e) {

}

}

public void DBcolse()

{

try

{

this.conn.close();

} catch(Exception e)

{

e.printStackTrace();

System.out.println("数据库关闭失败!");

}

}

}

DBconn db1=new DBconn();

ResultSet rs =null;

try{

System.out.println("用户请求注册");

out.println("please input name");

user_name =in.readLine().trim();

out.println("please input password");

user_password =in.readLine().trim();

}catch (Exception e)

{

System.out.println(e);

}

try

{

String sql= "select * from user where name='"+user_name+"'";

rs = db1.statement.executeQuery(sql);

if(rs.next())

{

System.out.println(user_name+"已经注册");

out.println(user_name+"已经注册");

}

else

{

String sql_insert= "insert into user(name,password) values( '"+ user_name+"' , '"+user_password+"' )";

System.out.println( db1.statement.executeUpdate(sql_insert));

System.out.println("注册成功,成功添加了"+user_name+user_password);

out.println("注册成功,成功添加了"+user_name+user_password);

}

}

catch (Exception e)

{

System.out.println(e);

}

更新数据

String sql= "update unit set "+field_id+" ='"+value+"' where device_id="+device_id+" and index_in_device="+unit_id+"";

out.println(sql);

DBconn db1=new DBconn();

int rs;

rs = db1.statement.executeUpdate(sql);

System.out.println("updata number="+rs);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值