快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

数据库JAVA源代码百度 java操作数据库代码

Java源代码注释

你的这段代码是获取配置文件中数据库配置信息

成都创新互联专业为企业提供鹿寨网站建设、鹿寨做网站、鹿寨网站设计、鹿寨网站制作等企业网站建设、网页设计与制作、鹿寨企业网站模板建站服务,10余年鹿寨做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

配置内容都在DBConfig.txt 这个文件中了

package dda;

import java.sql.*;

import java.io.LineNumberReader;

import java.io.FileReader;

import java.util.StringTokenizer;

/**

*

* 读取配置文件DBConfig.txt中的配置信息

* @author xxx

* @version 1.0

*/

public class DbConnection {

Connection conn = null;

ResultSet rs = null;

boolean b;

public DbConnection() {

this.b = false;

this.getDBConfig();

}

/* 定义变量 */

String strSeparate = " ";

String strFileName = "DBConfig.txt";

String strUserID = null;

String strUserPWD = null;

String strDB = null;

public void getDBConfig() {

this.getDBConfig();

try {

LineNumberReader oLineNumberReader = new LineNumberReader(

new FileReader(this.strFileName)); // 从 DBConfig.txt 文件中读取数据库配置

String strLine = null;

while ((strLine = oLineNumberReader.readLine()) != null) {// 遍历配置文件中每一行数据

StringTokenizer token = new StringTokenizer(strLine,(new String(this.strSeparate).toString())); // 按空格拆分配置内容

/* 以下三行都是给属性设值,获取配置属性 */

this.strDB = token.nextToken();

this.strUserID = token.nextToken();

this.strUserPWD = token.nextToken();

}

} catch (Exception e) {

System.out.println("DB:"+strFileName);

}

}

}

java程序操作MySQL数据库

数据库内容

java源代码 (代码实现的是查询成绩为 的人员信息 至于其他功能的 代码中有注释) 注意 在eclipse里运行程序的时候 要工程里插入jar包 否则运行异常! import java sql *; import java io *; class database_manage {  public Connection conn = null;  public ResultSet rs = null;  private String DatabaseDriver = " mysql jdbc Driver";  // DataSource 数据源名称DSN  private String DatabaseConnStr = "jdbc:mysql://localhost: /people_manage?useUnicode=truecharacterEncoding=utf "  + " root root";  public void setDatabaseDriver(String Driver) { this DatabaseDriver = Driver;  }  public String getDatabaseDriver() { return (this DatabaseDriver);  }  public void setDatabaseConnStr(String ConnStr) { this DatabaseConnStr = ConnStr;  }  public String getDatabaseConnStr() { return (this DatabaseConnStr);  }  public database_manage() {// 构造函数连接数据库 try {  Class forName(DatabaseDriver); } catch (java lang ClassNotFoundException e) {  System err println("加载驱动器有错误:" + e getMessage());  System out print("执行插入有错误:" + e getMessage());// 输出到客户端 }  }  public ResultSet query(String sql) {// 查询数据库 rs = null; try {  conn = DriverManager   getConnection(  "jdbc:mysql://localhost: /people_manage?useUnicode=truecharacterEncoding=utf "  "root" "root");  Statement stmt = conn createStatement();  rs = stmt executeQuery(sql); } catch (SQLException ex) {  System err println("执行查询有错误:" + ex getMessage());  System out print("执行查询有错误:" + ex getMessage()); // 输出到客户端 }

return rs;

}  public int update_database(String sql) {// 更新或插入数据库 int num = ; try {  conn = DriverManager   getConnection(  "jdbc:mysql://localhost: /people_manage?useUnicode=truecharacterEncoding=utf "  "root" "root");  Statement stmt = conn createStatement();  num = stmt executeUpdate(sql); } catch (SQLException ex) {  System err println("执行插入有错误:" + ex getMessage());  System out print("执行插入有错误:" + ex getMessage());// 输出到客户端 }  CloseDataBase(); return num;  }  public void CloseDataBase() {// 关闭数据库 try {  conn close(); } catch (Exception end) {  System err println("执行关闭Connection对象有错误 " + end getMessage());  System out print("执行执行关闭Connection对象有错误 有错误:" + end getMessage()); // 输出到客户端 }  } } class people {  private String uid;  private String name;  private String banji;  private int score;  public people() {  }  public people(String uid String name String banji) { this uid = uid; this name = name; this banji = banji;  }  public people(String uid String name String banji int score) { this uid = uid; this name = name; this banji = banji; this score = score;  }  public String getUid() { return uid;  }  public void setUid(String uid) { this uid = uid;  }  public String getName() { return name;  }  public void setName(String name) { this name = name;  }  public String getBanji() { return banji;  }  public void setBanji(String banji) { this banji = banji;  }  public int getScore() { return score;  }  public void setScore(int score) { this score = score;  } } public class manage {  private people[] people_array ;// 对象数组  public void add_people(String uid String name) {  String sql = "insert people (uid name) values ( " + uid + " " + name + " )";// sql插入语句 // String sql = "insert people (uid name) values ( uid tom )";  database_manage db_obj = new database_manage(); db_obj update_database(sql);  }  public void update_people(String uid String name) {  String sql = "update people set name= " + name + " where uid= " + uid + " "; database_manage db_obj = new database_manage(); db_obj update_database(sql);  }  public void delete_people(String uid) {  String sql = "delete from people  where uid= " + uid + " ";  database_manage db_obj = new database_manage();  db_obj update_database(sql);  }  public people query_people(String uid) { database_manage db_obj = new database_manage(); // String adminid=null; String uid_new name banji; uid_new = null; name = null; banji = null; String sql_query = "select * from people where uid= " + uid + " ";  try {  ResultSet rs = db_obj query(sql_query);  if (rs next()) { uid_new = rs getString("uid"); name = rs getString("name"); banji = rs getString("banji");  } } catch (Exception e) {  e getMessage(); } people new_people = new people(uid_new name banji); return new_people;  }  public people[] query_people_byscore(int score) { database_manage db_obj = new database_manage(); String uid_new name banji; uid_new = null; name = null; banji = null; int score_new = ;

String sql_query = "select * from people where score=" + score;// sql查询语句

try {

ResultSet rs = db_obj query(sql_query);// 查询后 返回结果集

int num = ;

ResultSet rs_new = rs;

while (rs_new next()) {// 统计结果集中学生个数

num++;

}

// System out println(num);

people_array = new people[num];

int i = ;

rs beforeFirst();// 返回结果集的开始

while (rs next()) {

uid_new = rs getString("uid");

name = rs getString("name");

banji = rs getString("banji");

score_new = rs getInt("score");

people_array [i] = new people(uid_new name banji score_new);

i++;

}

} catch (Exception e) {

e getMessage();

}

return people_array ;

}

public static void main(String args[]) {

/*

* people new_people=new people();

*

* manage mr=new manage(); //mr add_people(" " "小明");插入一个学生的信息

*

* new_people=mr query_people(" ");//查询uid= 的学生信息 返回对象 System out

* println(""+new_people getName()+"  "+new_people getBanji());

* mr update_people(" " "小王");更新一个学生的信息

*

* new_people=mr query_people(" ");//更新后查询

*

* System out println(""+new_people getName()+"  "+new_people getBanji(

* ));

*/

manage mr = new manage();

// mr delete_people(" ");删除uid= 的学生信息

people[] people_array;// 声明对象数组

people_array = mr query_people_byscore( );// 返回成绩为一百的学生类数组 后输出

int num = ;

num = people_array length;

for (int i = ; i num; i++) {

System out println(people_array[i] getUid() + " "

+ people_array[i] getName() + "  "

+ people_array[i] getBanji() + "  "

+ people_array[i] getScore());

}

}

}

lishixinzhi/Article/program/Java/JSP/201311/20030

求一个简单又经典的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学生信息管理系统源代码

你要连的数据库是SQL 还是ORACLE

但是代码都查不多

下面的是连接SQL数据库的代码

你需要先创建个数据库,还有表,表的字段是登陆名和密码

下面的"SA" 是登陆名 "111111" 是密码

ORACLE 和这个查不多

import java.sql.*;//做数据库时候必须要引入的包

import com.microsoft.jdbc.sqlserver.SQLServerDriver;

public class DBFactory {

Connection Conn=null;

PreparedStatement Stmt=null;

ResultSet Rs=null;

String driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";

String OracleUserName="sa";

String OracleUserPwd="111111";

String ConnUrl="jdbc:sqlserver://localhost:1433;databaseName=news";

public Connection getConnection()

{

try {

Class.forName(driverName);

} catch (ClassNotFoundException ex) {

System.out.println("加载驱动程序有错误");

}

try {

Conn = DriverManager.getConnection(ConnUrl, OracleUserName,OracleUserPwd);

} catch (SQLException ex1) {

System.out.print("取得连接的时候有错误,请核对用户名和密码");

}

return Conn;

}

这个是连接ORACLE数据库代码

import java.sql.*;

import oracle.jdbc.driver.OracleDriver;

public class DBFactory {

Connection Conn=null;

PreparedStatement Stmt=null;

ResultSet Rs=null;

String driverName="oracle.jdbc.driver.OracleDriver";

String OracleUserName="scott";

String OracleUserPwd="tiger";

String ConnUr1="jdbc:oracle:thin:@locahost:1521:Ora";

public Connection getConnection()

{

try {

Class.forName(driverName);

} catch (ClassNotFoundException ex) {

System.out.println("加载驱动程序有错误");

}

try {

Conn = DriverManager.getConnection(ConnUr1, OracleUserName,OracleUserPwd);

} catch (SQLException ex1) {

System.out.print("取得连接时有错误,请核对用户名和密码");

}

return Conn;

}

希望能追加分数谢谢!

求JAVA源代码

我用了半个小时 帮你写了一个简单的验证用户名和密码登陆问题 别辜负我的好意 下面是代码!(建好包和类 代码粘过去就能用)

实体类 包entity

-------------------------------------------------------------

package entity;

/**

* 用户实体类

* @author new

*

*/

public class Users {

private String name;//用户名

private String pass;//用户密码

/**

* 空的构造函数 用户实力化 此类对象

*/

public Users(){

}

/**

* 构造函数 接收用户名和密码

* @param name

* @param pass

*/

public Users(String name, String pass) {

this.name = name;

this.pass = pass;

}

/**

* 下面set和get方法就不用解释了吧

* @return

*/

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPass() {

return pass;

}

public void setPass(String pass) {

this.pass = pass;

}

}

数据库类 包dao(我是模拟一下数据库 没有用到数据库)

--------------------------------------------------------------

package dao;

import java.util.*;

import entity.Users;//导入实体类

/**

* 模拟数据库 用户DAO

* @author new

*

*/

public class UsersDAO {

private static Users users=new Users();

static

{

users.setName("tom");

users.setPass("jerry");

}

/**

* 根据姓名查找这个用户 (模拟一下数据库)

* @param name

* @return

*/

public Users findUserByName(String name)

{

if(name.equals(this.users.getName()))

{

return this.users;

}

return null;

}

}

业务类 包service (验证用户名和密码)

------------------------------------------------------------

package service;

import dao.UsersDAO;

import entity.Users;

/**

* 验证密码 业务类

* @author new

*

*/

public class validatePass {

//实力化DAO对象

private UsersDAO us=new UsersDAO();

/**

* 验证输入的密码是否正确

* @param name

* @param pass

* @return

*/

public Users validate(String name,String pass)

{

Users user=null;

user=us.findUserByName(name);

//如果不为空 说明查到了

if(user!=null)

{

//用查询出来对象的密码和传过来的密码比较

if(user.getPass().equals(pass))

{

return user;

}

}

return null;

}

}

最后是测试test类 包test

----------------------------------------------------------

package test;

import entity.Users;

import service.validatePass;

/**

* 测试类

* @author new

*

*/

public class test {

/**

* main方法 用于测试

* @param args

*/

public static void main(String[] args)

{

//实例化业务类对象

validatePass v=new validatePass();

//用户名和密码

String name="tom";

String pass="jerry";

//开始验证

Users user=v.validate(name, pass);

if(user==null)

{

System.out.println("你输入的用户名或密码错误!");

}else

{

System.out.println("你已经通过验证,成功登陆!");

}

}

}

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源代码百度 java操作数据库代码
网页路径:http://6mz.cn/article/dosdehd.html

其他资讯