ºìÁªLinuxÃÅ»§
Linux°ïÖú

dz̸ÈçºÎÓ¦ÓÃJDBCÁ¬½ÓÊý¾Ý¿âMy SQL

·¢²¼Ê±¼ä:2006-09-23 00:03:22À´Ô´:ºìÁª×÷Õß:fengmayi1
¡¡¡¡×î½üÔÚѧϰÊý¾Ý¿â¿ª·¢µÄһЩʵÀý£¬ÕâÀïdz̸һÏÂÓÃJDBCÁ¬½ÓÊý¾Ý¿âMySQL£¨µ±È»Ò²¿ÉÒÔÁ¬½ÓSQL Sever»òOracleÁË£¬Ö»ÊÇÎÒ¸üϲ»¶¿ªÔ´Èí¼þ£¬Í¬Ê±Ò²¸ü¼òµ¥£©¡£

¡¡¡¡Ê×ÏÈÕýÈ·°²×°ºÃMySQL£¬½¨Á¢ºÃÊý¾Ý¿âstudentinfo

[code]mysql>create database studentinfo;[/code]

¡¡¡¡È»ºó±àдjava´úÂ룬ConnectToMySQL.java

[code]import java.sql.*;
public class ConnectToMySQL {
public static Connection getConnection() throws SQLException ,
java.lang.ClassNotFoundException{
String url = "jdbc:mysql://localhost:3306/studentinfo";
Class.forName("com.mysql.jdbc.Driver");
String userName = "root";
String password = "";
Connection con = DriverManager.getConnection(url,userName,password);
return con;
}
public static void main(String[] args) {
try{
Connection con = getConnection();
Statement sql = con.createStatement();
sql.execute("drop table if exists student");
sql.execute("create table student(id int not null auto_increment,name varchar(20) not
null default 'name',math int not null default 60,primary key(id));");
sql.execute("insert student values(1,'AAA','99')");
sql.execute("insert student values(2,'BBB','77')");
sql.execute("insert student values(3,'CCC','65')");
String query = "select * from student";
ResultSet result = sql.executeQuery(query);
System.out.println("Student±íÊý¾ÝÈçÏ£º");
System.out.println("---------------------------------");
System.out.println("ѧºÅ"+" "+"ÐÕÃû"+" "+"Êýѧ³É¼¨");
System.out.println("---------------------------------");
int number;
String name;
String math;
while(result.next()){
number = result.getInt("id");
name = result.getString("name");
math = result.getString("math");
System.out.println(number + " " + name + " " + math);
}
sql.close();
con.close();

}catch(java.lang.ClassNotFoundException e){
System.err.println("ClassNotFoundException:" + e.getMessage());
}catch(SQLException ex){
System.err.println("SQLException:" + ex.getMessage());
}
}
}[/code]

¡¡¡¡ÇáËɸ㶨£¬Ò»ÏÂΪÊä³ö½á¹û£º

[align=center][/align]

¡¡¡¡Òª×¢ÒâµÄÊÇʹÓÃMySQLÊý¾Ý¿â£¬ÐèÒªÓõ½¶ÔÓ¦µÄJDBCÇý¶¯³ÌÐòmysql-connector-java-5.0.3£¬¿ÉÒÔµ½zySQLµÄ¹Ù·½ÍøÕ¾ÉÏÏÂÔØ(http://www.mysql.org)
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 91 ÌõÆÀÂÛ

  1. 201.210.155.* ÓÚ 2007-05-16 03:19:32·¢±í:

    http://6aef7944e1719997112193e4a354e7f5-t.qwoypw.info 6aef7944e1719997112193e4a354e7f5 http://6aef7944e1719997112193e4a354e7f5-b1.qwoypw.info 6aef7944e1719997112193e4a354e7f5 http://6aef7944e1719997112193e4a354e7f5-b3.qwoypw.info b43a48a848da56275457e93295654b68