java中操作数据库代码 java中操作数据库代码的命令

求:用Java连接数据库和简单的数据库操作代码

以上的代码都不如哥的 且看哥是怎么写条理清晰的代码的!!!

成都创新互联公司服务项目包括宁安网站建设、宁安网站制作、宁安网页制作以及宁安网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,宁安网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到宁安省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

package dbconnection //java 中不存在没有包的类(讲解详细因为项目需要)

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;//引入sql数据库包

public class DBConnection{

private Connection conn=null;

private Statement stmt=null;

private Result rs=null;

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

private String driverManager="jdbc:sqlserver://localhost:1433;databasename=HcitPos";

private String user="admin";

private int password="admin";

public DBConnection{

try{

Class.forName("jdbc");

conn.getConnection("driverManager");

}

catch(Exception e){}

}

public selectMethod(String sql){

stmt=conn.createStatement();

rs=stmt.extcuteQuery("sql");

while(rs.next()){

String title=rs.getString("title");//利用javaBean获得数据库中的属性

String name=rs.getString("name");

.......

System.out.println("title");

System.out.println("name");

......

//当然数据库的操作有很多 这里简单介绍下功能的实现

}

}

public void closeDB(){

if(rs != null) rs.close();

if(stmt != null) stmt.close();

if(conn != null) conn.close();

}

}

通过java代码如何实现对mysql数据库进行创建新的数据库的操作

1 import java.sql.Connection;

2 import java.sql.DriverManager;

3 import java.sql.ResultSet;

4 import java.sql.SQLException;

5 import java.sql.Statement;

6

7 public class CreateDataSource {

8

9 /**

10 * @param args

11 */

12 public static void main(String[] args) {

13 // TODO Auto-generated method stub

14 String database = "test2";

15 new CreateDataSource().getConn(database);

16 }

17

18 String mysqlDriver = "com.mysql.jdbc.Driver";

19 String url = "jdbc:mysql://localhost:3306/test1";

20 String newUrl = "jdbc:mysql://localhost:3306/";

21 String username = "root";

22 String password = "root";

23 Connection conn = null;

24 Connection newConn = null;

25

26 public Connection getConn(String database) {

27

28 try {

29 Class.forName(mysqlDriver);

30 } catch (ClassNotFoundException e) {

31 // TODO Auto-generated catch block

32 e.printStackTrace();

33 }

34 try {

35 String tableSql = "create table t_user (username varchar(50) not null primary key,"

36 + "password varchar(20) not null ); ";

37 String databaseSql = "create database " + database;

38

39 conn = DriverManager.getConnection(url, username, password);

40 Statement smt = conn.createStatement();

41 if (conn != null) {

42 System.out.println("数据库连接成功!");

43

44 smt.executeUpdate(databaseSql);

45

46 newConn = DriverManager.getConnection(newUrl + database,

47 username, password);

48 if (newConn != null) {

49 System.out.println("已经连接到新创建的数据库:" + database);

50

51 Statement newSmt = newConn.createStatement();

52 int i = newSmt.executeUpdate(tableSql);//DDL语句返回值为0;

53 if (i == 0) {

54 System.out.println(tableSql + "表已经创建成功!");

55 }

56 }

57 }

58

59 } catch (SQLException e1) {

60 // TODO Auto-generated catch block

61 e1.printStackTrace();

62 }

63 return conn;

64 }

65 }

java连接数据库的代码

用这个类吧.好的话,给我加加分.

import java.sql.*;

/**

* @功能: 一个JDBC的本地化API连接类,封装了数据操作方法,只用传一个SQL语句即可

* @作者: 李开欢

* @日期: 2007/

*/

public class ConnectionDemo {

/*

* 这里可以将常量全部放入另一个类中,以方便修改

*/

private static Connection conn;

private static Statement ps;

private static ResultSet rs;

private static final String DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

private static final String URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";

private static final String USER ="sa";

private static final String PASS = "sa";

public ConnectionDemo() {

// TODO Auto-generated constructor stub

ConnectionDemo.getConnection();

}

public static Connection getConnection(){

System.out.println("连接中...");

try {

Class.forName(ConnectionDemo.DRIVER);

conn = DriverManager.getConnection(ConnectionDemo.URL, ConnectionDemo.USER, ConnectionDemo.PASS);

System.out.println("成功连接");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return conn;

}

public static Statement getStatement(String sql){

System.out.println("执行SQL语句中...");

try {

ps = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

if(sql.substring(0, 6).equals("select")){

rs = ps.executeQuery(sql);

System.out.println("执行完查询操作,结果已返回ResultSet集合");

}else if(sql.substring(0, 6).equals("delete")){

ps.executeUpdate(sql);

System.out.println("已执行完毕删除操作");

}else if(sql.substring(0, 6).equals("insert")){

ps.executeUpdate(sql);

System.out.println("已执行完毕增加操作");

}else{

ps.executeUpdate(sql);

System.out.println("已执行完毕更新操作");

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return ps;

}

public static ResultSet getResultSet(){

System.out.println("查询结果为:");

return rs;

}

public static void closeConnection(){

System.out.println("关闭连接中...");

try {

if (rs != null) {

rs.close();

System.out.println("已关闭ResultSet");

}

if (ps != null) {

ps.close();

System.out.println("已关闭Statement");

}

if (conn != null) {

conn.close();

System.out.println("已关闭Connection");

}

} catch (Exception e) {

// TODO: handle exception

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

ConnectionDemo.getConnection();

String sql = "delete from type where id = 1";

ConnectionDemo.getStatement(sql);

String sql2 = "insert into type values(1,'教学设备')";

ConnectionDemo.getStatement(sql2);

String sql1 = "select * from type";

ConnectionDemo.getStatement(sql1);

ResultSet rs = ConnectionDemo.getResultSet();

System.out.println("编号 "+"类 型");

try {

while(rs.next()){

System.out.print(" "+rs.getInt(1)+" ");

System.out.println(rs.getString(2));

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

ConnectionDemo.closeConnection();

}

}

java操作数据库

createStatement 这个不是报错,只是这句可能会抛出异常,这种情况在java中是必须使用 try catch 捕捉的。 你只要把代码块用 try catch 包起来就可以了。

如何通过Java代码操作数据库

//此类为连接数据库并进行数据库的操作

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class Conn {

private static Connection conn = null;

private static Statement st = null;

private static ResultSet rs = null;

//建立数据库的连接


文章标题:java中操作数据库代码 java中操作数据库代码的命令
标题路径:http://pwwzsj.com/article/hiecpo.html