标签:maven tis 用户名 import public 连接数 driver 数据库名 数据库
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));
}
}
}
标签:maven tis 用户名 import public 连接数 driver 数据库名 数据库
原文地址:https://blog.51cto.com/9447803/2424991