JAVA 操作远程mysql数据库实现单表增删改查操作
2024-09-29 18:10:23
package MysqlTest; import java.sql.DriverManager;
import java.sql.ResultSet; import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement; public class MysqlTest02 {
public static void main(String[] args) {
//连接url
String url = "jdbc:mysql://10.1.1.136:3306/JAVADB";
//连接driver
String driver = "com.mysql.jdbc.Driver";
//用户名
String name = "root";
//密码
String pwd = "root";
try{
Class.forName(driver);
Connection connection = (Connection) DriverManager.getConnection(url, name, pwd);
System.out.println("connection success"); //增添
// String sql ="insert into TEST(name,func) values(?,?)";
// PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
// statement.setObject(1,"高文斌");
// statement.setObject(2,"准备找工作");
// int result = statement.executeUpdate();
// if (result == 1){
// System.out.println("插入成功");
// }
// connection.commit(); //查询
// String sql1 = "select * from TEST WHERE ID > ?";
// PreparedStatement sta1 = (PreparedStatement) connection.prepareStatement(sql1);
// sta1.setInt(1, 1);
// ResultSet re = sta1.executeQuery();
// while (re.next()){
// String names = re.getString(2);
// String func = re.getString(3);
// System.out.println(names+"\t:"+func);
// }
// System.out.println("查询完毕"); // //删除
// String ddl = "delete from TEST where name = '高文斌'";
// Statement sta = (Statement) connection.createStatement();
// int eff = sta.executeUpdate(ddl);
// System.out.println(eff); //更改
Statement s = (Statement) connection.createStatement();
int b = s.executeUpdate("update TEST set name='周文王' where name='李四'");
System.out.println(b);
}catch (Exception e) {
System.out.println(e);
}
}
}
准备工作:
1.下载mysql-connector-java-5.1.45.zip包到本地
2.配置好远程数据库配置文件,我的是在虚拟机上模仿的,文件位置在/etc/mysql/mysql.conf.d/mysqld.cnf,找到bind-address = 127.0.0.1注释掉,允许远程访问
最新文章
- python中单引号, 双引号,三引号的差异
- NodeJS入门(四)—— path对象
- Solr嵌套子文档的弊端以及一种替代方式
- Windows下 VM12虚拟机安装OS X 10.11 和VM TOOLS
- win7下IIS配置以及域名映射方法
- linux之iptables
- 使用Jsoup 抓取页面的数据
- Linux C 创建目录函数mkdir相关(转-清新居士)
- POJ C程序设计进阶 编程题#1:单词翻转
- ECshop 在迁移到 PHP7 时遇到的兼容性问题
- Css3案例
- HDU 1248 寒冰王座(全然背包:入门题)
- java MD5加密
- CI 模板解析器类
- 使用Webbrowser的一点心得体会
- 使用keepalived使用主备热备份功能
- vue2.0 子组件和父组件之间的传值(转载)
- CASE WHEN 及 SELECT CASE WHEN的用法
- day14 python各种推导式详解
- 011-MAC 设置环境变量path的几种方法