java 应用程序 怎么用到数据库? java 应用程序 怎么用到数据库? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 首先要加载相应的数据库驱动通过JDBC API对数据库进行操作详细你可以看一些相关的书籍 用JDBC了访问数据库。以Oracle为例,使用下面的代码连接数据库:Oracle数据库 Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID String user="test"; String password="test"; Connection conn= DriverManager.getConnection(url,user,password); package sqldriver; import java.sql.*; import java.io.*; public class testMysql { public static void main(String[] args) { String tableName=null;//表名 BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); try{ Class.forName("com.mysql.jdbc.Driver");//加载驱动程序 System.out.println("OK,DownLoad the driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost/hjl","root","123456");/*hjl为在MYSQL下建 立的数据库名字,123456是进入数据库的密码*/ con.setCatalog("hjl"); System.out.println("GOOG,Connect the DataBase"); Statement statement = con.createStatement();//数据库操作 System.out.print("请输入表名:"); tableName=input.readLine(); String sql="SELECT * FROM "+tableName; ResultSet rs = statement.executeQuery(sql); System.out.print("请输入列名:"); String cName=input.readLine(); // 获得数据结果集合 if(!rs.next()) System.out.println("表名或列名输入有误或到了末尾"); else { System.out.println("查询结果为:"); do { String result=rs.getString(cName); result=new String(result.getBytes("ISO-8859-1"),"GB2312"); System.out.println(result); }while(rs.next()); } rs.close(); con.close(); }catch(Exception ex){ System.out.println(ex); System.exit(0); } } } 各位前辈麻烦你 们帮我看看这个问题 请教个关于java的小问题 新手教程第一课之接口的理解 如何学习Java [GUI]如何实现swing中JTree的单个节点可编辑?? 怎么把.java文件编译成可执行文件 为什么结果是 double, double, double.(30分钟内结帖) 快发疯了~求救~ 如何在JBUILDER中将两个JAVA 文件编译成为class文件? JAVA区怎么没有斑竹啊?难道国内的JAVA水平真的这么差! java中的+=运算符和=+都分别指什么呢?能够给出实际例子来么? 如何在命令行中运行指定的java文件
通过JDBC API对数据库进行操作
详细你可以看一些相关的书籍
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
import java.sql.*;
import java.io.*;
public class testMysql { public static void main(String[] args) {
String tableName=null;//表名
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
try{
Class.forName("com.mysql.jdbc.Driver");//加载驱动程序
System.out.println("OK,DownLoad the driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/hjl","root","123456");/*hjl为在MYSQL下建
立的数据库名字,123456是进入数据库的密码*/
con.setCatalog("hjl");
System.out.println("GOOG,Connect the DataBase");
Statement statement = con.createStatement();//数据库操作
System.out.print("请输入表名:");
tableName=input.readLine(); String sql="SELECT * FROM "+tableName;
ResultSet rs = statement.executeQuery(sql);
System.out.print("请输入列名:");
String cName=input.readLine();
// 获得数据结果集合
if(!rs.next())
System.out.println("表名或列名输入有误或到了末尾");
else {
System.out.println("查询结果为:");
do
{
String result=rs.getString(cName);
result=new String(result.getBytes("ISO-8859-1"),"GB2312");
System.out.println(result);
}while(rs.next());
}
rs.close();
con.close();
}catch(Exception ex){
System.out.println(ex);
System.exit(0);
} }
}