本文目录一览

1,如何在JSP中进行数据库连接

很多数据库系统带有JDBC驱动程序,Java程序就通过JDBC驱动程序与数据库相连,执行查询、提取数据等等操作。Sun公司还开发了JDBC-ODBC bridge,用此技术Java程序就可以访问带有ODBC驱动程序的数据库,目前大多数数据库系统都带有ODBC驱动程序,所以Java程序能访问诸如Oracle、Sybase、MS SQL Server和MS Access等数据库。下面介绍如何用Access实现一个动态FAQ(常见问题及答案)网站。首先建立一个Access数据库faq.mdb,其中的表faqs有字段id(自动增量型,并设为主关键字)、subject(文字型,长度200)、answers(备注型)。这个表中可以存放一些编程知识的常见问题及答案, 然后,在Control Panel(控制面板)的ODBC Datasource模块中加入System DSN,取名faq,并指向faq.mdb。 创建一个JavaBean,名为faq.java,并保存在jswdk-1.0.1webpagesWEB-INFjspeans est目录下。faq.java 的内容如下:

如何在JSP中进行数据库连接

2,JSP怎样连接数据库使用呀 高手 帮帮忙

使用JDBC连接 需要到SUN官方网站下载
上网找下java-jdbc-connector.jar这个jdbc驱动包 代码实现连接: Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost?数据库名","用户名","密码"); Statement stmt = conn.creatStatement(); String sql = "select * from 表名"; ResultSet rs = stmt.excuteQuery(sql); 表里如果只有两表(Id name) 可以通过下面取得对应值 rs.getInt("Id")/rs.getInt(1) rs.getString("name")/rs.getString(2)
JDBC呀 自己再写通用的DBHelper类 这个类网上很多你可以搜索下相关资料

JSP怎样连接数据库使用呀 高手 帮帮忙

3,Jsp中数据库的连接怎么做

import java.sql.*;public class DBCon public DBCon() } public Connection getConnection() Connection conn=null; try Class.forName("oracle.jdbc.driver.OracleDriver");//驱动程序名 String url="jdbc:oracle:thin:@192.168.1.51:1521:evatesoft"; //192.168.1.51为服务器的地址 //ads2为数据库的SID ads2 String user="evatesoft";//数据库的用户名 String password="evatesoft";//数据库的密码 conn= DriverManager.getConnection(url,user,password); } catch(Exception e) System.out.println(e.toString()); } return conn; }}在jsp中<%DBCon db = new DBCon(); %>
① driverclass=”sun.jdbc.odbc.jdbcodbcdriver”② url=”jdbc:odbc:driver=连接数据库查询表的相关语句(表userinfo中字段有username,password,下同): class.forname("sun.jdbc.odbc.jdbcodbcdriver"); connection conn=drivermanager.getconnection("jdbc:odbc:driver= statement stmt=conn.createstatement(); resultset rs=stmt.executequery("select * from userinfo"); while(rs.next()) out.print("用户名:"+rs.getstring("username")+"密码:"+rs.getstring("password")); } rs.close(); stmt.close(); conn.close();

Jsp中数据库的连接怎么做

4,JSP怎样链接数据库

难得讲:直接连接和关闭代码:private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// 驱动类 private static final String URL = "jdbc:sqlserver://localhost:1433;DataBaseName=paipaiDB";// 连接URL地址 private static final String USER = "sa";// 数据库用户名 private static final String PWD = "123";// 数据库用户密码 /** * 与数据库建立连接 * * @return * @throws ClassNotFoundException * @throws SQLException */ public static Connection getCon() throws ClassNotFoundException, SQLException Connection con = null; Class.forName(DRIVER); con = DriverManager.getConnection(URL, USER, PWD); return con; } /** * 关闭所有与数据库的连接对象 * * @param res * 结果集对象 * @param pstat预编义对象 * @param con连接对象 */ public static void closeAll(ResultSet res, PreparedStatement pstat, Connection con) if (res != null) try res.close(); } catch (SQLException e) // TODO 自动生成 catch 块 e.printStackTrace(); } } if (pstat != null) try pstat.close(); } catch (SQLException e) // TODO 自动生成 catch 块 e.printStackTrace(); } } try if (con != null && !con.isClosed()) con.close(); } } catch (SQLException e) // TODO 自动生成 catch 块 e.printStackTrace(); } }
try class.forname("sun.jcbc.odbc.jdbcodbcdriber"); // 使用odbc驱动,你的写错了:sun.jdbc.odbc.jdbcodbcdriver string url = "jdbc:odbc:user_db"; // 数据源url connection con = drivermanager.getconnection(url, "insher","insher"); // 获得连接 preparedstatement stm = con.preparestatement("insert into message values(?,?,?,?,?)"); // 执行预处理插入语句 /* stm.set*() 填充上面insert语句中的? */ stm.setstring(1, title); stm.setstring(2, name); if (mail.length() == 0) // 判断mail是否为"",yes插入null stm.setstring(3, null); else stm.setstring(3, mail); stm.setstring(4, datetime); stm.setstring(5, content); /* stm.set*() 结束 */ try stm.executequery(); // 执行sql语句 } catch (exception e) } // stm.close(); // 关闭连接前建议先关闭stm con.close(); // 关闭连接 } catch (exception e) }

5,jsp系统怎么连接数据库

请问下是什么结构?用jdbc连接的话:public class DBUtil private static String user; private static String password; private static String url; static Properties prop=new Properties(); try ClassLoader classLoader=DBUtil.class.getClassLoader(); InputStream is=classLoader.getResourceAsStream("db.properties"); prop.load(is); user=prop.getProperty("user"); password=prop.getProperty("password"); url=prop.getProperty("url"); Class.forName("com.mysql.jdbc.Driver"); } catch (Exception e) e.printStackTrace(); throw new RuntimeException("找不到加载类"); } } public static Connection getConnection()throws Exception Connection conn=null; conn=DriverManager.getConnection(url,user,password); return conn; } public static void close(Connection conn) if(conn!=null) try conn.close(); } catch (SQLException e) e.printStackTrace(); } } } public static void main(String[] args)throws Exception System.out.println(DBUtil.getConnection()); }}如果是用SSH架构的话,用hibernate里面去配置就OK了!
书上到处都是。。。
一、jsp连接oracle8/8i/9i数据库(用thin模式)testoracle.jsp如下:<%@ page contenttype="text/html;charset=gb2312"%><%@ page import="java.sql.*"%><html><body><%class.forname("oracle.jdbc.driver.oracledriver").newinstance();string url="jdbc:oracle:thin:@localhost:1521:orcl";//orcl为你的数据库的sidstring user="scott";string password="tiger";connection conn= drivermanager.getconnection(url,user,password);statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);string sql="select * from test";resultset rs=stmt.executequery(sql);while(rs.next()) 您的第一个字段内容为:<%=rs.getstring(1)%>您的第二个字段内容为:<%=rs.getstring(2)%><%}%><%out.print("数据库操作成功,恭喜你\");%><%rs.close();stmt.close();conn.close();%></body></html>二、jsp连接sql server7.0/2000数据库testsqlserver.jsp如下:<%@ page contenttype="text/html;charset=gb2312"%><%@ page import="java.sql.*"%><html><body><%class.forname("com.microsoft.jdbc.sqlserver.sqlserverdriver").newinstance();string url="jdbc:microsoft:sqlserver://localhost:1433;databasename=pubs";//pubs为你的数据库的string user="sa";string password="";connection conn= drivermanager.getconnection(url,user,password);statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);string sql="select * from test";resultset rs=stmt.executequery(sql);while(rs.next()) 您的第一个字段内容为:<%=rs.getstring(1)%>您的第二个字段内容为:<%=rs.getstring(2)%><%}%><%out.print("数据库操作成功,恭喜你\");%><%rs.close();stmt.close();conn.close();%></body></html>
在jsp页面写链接数据库的脚本,在网上随便搜就有,这个跟你的数据库类型有关系

文章TAG:jsp数据库连接哪个表  如何在JSP中进行数据库连接  
下一篇