Java连接MySql插入数据的代码
package com.chendan.maven;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class MavenTest {
public static void main(String[] args) throws Exception {
String driver = "com.mysql.jdbc.Driver"; // 获取mysql数据库的驱动类
String url = "jdbc:mysql://10.0.0.30:3306/test?&useSSL=false"; // 连接数据库(test是数据库名)
String user = "chendan";// 连接mysql的用户名
String password = "123456";// 连接mysql的密码
String insertsql = "insert into t1 values(?,?)";
String querysql = "select * from t1";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
conn.setAutoCommit(false);
PreparedStatement statement = conn.prepareStatement(insertsql);
statement.setInt(1, 1);
statement.setString(2, "Tom");
statement.executeUpdate();
conn.commit();
ResultSet rst = statement.executeQuery(querysql);
while (rst.next()) {
System.out.println(rst.getInt(1) + "\t" + rst.getString(2));
}
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。