全球主机交流论坛

标题: 哪位熟悉怎样让jsp连接上mysql db? [打印本页]

作者: luckypoem    时间: 2011-10-22 23:40
标题: 哪位熟悉怎样让jsp连接上mysql db?
我搞了半天,都没搞定。指点一下?
作者: cnweb    时间: 2011-10-22 23:41
我有JSP高端主机,性能刚刚的!
作者: caboo    时间: 2011-10-22 23:41
提示: 作者被禁止或删除 内容自动屏蔽
作者: yexinzhu    时间: 2011-10-22 23:42
楼下来
作者: wdlth    时间: 2011-10-22 23:50
JDBC
很简单吧
作者: maksim    时间: 2011-10-23 00:17
  1. package yy;
  2. import java.io.*;
  3. import java.sql.*;
  4. public class jdbc {

  5.   public jdbc() {
  6.   }
  7. Connection conn = null ;
  8. //String re = "" ;
  9. //设置你的数据库ip
  10. //String dbip = "127.0.0.1" ;
  11. //设置你的数据库用户名和密码:
  12. //String use = "" ;
  13. //String pass = "" ;
  14.   public java.sql.Connection getConn(){
  15.     try{
  16.      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
  17.      //String url ="jdbc:mysql://"+dbip+":3306/"+use+"?user="+use+"&password="+pass+"&useUnicode=true&characterEncoding=gb2312" ;
  18.      conn= DriverManager.getConnection("jdbc:odbc:yyForum","xyworker","999");
  19.      ///Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  20.        
  21.         }
  22.     catch(Exception e){
  23.     e.printStackTrace();
  24.     System.out.println("LICHAO");
  25.     }
  26.     return this.conn ;
  27.   }
复制代码
或许可以
作者: 落霞孤鹜    时间: 2011-10-23 12:10
import java.sql.*;
public class DbConn
{
        static String drivername = "com.mysql.jdbc.Driver";    //数据库驱动,这里是MySQL的驱动
        static String url = "db_url";   //数据库url
        static String username = "db_user";   // 数据库用户名
        static String password = "db_pass";    //数据库密码
        static{
                try{
                        Class.forName(drivername);    //加载驱动程序
                } catch(ClassNotFoundException e) {
                        e.printStackTrace();
                }
        }

        public static Connection getConn(){    //获取连接
                Connection conn = null;
                try {
                        conn=(Connection) DriverManager.getConnection(url, username, password);
                } catch(SQLException e) {
                        e.printStackTrace();
                }
                return conn;  //返回连接
        }
}

这是JDBC连接的




欢迎光临 全球主机交流论坛 (https://hostloc-workers.ikyomon.com/) Powered by Discuz! X3.4