package com.xushiwei;import java.beans.Statement;import java.sql.*;public class jdbc { private static final String URL="jdbc:mysql://127.0.0.1:3306/xushiwei"; private static final String USER="root"; private static final String PASSWORD="123456";? ? ?public static void main(String[] args) throws ClassNotFoundException, SQLException { ?Class.forName("com.mysql.jdbc.Driver"); ? Connection conn=DriverManager.getConnection(URL,USER,PASSWORD);? ? ? ?java.sql.Statement ?xushi=conn.createStatement();? ? ? ? ResultSet x=xushi.executeQuery("select name from miao ");? ? ? ? while(x.next()){? ? ? ? System.out.println(x.getString("name"));? ? ? ? }? ? ?} }
7 回答

染紅_街道
TA貢獻3條經(jīng)驗 獲得超0個贊
Class.forName("com.mysql.jdbc.Driver");
g改成????? Class.forName("org.gjt.mm.mysql.Driver");

狙翎利利
TA貢獻3條經(jīng)驗 獲得超0個贊
導入mysql的驅動包了嗎?是一個工具jar包。因為看了眼說異常沒有發(fā)現(xiàn)驅動類。另外一個建議就是在數(shù)據(jù)庫的使用結束以后要關閉連接。數(shù)據(jù)庫的開關工作,重新定義一個類(作為數(shù)據(jù)庫操作的工具類,里面封裝數(shù)據(jù)庫的開和關,因為在數(shù)據(jù)庫操作時候要經(jīng)常使用開和關)。謝謝~
添加回答
舉報
0/150
提交
取消