JAVA连接MYSQL配置
时间:2007-09-04 来源:hello386
先说下我的电脑的JAVA环境,
JDK 1.5 (下载地址:http: //192.18.108.215/ECom/EComTicketServlet/BEGIND5B49F01C1A7D4A81759BEFC69957E99/-2147483648/1965003195/1/798890/798638/1965003195/2ts+/westCoastFSEND/jdk-1.5.0_11-oth-JPR/jdk-1.5.0_11-oth-JPR:2/jdk-1_5_0_11-windows-i586-p.exe)
netbean 5.5(下载地址:http://us1.mirror.netbeans.org/download/5_5/mlfcs/200612070100/netbeans-5_5-windows-zh_CN.exe)
mysql 5.01(下载地址:http://www.skycn.com/soft/6916.html)
mysql-connector-java-5.0.4-bin(下载地址:http://mysql.holywar.net/Downloads/Connector-J/mysql-connector-java-5.0.4.zip)
classpath路径为: e:\(添加用户变量时注意 .:e:\)
修改系统变量 ;C:\Program Files\Java\jdk1.5.0_11\bin
以 上是基本条件,下面一步就是把mysql-connector-java-5.0.4-bin解压后里面有一个mysql-connector-java -5.0.4-bin.jar的文件,把此文件复制到C:\Program Files\Java\jre1.5.0_11\lib\ext下,就可以拉,下面介绍几个网上别人写的访问程序:
1,简单测试用的
import java.sql.*;
import java.lang.*;
public class test{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");//驱动的名称
//或者:Class.forName("org.gjt.mm.mysql.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=286772");//访问的数据库的帐号密码
Statement s=c.createStatement();
ResultSet r=s.executeQuery("select * from first");//执行SQL语句
while(r.next()){
System.out.println(r.getString("name"));//返回的结果
}
s.close();
}catch(Exception e){e.printStackTrace();}//捕获异常
}
} 2,下面的方法可以根据特定的条件访问 import java.sql.*;
import java.io.*;
public class test{ 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/test","root","286772");/*test为在MYSQL下建
立的数据库名字,286772是进入数据库的密码*/
con.setCatalog("test");
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);
} }
}
import java.lang.*;
public class test{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");//驱动的名称
//或者:Class.forName("org.gjt.mm.mysql.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=286772");//访问的数据库的帐号密码
Statement s=c.createStatement();
ResultSet r=s.executeQuery("select * from first");//执行SQL语句
while(r.next()){
System.out.println(r.getString("name"));//返回的结果
}
s.close();
}catch(Exception e){e.printStackTrace();}//捕获异常
}
} 2,下面的方法可以根据特定的条件访问 import java.sql.*;
import java.io.*;
public class test{ 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/test","root","286772");/*test为在MYSQL下建
立的数据库名字,286772是进入数据库的密码*/
con.setCatalog("test");
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);
} }
}
相关阅读 更多 +