
try{
Class.forName("com.mysql.jdbc.Driver")
}
catch(ClassNotFoundException e) {}
Connection con=null//连接对象
Statement sql=null//Statement对象(SQL语句)
ResultSet rs=null//结果集对象
//进行数据源的连接
try{
con=DriverManager.getConnection ("jdbc:mysql://localhost/scutcs","","")//连接数据库的url 用户名和密码
sql=con.createStatement()
String to="Select * From user1 Where username='"+username+"'"
rs=sql.executeQuery(to) //根据所定义的Statement执行生成相应的结果集并存在RS中
if(rs.next()) //判断结果集是否为空,如果不为空则表示有记录
{
out.print("<script>alert('用户名 "+xm+"已存在,请另选一个!')history.back()</script>")//如果存在返回注册页面
}
else {如果不存在就向数据库添加一条记录}
}
catch (SQLException e)
{ out.print(e)
}
2.
username=root
password=
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jsddb?useUnicode=true&characterEncoding=UTF8
maxActive=10
initialSize=1
maxWait=5000
首先你得有一个jdbc的架包
然后
import java.sql.Connectionimport java.sql.DriverManager
import java.sql.PreparedStatement
import java.sql.SQLException
public class Util {
public static Connection getConnection(){
Connection con = null
String user = "root"//数据库登陆用户名
String password = "root"//数据库登陆密码
String url = "jdbc:mysql://localhost:3306/password"//数据库库名
String driver = "com.mysql.jdbc.Driver"
try{
Class.forName(driver)
con = DriverManager.getConnection(url,user,password)
}catch(Exception e){
}
return con
}
public static void main(String[] args) {
Connection con = getConnection()
String sql = "insert into stu values(null,\'hello\')"
PreparedStatement p = null
try{
p = con.prepareStatement(sql)
p.executeUpdate()
}catch(SQLException e){
e.printStackTrace()
}finally{
try{
p.close()
con.close()
}catch(SQLException e){
e.printStackTrace()
}
}
}
}
你可以用数据库集群来做。用jta控制2个数据源的事务。具体实现要靠自己捉摸了。不是那么容易的事情。而且在这里问,不会得到你满意的答案的。只能提供你能用得到的技术的名字,给个方向。百度知道里面 不会有人手把手教你做的
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)