数据库——jdbc+jta (基础)

本文介绍了使用Java JDBC技术连接MySQL数据库并读取数据的基本步骤,包括导入驱动、建立连接、执行SQL查询及关闭资源等关键操作。同时,通过示例代码展示了如何在JSP页面中利用JDBC进行数据库交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据库,jdbc验证--mysql 需要导入驱动jar包

import java.sql.*;

public class Mysql {

//public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
public static final String DBDRIVER = "com.mysql.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/jpademo"; //把SC换成你要连接的数据库的名字
public static final String DBUSER = "root";   //user_name代表数据库用户的名称
public static final String DBPASS = "chx0501";   //user_password代表你对连接用户设置的密码
public static void main(String[] args) {
Connection conn = null;
try {
Class.forName(DBDRIVER);         //把驱动程序加载到jvm中去
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);   //取得数据库的一个连接对象
if(conn!=null){
System.out.println("连接成功!");
}

}catch(Exception e){
System.out.println("连接失败!");
}
}
}



数据库,读取——mysql

package com.test.mysql;
import java.sql.*;


public class Mysql_test2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PreparedStatement ps = null;
Connection ct = null;
ResultSet rs = null;
try {
Class.forName("org.gjt.mm.mysql.Driver");         //把驱动程序加载到jvm中去
ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/jpademo", "root", "chx0501");   //取得数据库的一个 连接对象
ps=ct.prepareStatement("select *from employee");
rs=ps.executeQuery();

while(rs.next())
{
System.out.println(rs.getString(1) + " "
+rs.getString(2)+ " " +
rs.getString(3)+ " "+
rs.getString(4)+ " ");
}
// if(ct!=null){
// System.out.println("连接成功!");
// }

} catch (Exception e) {
// TODO: handle exception
// System.out.println("连接失败!");
e.printStackTrace();
}finally{
//关闭
try {
if(rs!=null) rs.close();
if(ps!=null) ps.close();
if(ct!=null) ct.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}


JDBC和JTA

数据源配置

JNDI名以及数据源的配置需要正确,否则容易读取不到

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<datasources>
    <local-tx-datasource>
        <jndi-name>firstds</jndi-name>
        <rar-name>jboss-local-jdbc.rar</rar-name>
        <use-java-context>true</use-java-context>
        <connection-definition>javax.sql.DataSource</connection-definition>
        <jmx-invoker-name>jboss:service=invoker,type=jrmp</jmx-invoker-name>
        <min-pool-size>1</min-pool-size>
        <max-pool-size>20</max-pool-size>

        <blocking-timeout-millis>30000</blocking-timeout-millis>
        <idle-timeout-minutes>30</idle-timeout-minutes>
        <prefill>false</prefill>
        <background-validation>false</background-validation>
        <background-validation-millis>0</background-validation-millis>
        <validate-on-match>true</validate-on-match>
        <statistics-formatter>org.jboss.resource.statistic.pool.JBossDefaultSubPoolStatisticFormatter</statistics-formatter>
        <isSameRM-override-value>false</isSameRM-override-value>
        <allocation-retry>0</allocation-retry>
        <allocation-retry-wait-millis>5000</allocation-retry-wait-millis>
        <security-domain xsi:type="securityMetaData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
        <metadata/>
        <local-transaction/>
        <user-name>root</user-name>
        <password>chx0501</password>

        <prepared-statement-cache-size>0</prepared-statement-cache-size>
        <share-prepared-statements>false</share-prepared-statements>
        <set-tx-query-timeout>false</set-tx-query-timeout>
        <query-timeout>0</query-timeout>
        <use-try-lock>0</use-try-lock>
        <driver-class>com.mysql.jdbc.Driver</driver-class>
        <connection-url>jdbc:mysql://localhost:3306/jpademo</connection-url>

    </local-tx-datasource>
</datasources>


验证页面——jsp

需要首先导入jar包

<%@ page language="java" import="java.sql.*,javax.naming.*,javax.sql.DataSource" pageEncoding="UTF-8"%>

页面代码:

<body>
<%
Context ctx = new InitialContext();

DataSource ds = (DataSource)ctx.lookup("java:/firstds");

Connection conn = ds.getConnection();

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery("select *from employee");

while(rs.next())
{
out.println(rs.getString(1) + "&nbsp;&nbsp;"
+rs.getString(2)+ "&nbsp;&nbsp;" +
rs.getString(3)+ "&nbsp;&nbsp;"+
rs.getString(4)+ "&nbsp;&nbsp;");
}
rs.close();
stmt.close();
conn.close();

%>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值