mysql数据库__Jdbc直接操作__Statement__insert

一、代码如下

package www.tanniu.jdbc;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Statement {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Statement s= new Statement();
		s.lianJieMysql();

	}

	private void lianJieMysql() {
		// TODO Auto-generated method stub
		java.sql.Statement sm= null;
		java.sql.Connection cn = null;
		ResultSet rs = null;

		try {
			//加载驱动程序
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			//得到连接
			cn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/learn?user=root&password=&useUnicode=true&characterEncoding=UTF8");
			sm= cn.createStatement();

			int i= sm.executeUpdate("insert into user(username, password, phone, email) values('332', '332', '33', '322');");
			System.out.println(i);
			if(i == 1) {
				System.out.println("新增:成功");
			}else {
				System.out.println("新增:失败");
			}
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(sm != null) {sm.close();}
				if(cn != null) {cn.close();}} catch (SQLException e) {e.printStackTrace();}
		}
	}

}

 

posted @ 2017-10-08 12:18  18513757531  阅读(276)  评论(0编辑  收藏  举报