Java程序使用使用execute update建议新的数据表_微信
日期:2014-07-13 / 人气: / 来源:网络
一、创建
mysql.ini文件,配置如下
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/select_test
user=root
pass=123456
这样以后修改数据库的配置直接在mysql.ini文件中修改。
二、编写代码
initParam方法: 获得mysql.ini中的数据
createTale方法: 连接数据库,并且executeUpdate执行sql语句。此例的sql文件为创建表语句。
main方法: 传入Sql语句。
class ExecuteDDL {
private String driver;
private String url;
private String user;
private String pass;
Connection conn;
Statement stmt;
public void initParam(String paramFile) throws Exception {
Properties props = new Properties();
props.load(new FileInputStream(paramFile));
driver = props.getProperty("driver");
url = props.getProperty("url");
user = props.getProperty("user");
pass = props.getProperty("pass");
}
public void createTale(String sql) throws Exception{
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,user,pass);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
}
finally
{
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ExecuteDDL ed = new ExecuteDDL();
ed.initParam("src/mysql.ini");
ed.createTale("create table student "
"(id int, "
"name varchar(50), "
"num varchar(20) )");
System.out.println("Creating table success!");
}
注意事项:传入的Sql语句最好在MySql测试通过,并且传入的mysql.int文件的路径必须正确。
当执行完毕后,在MySql的select_test数据库中查看该Student表是否已经创建成功了。
三、使用executeUpdate方法,向表中插入数据。
将上面的创建表的Sql语句改为插入数据表的语句,执行executeUpdate方法,其结果就是想表中插入数据。
创建insertSql变量。
private static String insertSql = "insert into student values(1,'XiaoMing','06108787')";
执行插入语句。
ed.createTale(insertSql);
其它代码都是一样的。
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/select_test
user=root
pass=123456
这样以后修改数据库的配置直接在mysql.ini文件中修改。
二、编写代码
initParam方法: 获得mysql.ini中的数据
createTale方法: 连接数据库,并且executeUpdate执行sql语句。此例的sql文件为创建表语句。
main方法: 传入Sql语句。
class ExecuteDDL {
private String driver;
private String url;
private String user;
private String pass;
Connection conn;
Statement stmt;
public void initParam(String paramFile) throws Exception {
Properties props = new Properties();
props.load(new FileInputStream(paramFile));
driver = props.getProperty("driver");
url = props.getProperty("url");
user = props.getProperty("user");
pass = props.getProperty("pass");
}
public void createTale(String sql) throws Exception{
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,user,pass);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
}
finally
{
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ExecuteDDL ed = new ExecuteDDL();
ed.initParam("src/mysql.ini");
ed.createTale("create table student "
"(id int, "
"name varchar(50), "
"num varchar(20) )");
System.out.println("Creating table success!");
}
注意事项:传入的Sql语句最好在MySql测试通过,并且传入的mysql.int文件的路径必须正确。
当执行完毕后,在MySql的select_test数据库中查看该Student表是否已经创建成功了。
三、使用executeUpdate方法,向表中插入数据。
将上面的创建表的Sql语句改为插入数据表的语句,执行executeUpdate方法,其结果就是想表中插入数据。
创建insertSql变量。
private static String insertSql = "insert into student values(1,'XiaoMing','06108787')";
执行插入语句。
ed.createTale(insertSql);
其它代码都是一样的。
解决Eclipse连接MySQL乱码问题以及MySQL[error 1366 (hy000):错
用MS SQL和oracle9 太吃内存,有点大,全给卸载了.安装了mysql占内存小,方便使用!version:mysql-essential-5.1.36在 MySQL Command Line Client显示中文一切正常;在eclipse中新工程,连接
MySQL,mysql乱码
作者:管理员
推荐内容 Recommended
- 江苏飞浩信息科技期待您的加入07-20
- 江苏飞浩科技欢迎您07-19
相关内容 Related
- 江苏飞浩信息科技期待您的加入07-20
- 江苏飞浩科技欢迎您07-19