数据库java代码实例 java的数据库
java连接数据库的例子
ConnectionManager.java
夷陵网站建设公司创新互联公司,夷陵网站设计制作,有大型网站制作公司丰富经验。已为夷陵上1000+提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的夷陵做网站的公司定做!
import java.sql.*;
public class ConnectionManager {
private static final String DRIVER_CLASS="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String DATABASE_URL="jdbc:sqlserver://localhost:1433;DatabaseName=student";
private static final String DATABASE_USER="xz";
private static final String DATABASE_PASSWORD="test";
private ConnectionManager()
{}
/**
* 返回连接
* @return Connection
* @throws DBAccessException
*/
public static Connection getConnection()
{
Connection dbConnection=null;
try {
Class.forName(DRIVER_CLASS);
dbConnection=DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dbConnection;
}
/**
* 关闭连接
* @param conn
* Connection
*/
public static void closeConnection(Connection conn)
{
try{
if(conn!=null !conn.isClosed())
{
conn.close();
conn=null;
}
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
/**
* 关闭结果集
* @param rs
* ResultSet
*/
public static void closeResultSet(ResultSet rs)
{
try{
if(rs!=null)
{
rs.close();
rs=null;
}
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
/**
* 关闭SQL预处理语句
* @param pstmt
* PreparedStatement
*/
public static void closePreparedStatement(PreparedStatement pstmt)
{
try{
if(pstmt!=null)
{
pstmt.close();
pstmt=null;
}
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
/**
* 关闭SQL语句
* @param stmt
* Statement
*/
public static void closeStatement(Statement stmt)
{
try{
if(stmt!=null)
{
stmt.close();
stmt=null;
}
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
public static void main(String[]args)
{
try {
Connection con=ConnectionManager.getConnection();
if(!con.isClosed())
System.out.println("数据库连接成功。。。。。");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
User.javapublic class User {
private String username;
private String userpass;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpass() {
return userpass;
}
public void setUserpass(String userpass) {
this.userpass = userpass;
}
}
UserDAO.javaimport java.util.*;
import java.sql.*;
public class UserDAO {
public ListUser getAllUser()
{
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
ListUser userList=new ArrayListUser();
String sql="select UserName,UserPas from Users";
try {
conn=ConnectionManager.getConnection();
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next())
{
User u=new User();
u.setUsername(rs.getString("UserName"));
u.setUserpass(rs.getString("UserPas"));
userList.add(u);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
ConnectionManager.closeConnection(conn);
ConnectionManager.closePreparedStatement(pstmt);
ConnectionManager.closeResultSet(rs);
}
return userList;
}
}
java连接数据库具体操作以及代码!最好有个例子 越详细越好?
你要连接的是什么数据库
连接什么数据库,你就要去下载相应的驱动
package com.dao.conn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.entity.PubTermBean;
/**
* ms sql2005连接
* @author Administrator
*
*/
public class ConnByMsSql2005 {
public static Connection getConn(){
Connection conn=null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); //加载驱动类
conn=DriverManager.getConnection("jdbc:sqlserver:127.0.0.1:1433;databasename=数据库名","登录数据库名","密码");
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
//查询数据库表的方法
public ListUs select() {
Connection con = null;
PreparedStatement pst = null;
ResultSet rt = null;
con = ConnByMsSql2005 .getConn();//获得上面的连接数据库的对象
ListUs list = new ArrayListUs();
try {
String sql="select * from users";//查询语句
pst = con.prepareStatement();
rt = pst.executeQuery();//执行查询
while (rt.next()) { //循环查询的结果集
//将数据封装到javabean里
Us u = new Us();
u.setId(rt.getInt(1));
u.setName(rt.getString(2));
u.setPwd(rt.getString(3));
//将每一个javabean对象放到list集合
list.add(u);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库操作对象
try {
if (rt != null) {
rt.close();
}
if (pst != null) {
pst.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return list; //返回得到的数据库数据
}
java连接数据库的代码
package mysql;
import java.sql.*;
/**
* @author xys
*/
public class ConnectMysql {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/databaseName";
String user = "mysqluser";
String password = "password";
String driverClass = "com.mysql.cj.jdbc.Driver";
Connection connection = null;
Class.forName(driverClass);
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
if (connection != null) {
System.out.println("数据库连接成功");
} else {
System.out.println("数据库连接失败");
connection.close();
}
return connection;
}
public void getResult() throws ClassNotFoundException, SQLException {
// 实例化 Statement 对象
Statement statement = getConnection().createStatement();
// 要执行的 Mysql 数据库操作语句(增、删、改、查)
String sql = "";
// 展开结果集数据库
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
// 通过字段检索
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
// 输出数据
System.out.println("ID : " +id);
System.out.println("name :" + name);
}
// 完成后需要依次关闭
resultSet.close();
statement.close();
getConnection().close();
}
}
求一个简单又经典的JAVA数据库连接的例子,要有源代码哦!
我就弄的用户登入的代码吧.这个挺简单的.
这是题目:
用户登陆验证:
1.创建数据库Test,并新建用户表users
字段包含:username varchar(20) not null
userpwd varchar(20) not null
在JBUILDER中编写Long类,实现登陆界面,并在用户输入用户名和密码后,
完成按纽的单击事件,对用户输入的数据进行验证,
(需要严整数据是否为空,密码长度必须是15位),
并实现与数据库的连接,将用户输入的用户名密码与表中的记录比较,
若用户名正确且密码正确,弹出提示框告知登陆成功,否则登陆失败。
这是代码:
//连接数据库
boolean isLogin(String name,String pwd){
boolean flag=false;
Connection conn=null;
PreparedStatement pst=null;
ResultSet rs=null;
//加载驱动
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
//连接数据库
try {
conn=DriverManager.getConnection("jdbc:odbc:login");
String sql="select * from [user] where username=? and userpwd=?";
pst=conn.prepareStatement(sql);
pst.setString(1,name);
pst.setString(2,pwd);
rs=pst.executeQuery();
if(rs.next())
flag=true;
} catch (Exception ex) {
ex.printStackTrace();
}finally{
try {
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
return flag;
}
//验证方法
public void jButton1_actionPerformed(ActionEvent e) {
String name=jTextField1.getText();
String pwd=jTextField2.getText();
//错误处理
if(name.equals("")||pwd.equals(""))
JOptionPane.showMessageDialog(this,"请输入完整的信息");
else {
if(isLogin(name,pwd))
JOptionPane.showMessageDialog(this,"登陆成功");
else
JOptionPane.showMessageDialog(this,"用户名或密码错误");
}
}
}
.....
.....
这是在事件里写的,
分享文章:数据库java代码实例 java的数据库
当前路径:http://ybzwz.com/article/dojcdds.html