项目涉及java编程与数据库的数据交互,需要怎么开始?
已经安装了Microsoftserver2005,还需要安装什么东西吗?需要怎样才能够将比如一个string存到数据库中,最好有代码示例。...
已经安装了Microsoft server 2005,还需要安装什么东西吗?需要怎样才能够将比如一个string存到数据库中,最好有代码示例。
展开
4个回答
展开全部
package com.briup.jdbc.basic;
import java.sql.*;
import oracle.jdbc.driver.OracleTypes;
public class FirstJdbc {
private static final String driver_name="oracle.jdbc.driver.OracleDriver";//驱动器的名字
private static final String db_url="jdbc:oracle:thin:@127.0.0.1:1521:xe";//数据库实例的名字
private static final String user="jd1201";//登录数据库的用户名
private static final String password="jd1201";/登录数据库的用户名
public static void main(String[] args) {
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
//int code;
try {
//1.注册驱动程序
//1.1 using class loader
Class.forName(driver_name);
//2.建立数据库连接
//2.1 query the driver
con=DriverManager.getConnection(db_url,user,password);
//3.建立statement对象
stmt = con.createStatement();
System.out.println("====insert====");
//往表中插入数据的语句
String insertSql="insert into account_tbl values(111,'34239585','mJack1','8743428343'," +
"5000.00,sysdate,sysdate)";
int counter=stmt.executeUpdate(insertSql);
System.out.println("counter:"+counter);
//4.操作查询语句
rs=stmt.executeQuery("select account_id,account_code,name," +
"identity_id,balance,open_date,operation_date from account_tbl");
//5.处理结果
while(rs.next()){
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
System.out.println(rs.getString(4));
System.out.println(rs.getDouble(5));
System.out.println(rs.getDate(6));
System.out.println(rs.getDate(7));
}
System.out.println("====update====");
String updateSql="update account_tbl set balance=20000.00";
counter=stmt.executeUpdate(updateSql);
System.out.println("counter:"+counter);
System.out.println("====delete====");
String deleteSql="delete from account_tbl where account_id=100";
counter=stmt.executeUpdate(deleteSql);
System.out.println("counter:"+counter);*/
} catch (Exception e) {
e.printStackTrace();
}
finally{
//6.释放资源
if(rs!=null)try{rs.close();}catch (SQLException e) {}
if(stmt!=null)try{stmt.close();}catch(SQLException e){}
if(con!=null)try{con.close();}catch(SQLException e){}
}
}
}
//你还得有数据库驱动.jar包
import java.sql.*;
import oracle.jdbc.driver.OracleTypes;
public class FirstJdbc {
private static final String driver_name="oracle.jdbc.driver.OracleDriver";//驱动器的名字
private static final String db_url="jdbc:oracle:thin:@127.0.0.1:1521:xe";//数据库实例的名字
private static final String user="jd1201";//登录数据库的用户名
private static final String password="jd1201";/登录数据库的用户名
public static void main(String[] args) {
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
//int code;
try {
//1.注册驱动程序
//1.1 using class loader
Class.forName(driver_name);
//2.建立数据库连接
//2.1 query the driver
con=DriverManager.getConnection(db_url,user,password);
//3.建立statement对象
stmt = con.createStatement();
System.out.println("====insert====");
//往表中插入数据的语句
String insertSql="insert into account_tbl values(111,'34239585','mJack1','8743428343'," +
"5000.00,sysdate,sysdate)";
int counter=stmt.executeUpdate(insertSql);
System.out.println("counter:"+counter);
//4.操作查询语句
rs=stmt.executeQuery("select account_id,account_code,name," +
"identity_id,balance,open_date,operation_date from account_tbl");
//5.处理结果
while(rs.next()){
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
System.out.println(rs.getString(4));
System.out.println(rs.getDouble(5));
System.out.println(rs.getDate(6));
System.out.println(rs.getDate(7));
}
System.out.println("====update====");
String updateSql="update account_tbl set balance=20000.00";
counter=stmt.executeUpdate(updateSql);
System.out.println("counter:"+counter);
System.out.println("====delete====");
String deleteSql="delete from account_tbl where account_id=100";
counter=stmt.executeUpdate(deleteSql);
System.out.println("counter:"+counter);*/
} catch (Exception e) {
e.printStackTrace();
}
finally{
//6.释放资源
if(rs!=null)try{rs.close();}catch (SQLException e) {}
if(stmt!=null)try{stmt.close();}catch(SQLException e){}
if(con!=null)try{con.close();}catch(SQLException e){}
}
}
}
//你还得有数据库驱动.jar包
展开全部
先写domain和建立数据库表格,再java编程,最后通过jdbc连接数据库,通过dao层连接
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼上的方法可行,不过这个还得自己去学习下java编程,如果一点儿都不会的话,交互数据就很难。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
还要驱动,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询