在项目lib中加入mysql的jar包,然后写jdbc链接信息即可,如下提供多种链接方式希望对你有帮助
JDBC连接MySQL加载及注册JDBC驱动程序
ClassforName("commysqljdbcDriver");
ClassforName("commysqljdbcDriver")newInstance();
JDBC URL 定义驱动程序与数据源之间的连接
标准语法:
<protocol(主要通讯协议)>:<subprotocol(次要通讯协议,即驱动程序名称)>:<data source identifier(数据源)>
MySQL的JDBC URL格式:
jdbc:mysql//[hostname][:port]/[dbname][param1=value1][¶m2=value2]…
示例:jdbc:mysql://localhost:3306/sample_dbuser=root&password=your_password
常见参数:
user 用户名
password 密码
autoReconnect 联机失败,是否重新联机(true/false)
maxReconnect 尝试重新联机次数
initialTimeout 尝试重新联机间隔
maxRows 传回最大行数
useUnicode 是否使用Unicode字体编码(true/false)
characterEncoding 何种编码(GB2312/UTF-8/…)
relaxAutocommit 是否自动提交(true/false)
capitalizeTypeNames 数据定义的名称以大写表示
建立连接对象
String url="jdbc:mysql://localhost:3306/sample_dbuser=root&password=your_password";
Connection con = DriverManagergetConnection(url);
建立SQL陈述式对象(Statement Object)
Statement stmt = concreateStatement();
执行SQL语句
executeQuery()
String query = "select from test";
ResultSet rs=stmtexecuteQuery(query);
结果集ResultSet
while(rsnext())
{rsgetString(1);rsgetInt(2);}
executeUpdate()
String upd="insert into test (id,name) values(1001,xuzhaori)";
int con=stmtexecuteUpdate(upd);
execute()
示例:
try
{
}
catch(SQLException sqle)
{
}
finally
{
}
Java类型和SQL类型 技术手册P421
PreparedStatement(预编语句)
PreparedStatement stmt = connprepareStatement("insert into test(id,name)values(,)");
stmtsetInt(1,id);
stmtsetString(2,name);
注:一旦设定语句的参数值后,就可以多次执行改语句,直到调用clearParameters()方法将他清除为止
CallableStatement(预储程序)技术手册P430
JDBC20使用
ResultSet对象中的光标上下自由移动
Statement stmt = concreateStatement (ResultSetTYPE_SCROLL_SENSITIVE, ResultSetCONCUR_READ_ONLY);
ResultSet rs=stmtexecuteQuery("select from test");
public Statement createStatement(int resultSetType,int resultSetConcuttency) throws SQLException
resultSetType
TYPE_FORWARD_ONLY 只能使用next()方法。
TYPE_SCROLL_SENSITIVE 可以上下移动,可以取得改变后的值。
TYPE_SCROLL_INSENSITIVE 可以上下移动。
resultSetConcuttency
CONCUR_READ_ONLY 只读
CONCUR_UPDATABLE ResultSet对象可以执行数据库的新增、修改、和移除
直接使用ResultSet对象执行更新数据
新增数据
Statement stmt=concreateStatement(ResultSetTYPE_SCROLL_SENSITIVE,ResultSetCONCUR_PUDATABLE);
ResultSet uprs=stmtexecuteQuery("select from test");
uprsmoveToInsertRow();
uprsupdateInt(1,1001);
uprsupdateString(2,"许召日");
uprsinsertRow;
更新数据
Statement stmt=concreateStatement(ResultSetTYPE_SCROLL_SENSITIVE,ResultSetCONCUR_PUDATABLE);
ResultSet uprs=stmtexecuteQuery("select from test");
uprslast();
uprsupdateString("name","=">
以上就是关于java在jsp页面如何直接执行sql全部的内容,包括:java在jsp页面如何直接执行sql、如何将JSP页面中的表单信息保存到Mysql数据库、如何用JSP连接安装在Linux上的MySQL等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)