Java mysql insert(Java 数据库数据插入)
插入SQL语法
insert into 表名(字段1) values(?)
insert into 表名(字段1, 字段2) values(?, ?)
insert into 表名(字段1, 字段2, 字段3) values(?, ?, ?)
注意:
有多少个字段,就有多少个'?' , 它们是一一对应的!
例子
创建数据库testpgdb
创建用户表
CREATE TABLE IF NOT EXISTS pangugle_user (
user_id int(11) UNSIGNED NOT NULL AUTO_INCREMENT ,
user_name varchar(20) NOT NULL ,
user_password char(32) COLLATE utf8_bin NOT NULL ,
PRIMARY KEY (user_id),
UNIQUE INDEX pangugle_user_name(user_name)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci;
表里有三个字段, user_id为自增字段,并且为主键, user_name为用户名, user_password为密码 最后 UNIQUE INDEX pangugle_user_name(user_name) 表示为 user_name添加了索引,并且加了一个唯一约束!
代码如下:
package com.pangugle.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class TestJDBC
{
public static Connection getConnect() throws ClassNotFoundException, SQLException
{
// 通过的反射的方式,导入数据库连接驱动
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://192.168.3.201:3306/testpgdb??useUnicode=true&characterEncoding=utf8&jdbcCompliantTruncation=false&useSSL=false&tinyInt1isBit=false";
Connection conn = DriverManager.getConnection(url, "root", "root");
System.out.println("connect mysql is success ? " + !conn.isClosed());
return conn;
}
public static void testInsert(String username, String password) throws ClassNotFoundException, SQLException
{
Connection conn = getConnect();
PreparedStatement ps = null;
try {
String sql = "insert into pangugle_user(user_name, user_password) values(?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
int rs = ps.executeUpdate();
System.out.println("insert 结果为 = " + rs);
} catch (Exception e) {
e.printStackTrace();
} finally
{
// 一定要关闭资源(当然我们还有一种技术,叫做连接池技术,就不在需要我们来关闭了)
ps.close();
conn.close();
}
}
public static void main(String[] args) throws ClassNotFoundException, SQLException
{
//getConnect();
testInsert("u1", "p1");
}
}
第一次运行结果为
connect mysql is success ? true
insert 结果为 = 1
我们到数据库去查询数据 你会看到以下结果
user_id | user_name | user_passowrd |
---|---|---|
1 | u1 | p1 |
然后我们再运行一次,你会发现,出错了
connect mysql is success ? true
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'u1' for key 'pangugle_user_name'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2484)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013)
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998)
at com.pangugle.test.TestJDBC.testInsert(TestJDBC.java:31)
at com.pangugle.test.TestJDBC.main(TestJDBC.java:47)
从上面报错的信息我们可以看到是
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'u1' for key 'pangugle_user_name'
这句话的意思表 'u1' 这个value 在表中重复添加, 为什么呢?
原因其实是我们在创建表的时候为user_name 添加了一个unique 唯一约束,也就是说在这张表,user_name 的值不能一模一样,就是说表里已经有一个用户u1,就不能再有一个u1, 类似我们的身份证号码一样,也是唯一的!
知道了所有,那么我们做如下改动
public class TestJDBC
{
...... 两个方法的代码,这里就省略了
public static void main(String[] args) {
// getConnect();
// testInsert("u1", "p1");
testInsert("u2", "p2");
testInsert("u3", "p3");
}
}
然后再运行一次,是不是又可以了;
现在表中有三条数据了
user_id | user_name | user_passowrd |
---|---|---|
1 | u1 | p1 |
3 | u2 | p2 |
4 | u3 | p3 |
Perfect !
是不是很简单!