使用JDBC添加带参数的数据
2007-12-22 16:09
190 查看
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestDML2 ...{
public static void main(String[] args) ...{
if(args.length != 3) ...{
System.out.println("Parameter Error! Please Input Again!");
System.exit(-1);
}
int userId = 0;
try ...{
userId = Integer.parseInt(args[0]);
} catch (NumberFormatException e) ...{
System.out.println("Parameter Error! Deptid should be Number Format!");
System.exit(-1);
}
String userName = args[1];
String userAddress = args[2];
Statement stmt = null;
Connection conn = null;
try ...{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/data?user=root&password=123456";
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
String sql = "insert into dept2 values (" + userId + ",'" + userName + "','" + userAddress + "')";
System.out.println(sql);
stmt.executeUpdate(sql);
} catch (ClassNotFoundException e) ...{
e.printStackTrace();
} catch (SQLException e) ...{
e.printStackTrace();
} finally ...{
try ...{
if(stmt != null) ...{
stmt.close();
stmt = null;
}
if(conn != null) ...{
conn.close();
conn = null;
}
} catch (SQLException e) ...{
e.printStackTrace();
}
}
}
}
相关文章推荐
- oracle中,使用存储过程带数组参数的形式向表中添加数据(存储过程,数组参数,序列,触发器,类型,循环)
- 针对使用Myeclipse进行JDBC链接MySQL时添加中文数据出现?的解决方法!
- java 演示使用jdbc-odbc桥连接的方式操作数据库 向数据库中添加数据
- 使用java(jdbc)向mysql中添加数据时出现“unknown column……”错误
- 解决使用AJax往DropDownList中动态添加数据时发生,回发或回调参数无效 的问题
- jplayer中动态添加列表曲目(js提取request中的list数据作为js参数使用)
- 数据库数据添加到TextView详解(limite参数如何使用)
- 【PB】使用参数指定的SQL语句向指定的下拉列表框中添加数据
- ADO.NET使用带参数方法实现添加、删除和修改数据
- jdbc使用 Statement 接口实现添加数据操作小例子
- 在T-SQL中使用参数将XML数据添加到SQL数据库的XML列
- Linq2Sql数据实体外部更新时“不能添加其键已在使用中的实体”的解决办法
- 使用 SQL Server 2008 数据类型-xml 字段类型参数进行数据的批量选取或删除数据
- 使用js获取url中的参数并添加到新的url中
- 使用JDBC在MySQL数据库中快速批量插入数据
- db2 import数据导入的skipcount参数使用方法
- 使用OleDb写入Excel异常,"字段太小而不能接受所要添加的数据的数量" "the field is too small to accept the amount of data "
- 小结一下c#WinForm调用sql2000,将老表数据通过与新表数据比较,不同的添加进去,并使用进度条显示进度的教训与经验
- Hibernate查询数据的几种方式以及HQL用法及参数的几种使用方法
- shell中使用curl时,数据为乱码的解决方法,以及一些参数的解释