a:建立数据库和表
b:控制面板->性能和维护->管理工具->数据源
添加数据源名称--选择数据库--设置用户名和密码--确定[@more@]2:在程序里:
public class JdbcTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection con=null;
Statement st=null;
ResultSet rs=null;
String bookName="java2";
double bookPrice=67;
try {
//1.注册驱动
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//2.连接到数据库
con = DriverManager.getConnection("jdbc:odbc:redsun","aa","aa");//jdbc:odbc:是固定的redsun是数据源的 名字,不是数据库的名字!!!后面两个参数 是:用户名和密码
//3.发送sql语句
st = con.createStatement();
//4.处理结果集
//select
rs = st.executeQuery("select * from book");//查询是用executeQuery方法!!!
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
double price = rs.getDouble("price");
System.out.print(id+" "+name+" "+price);
}
//insert 所有更新都是用executeUpdate!!!
st.executeUpdate("insert into book values(5,'"+bookName+"',"+bookPrice+")");//注意变量的书写方式!!!
//update
st.executeUpdate("update book set price=44,name='mm' where id=1");
//delete
st.executeUpdate("delete from book where id=2");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
//5.关闭连接
try {
rs.close();
st.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
----------------------------------------------
常用数据库驱动: ----------------------------------------------- 连接URL
a:JDBC-ODBC: sun.jdbc.odbc.JdbcOdbcDriver ----------------------jdbc:odbc:redsun","aa","aa"
b:mysql: com.mysql.jdbc.Driver //org.gjt.mm.mysql.Driver--------jdbc:mysql://localhost:3306/test","root","lxl"
c:sqlserver: com.microsoft.jdbc.sqlserver.SQLServerDriver-----------jdbc:microsoft:sqlserver://192.168.1.134:1400;DatabaseName =test,"aa","aa"
d:oracle: oracle.jdbc.OracleDriver-------------------------------jdbc:oracle:thin:@localhost:1521:lxldb","aa","aa"
b:控制面板->性能和维护->管理工具->数据源
添加数据源名称--选择数据库--设置用户名和密码--确定[@more@]2:在程序里:
public class JdbcTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection con=null;
Statement st=null;
ResultSet rs=null;
String bookName="java2";
double bookPrice=67;
try {
//1.注册驱动
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//2.连接到数据库
con = DriverManager.getConnection("jdbc:odbc:redsun","aa","aa");//jdbc:odbc:是固定的redsun是数据源的 名字,不是数据库的名字!!!后面两个参数 是:用户名和密码
//3.发送sql语句
st = con.createStatement();
//4.处理结果集
//select
rs = st.executeQuery("select * from book");//查询是用executeQuery方法!!!
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
double price = rs.getDouble("price");
System.out.print(id+" "+name+" "+price);
}
//insert 所有更新都是用executeUpdate!!!
st.executeUpdate("insert into book values(5,'"+bookName+"',"+bookPrice+")");//注意变量的书写方式!!!
//update
st.executeUpdate("update book set price=44,name='mm' where id=1");
//delete
st.executeUpdate("delete from book where id=2");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
//5.关闭连接
try {
rs.close();
st.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
----------------------------------------------
常用数据库驱动: ----------------------------------------------- 连接URL
a:JDBC-ODBC: sun.jdbc.odbc.JdbcOdbcDriver ----------------------jdbc:odbc:redsun","aa","aa"
b:mysql: com.mysql.jdbc.Driver //org.gjt.mm.mysql.Driver--------jdbc:mysql://localhost:3306/test","root","lxl"
c:sqlserver: com.microsoft.jdbc.sqlserver.SQLServerDriver-----------jdbc:microsoft:sqlserver://192.168.1.134:1400;DatabaseName =test,"aa","aa"
d:oracle: oracle.jdbc.OracleDriver-------------------------------jdbc:oracle:thin:@localhost:1521:lxldb","aa","aa"
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/663635/viewspace-923145/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/663635/viewspace-923145/