数据库,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) + " "
+rs.getString(2)+ " " +
rs.getString(3)+ " "+
rs.getString(4)+ " ");
}
rs.close();
stmt.close();
conn.close();
%>